Android Animation : Rotating View Animation
This is second tutorial of animation. In this tutorial we will learn about android view animation. We will create rotating view animation.
Anim Directory:
First of all create anim resource file as rotate.xml.(If you do not have anim resource than create it first).
If you don't know how to create a anim resource directory than watch this tutorial.
Codes:
1. rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatMode="restart"
android:repeatCount="infinite"/>
</set>
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView
android:id="@+id/rotate"
android:layout_width="200dp"
android:layout_height="100dp"
android:src="@drawable/logo"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/btn"
android:layout_width="200dp"
android:layout_height="80dp"
android:text="Rotate"
android:textSize="30dp"
android:background="@drawable/capsule"
android:textColor="@color/blue"
android:layout_marginBottom="20dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3. ActivityMain.java
package com.bharat_putra_tech.testapp;
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.ImageView;
public class MainActivity extends AppCompatActivity {
Animation rotate;
Button btn;
ImageView logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
logo = findViewById(R.id.rotate);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rotate = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate);
logo.setVisibility(View.VISIBLE);
logo.startAnimation(rotate);
}
});
}
}
Result :
Video Tutorial:
No comments:
Post a Comment