Relativelayout: Activity - Main - XML
Relativelayout: Activity - Main - XML
xml
<Button
android:id="@+id/bounceBallButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="70dp"
android:rotationX="13"
android:text="Throw Ball" />
<ImageView
android:id="@+id/bounceBallImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bounceBallButton"
android:layout_marginTop="108dp"
android:background="@drawable/ball_shape" />
<ImageView
android:id="@+id/bat"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_below="@id/bat"
android:layout_alignParentEnd="true"
android:layout_marginEnd="2dp"
android:background="@drawable/bat_shape" />
<ImageView
android:id="@+id/handle"
android:layout_width="69dp"
android:layout_height="wrap_content"
android:layout_below="@id/bat"
android:layout_alignParentEnd="true"
android:layout_marginTop="-37dp"
android:layout_marginEnd="147dp"
android:background="@drawable/handle_shape" />
<Button
android:id="@+id/fadeIn"
android:text="Fade In"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/fadeOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fade Out" />
</RelativeLayout>
ball_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="2dp"
android:color="#fff" />
<size
android:height="80dp"
android:width="80dp" />
</shape>
bat_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#fff" />
<size
android:height="60dp"
android:width="150dp" />
</shape>
handle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#fff" />
<size
android:height="30dp"
android:width="80dp" />
</shape>
MainActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.animation.ObjectAnimator;
import android.view.animation.AlphaAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.bounceBallImage);
Button fadeIn = findViewById(R.id.fadeIn);
fadeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(1000);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(alphaAnimation);
}
});
Button fadeOut = findViewById(R.id.fadeOut);
fadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView,
"alpha", 1f, 0);
fadeOut.setDuration(2000);
fadeOut.start();
}
});
Button bounceBallButton = (Button) findViewById(R.id.bounceBallButton);
final ImageView bounceBallImage = (ImageView)
findViewById(R.id.bounceBallImage);
bounceBallButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bounceBallImage.clearAnimation();
TranslateAnimation transAnim = new TranslateAnimation(0, 0, 0,
getDisplayHeight()/2);
transAnim.setStartOffset(1000);
transAnim.setDuration(5000);
transAnim.setFillAfter(true);
transAnim.setInterpolator(new BounceInterpolator());
transAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i(TAG, "Starting button");
@Override
public void onAnimationRepeat(Animation animation) {
@Override
public void onAnimationEnd(Animation animation) {
Log.i(TAG,"Ending button");
bounceBallImage.clearAnimation();
final int left = bounceBallImage.getLeft();
final int top = bounceBallImage.getTop();
final int right = bounceBallImage.getRight();
final int bottom = bounceBallImage.getBottom();
bounceBallImage.layout(left, top, right, bottom);
}
});
bounceBallImage.startAnimation(transAnim);
}
});