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

Q.4 Solution

The document describes how to develop an animation of a person in Android Studio that can move forward, backward, and jump when arrow keys are pressed. It includes Java code to load the animation resources and handle button click events to trigger the animations. It also includes XML code for the activity layout, and XML code for the three animations - a left animation, right animation, and jump animation. The animations use properties like translate and scale to move and resize the person image when the respective buttons are pressed.

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)
24 views

Q.4 Solution

The document describes how to develop an animation of a person in Android Studio that can move forward, backward, and jump when arrow keys are pressed. It includes Java code to load the animation resources and handle button click events to trigger the animations. It also includes XML code for the activity layout, and XML code for the three animations - a left animation, right animation, and jump animation. The animations use properties like translate and scale to move and resize the person image when the respective buttons are pressed.

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/ 3

4.

Develop an animation of a person in android studio which can move forward, backward and jump
after pressing respective arrow keys.

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 person;
Button left, right, jump;

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

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

person.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rightt
oleft)); } });
right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

person.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.leftto
right)); } });
jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

person.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/personImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_height="wrap_content"
android:text="Left"/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"/>
<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: Right animation


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="700" />
</set>

XML code: Left animation


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/> </set>

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