Animation App
1. Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Animation"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2. Xml
i. Main Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtAnim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WSCube Tech"
android:textSize="34sp"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textColor="#4CAF50"/>
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sadam Hussain Mahar"
android:textSize="34sp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textColor="#4CAF50"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnTranslate"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate"
android:paddingLeft="3dp"
android:paddingRight="6dp"
android:backgroundTint="#4CAF50"/>
<Button
android:id="@+id/btnAlpha"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alpha"
android:paddingLeft="6dp"
android:paddingRight="3dp"
android:backgroundTint="#4CAF50"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnRotate"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:backgroundTint="#4CAF50"/>
<Button
android:id="@+id/btnScale"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:backgroundTint="#4CAF50"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ii. Translate (Move):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator ="@android:anim/bounce_interpolator">
<translate android:fromXDelta="0"
android:toXDelta="400"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"/>
</set>
iii. Alpha
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration = "4000"
android:repeatCount = "infinite"/>
</set>
iv. Rotate
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="3600"
android:duration="12000"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
v. Scale
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1"
android:toXScale="4"
android:fromYScale="1"
android:toYScale="4"
android:pivotY="50%"
android:pivotX="50%"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"/>
</set>
3. Java
package com.example.animation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView txtAnim;
Button btnTranslate, btnAlpha, btnRotate, btnScale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtAnim = findViewById(R.id.txtAnim);
btnTranslate = findViewById(R.id.btnTranslate);
btnAlpha = findViewById(R.id.btnAlpha);
btnRotate = findViewById(R.id.btnRotate);
btnScale = findViewById(R.id.btnScale);
//1. Translate Animation
btnTranslate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation move = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
txtAnim.startAnimation(move);
}
});
//2. Alpha Animation
btnAlpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation alpha = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
txtAnim.startAnimation(alpha);
}
});
//3. Rotate Animation
btnRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
txtAnim.startAnimation(rotate);
}
});
//4. Scale Animation
btnScale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation scale = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
txtAnim.startAnimation(scale);
}
});
}
}