To implement view Animation in Android follow the steps mentioned below:-
1) Start a new project in android studio.
2) Now create a new Android resource Directory under res directory then choose interpolater option in directory type and name it anim .
3) Here is your MainActivity code
MainActivity.java
public class MainActivity extends Activity {
private Button mFade,mBlink,mMove,mSlide,mClockwise;
private ImageView mImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImage=(ImageView)findViewById(R.id.image123);
mFade=(Button)findViewById(R.id.Fade);
mClockwise=(Button)findViewById(R.id.Clockwise);
mSlide=(Button)findViewById(R.id.Slide);
mFade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//implementing fade animation with the help of fade.xml file
mImage.startAnimation(android.view.animation.AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade));
}
});
mClockwise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//implementing rotation animation with the help of rotate.xml file
mImage.startAnimation(android.view.animation.AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
}
});
mSlide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//implementing slide animation with the help of slide.xml file
mImage.startAnimation(android.view.animation.AnimationUtils.loadAnimation(MainActivity.this,R.anim.slide));
}
});
}
}
4) Create new animation resource files fade.xml, rotate.xml and slide.xml in anim directory.
fade.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000" >
</alpha>
<alpha
android:startOffset="2000"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="2000" >
</alpha>
</set>
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
</set>
slide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="2.0"
android:toYScale="2.0" />
</set>
5) Here is your activity_main.xml code
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:id="@+id/image123"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@android:drawable/btn_star_big_on"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clockwise"
android:id="@+id/Clockwise"
android:onClick="zoom"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fade"
android:id="@+id/Fade"
android:onClick="fade"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide"
android:onClick="slide"
android:id="@+id/Slide"
/>
</LinearLayout>
</LinearLayout>
0 Comment(s)