Use of Animations in android development (1)
Use of Animations in android development (1)This blog post focuses on the use of the animation effect (Animations) API in android development, and mainly on the implementation of four animation effects:First, the image scaling effectSecond, the image moving EffectThird, image flip EffectFourth, the progressive effect of the image
Let's look at the code below:
First, let's 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 scaling effect listener
Class setScaleListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
// Create an 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 animation process time
Animation. setDuration (1000 );
// Sets the animation start time offset.
Animation. setStartOffset (1000 );
// Set the number of replay times
Animation. setRepeatCount (0 );
// The Status at the end
Animation. setFillAfter (true );
// Add the Animation object to the AnimationSet object
AnimationSet. addAnimation (animation );
ImageView. startAnimation (animation );
}
}
// Animation movement effect listener
Class setTranslateListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
// (0, 0) --> (1, 1)
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 );
}
}
// Rotating the animation effect listener
Class setRotateListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
// 360 rotation. The RELATIVE_TO_PARENT is centered on the parent control ().
// RELATIVE_TO_SELF
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 );
}
}
// Gets the animation effect listener in and out.
Class setAlphaListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
// 1: completely 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:
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">
Android: id = "@ + id/myText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
Android: id = "@ + id/imgLayout"
Android: layout_below = "@ id/myText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: padding = "55dp"
>
Android: id = "@ + id/myImage"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/ic_launcher"
/>
Android: id = "@ + id/scaleButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/imgLayout"
Android: text = "@ string/scale"
/>
Android: id = "@ + id/translateButton"
Android: layout_below = "@ id/scaleButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/translate"
/>
Android: id = "@ + id/rotateButton"
Android: layout_below = "@ id/translateButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/rotate"
/>
Android: id = "@ + id/alphaButton"
Android: layout_below = "@ id/rotateButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/alpha"
/>
The implementation result is as follows:
Click the four buttons to view the four effects.