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

Relativelayout: Activity - Main - XML

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)
114 views4 pages

Relativelayout: Activity - Main - XML

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
You are on page 1/ 4

Activity_main.

xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="http://schemas.android.com/apk/res/android"
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="http://schemas.android.com/apk/res/android"
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="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

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

<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;

public class MainActivity extends Activity {

private static final String TAG = "AnimationStarter";

@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);
}
});

private int getDisplayHeight() {


return this.getResources().getDisplayMetrics().heightPixels;
}
}

You might also like