Use of Android development animations (i)
This blog is mainly about the use of the animation effects (animations) API in Android development, which mainly covers the implementation of the four effects of animations:
First, picture zoom effect
second, the picture moves the effect
third, picture rollover effect
Four, the progressive effect of the picture
let's look at the code:
First we look at Mainactivity.java:
Package com.example.animationtest;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.view.animation.AlphaAnimation;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationSet;
Import android.view.animation.RotateAnimation;
Import android.view.animation.ScaleAnimation;
Import android.view.animation.TranslateAnimation;
Import Android.widget.Button;
Import Android.widget.ImageView;
public class Mainactivity extends Activity {
Private ImageView ImageView = null;
Private Button Scalebutton = null;
Private Button Translatebutton = null;
Private Button Rotatebutton = null;
Private Button Alphabutton = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ImageView = (ImageView) Findviewbyid (r.id.myimage);
Scalebutton = (Button) Findviewbyid (R.id.scalebutton);
Translatebutton = (Button) Findviewbyid (R.id.translatebutton);
Rotatebutton = (Button) Findviewbyid (R.id.rotatebutton);
Alphabutton = (Button) Findviewbyid (R.id.alphabutton);
Scalebutton.setonclicklistener (New Setscalelistener ());
Translatebutton.setonclicklistener (New Settranslatelistener ());
Rotatebutton.setonclicklistener (New Setrotatelistener ());
Alphabutton.setonclicklistener (New Setalphalistener ());
}
Animation Zoom Effect Listener
Class Setscalelistener implements onclicklistener{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Create a Animationset object
Animationset animationset = new Animationset (true);
Create a Animation object (0.5,0.5)
Animation Animation = new Scaleanimation (1f, 0.5f, 1f, 0.5f,
Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
Set the time for the animation process
Animation.setduration (1000);
Set Animation start time offset
Animation.setstartoffset (1000);
Set the number of repeat plays
Animation.setrepeatcount (0);
State at the end
Animation.setfillafter (TRUE);
To add a animation object to a Animationset object
Animationset.addanimation (animation);
Imageview.startanimation (animation);
}
}
Animation Move Effect Listener
Class Settranslatelistener implements onclicklistener{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Create a Animationset object
Animationset animationset = new Animationset (true);
(0,0)--(+)
Animation Animation = new Translateanimation (animation.relative_to_self, 0f, animation.relative_to_self, 1f,
Animation.relative_to_self, 0f, animation.relative_to_self, 1f);
Animation.setduration (1000);
Animationset.addanimation (animation);
Imageview.startanimation (animation);
}
}
Rotate animation effect Listener
Class Setrotatelistener implements onclicklistener{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Create a Animationset object
Animationset animationset = new Animationset (true);
360 rotation, relative_to_parent centered on the parent control (0,1)
Relative_to_self itself
Animation Animation = new Rotateanimation (0f, 360f,
Animation.relative_to_self, 0f, animation.relative_to_self, 1f);
Animation.setduration (1000);
Animationset.addanimation (animation);
Imageview.startanimation (animation);
}
}
Fade-in fade-out animation effect listener
Class Setalphalistener implements onclicklistener{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Create a Animationset object
Animationset animationset = new Animationset (true);
1: Fully visible
0: Completely invisible
Animation Animation = new Alphaanimation (1, 0);
Animation.setduration (1000);
Animationset.addanimation (animation);
Imageview.startanimation (animation);
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
The
main layout file Main.xml is as follows:
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">
<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<linearlayout
Android:id= "@+id/imglayout"
android:layout_below= "@id/mytext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:padding= "55DP"
>
<imageview
Android:id= "@+id/myimage"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"
/>
</LinearLayout>
<button
Android:id= "@+id/scalebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/imglayout"
android:text= "@string/scale"
/>
<button
Android:id= "@+id/translatebutton"
android:layout_below= "@id/scalebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/translate"
/>
<button
Android:id= "@+id/rotatebutton"
android:layout_below= "@id/translatebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/rotate"
/>
<button
Android:id= "@+id/alphabutton"
android:layout_below= "@id/rotatebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/alpha"
/>
</RelativeLayout>
The
implementation results are as follows:
Click on each of the four buttons to see the image will show the corresponding four kinds of effects
Use of Android development animations (i)