0% found this document useful (0 votes)
75 views

Q.19 Solution

The document describes how to create an Android application with a bouncing ball animation. It includes Java code to define the activity class with methods for initializing views and handling button clicks. It also includes XML layout code to define the user interface with an image view for the ball and a button. Additionally, it provides XML code for a jump animation that scales the ball up from 0 to 1 on the y-axis over 1 second using a bounce interpolator. Pressing the button starts this animation to make the ball jump randomly on screen.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Q.19 Solution

The document describes how to create an Android application with a bouncing ball animation. It includes Java code to define the activity class with methods for initializing views and handling button clicks. It also includes XML layout code to define the user interface with an image view for the ball and a button. Additionally, it provides XML code for a jump animation that scales the ball up from 0 to 1 on the y-axis over 1 second using a bounce interpolator. Pressing the button starts this animation to make the ball jump randomly on screen.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

19.

Develop an android application to make an animation for bouncing ball application in which ball
should make jumping randomly after pressing ‘jump’ button.

JAVA code:
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


ImageView ball;
Button jump;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball=findViewById(R.id.ballImage);
left=findViewById(R.id.btn_left);
right=findViewById(R.id.btn_right);
jump=findViewById(R.id.btn_jump);

jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

ball.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.jump)
);
}
});
}
}

XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">

<ImageView
android:id="@+id/car"
android:layout_marginTop="250dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="100dp"
android:src="@drawable/ballImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_jump"
android:layout_width="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_height="wrap_content"
android:text="Jump"/>
</LinearLayout>

</LinearLayout>

XML code: jump animation


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">

<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />

</set>

You might also like