0% found this document useful (0 votes)
30 views4 pages

Android Bounce and Fade Animations

The document contains code for an Android activity layout and Java code to control animations on the layout. The layout contains buttons and image views to control animations on a ball image. The Java code defines click listeners for the buttons to start different animations on the ball image like bounce, fade in, and fade out using techniques like TranslateAnimation and ObjectAnimator.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Android Bounce and Fade Animations

The document contains code for an Android activity layout and Java code to control animations on the layout. The layout contains buttons and image views to control animations on a ball image. The Java code defines click listeners for the buttons to start different animations on the ball image like bounce, fade in, and fade out using techniques like TranslateAnimation and ObjectAnimator.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Activity_main.

xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<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="[Link]
android:shape="oval" >

<solid android:color="#8c0000" />

<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="[Link]
android:shape="rectangle" >

<solid android:color="#8c00" />

<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="[Link]
android:shape="rectangle" >

<solid android:color="#8c00" />

<stroke
android:width="1dp"
android:color="#fff" />

<size
android:height="30dp"
android:width="80dp" />

</shape>

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends Activity {

private static final String TAG = "AnimationStarter";

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final ImageView imageView = findViewById([Link]);
Button fadeIn = findViewById([Link]);
[Link](new [Link]() {

@Override
public void onClick(View v) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
[Link](1000);
[Link](1);
[Link]([Link]);
[Link](alphaAnimation);
}
});
Button fadeOut = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
ObjectAnimator fadeOut = [Link](imageView,
"alpha", 1f, 0);
[Link](2000);
[Link]();
}
});
Button bounceBallButton = (Button) findViewById([Link]);
final ImageView bounceBallImage = (ImageView)
findViewById([Link]);

[Link](new OnClickListener() {

@Override
public void onClick(View v) {
[Link]();
TranslateAnimation transAnim = new TranslateAnimation(0, 0, 0,
getDisplayHeight()/2);
[Link](1000);
[Link](5000);
[Link](true);
[Link](new BounceInterpolator());
[Link](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");
[Link]();
final int left = [Link]();
final int top = [Link]();
final int right = [Link]();
final int bottom = [Link]();
[Link](left, top, right, bottom);

}
});
[Link](transAnim);
}
});

private int getDisplayHeight() {


return [Link]().getDisplayMetrics().heightPixels;
}
}

You might also like