Android animation effect and android Animation
Animation Resources
I. Classification: (1). Summary: android 3.0 and earlier versions support two animation modes: tween animation and frame animation ), in android3.0, a new animation system is introduced: property animation ). These three animation modes are called view animation, drawable animation, and property animation in the SDK. (2) classification of animation resources:
2. Compensation Animation: View Animation is a series of View shape transformations, such as scaling the size, changing the transparency, changing the horizontal position, and changing the Rotation Position, the definition of an animation can be defined either in java code or in XML. We recommend that you use XML. Place the animation defined in XML in the/res/anim/folder. The root element of the XML file is <set>, and the secondary node can be <alpha>, <scale>, <translate>, <rotate>. (1) use xml resources to implement animation population:
publicclass MainActivity extends Activity {
private ImageView imageView_main;
private Animation animation = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}
publicvoid clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
break;
case R.id.button_main_translate:
animation = new TranslateAnimation(0, 150, 0, 0);
break;
case R.id.button_main_rotate:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
default:
break;
}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}
(2) Use java code to implement a complementary Animation:
publicclass MainActivity extends Activity {
private ImageView imageView_main;
private Animation animation = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}
publicvoid clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
break;
case R.id.button_main_translate:
animation = new TranslateAnimation(0, 150, 0, 0);
break;
case R.id.button_main_rotate:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
default:
break;
}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}
3. Frame Animation: Frame Animation (Animation Drawable object): A Frame Animation, like a GIF image, is displayed in sequence through a series of Drawable to simulate the Animation effect. You must use <animation-list> as the root element and <item> as the image to be rotated. The duration Attribute indicates the display time of each item. The XML file must be stored in the/res/anim/or/res/animator directory. (1) instance code:
I. res/anim/frame_animation.xml code:
<Animation-listxmlns: android =Http://schemas.android.com/apk/res/android"
Android: oneshot ="True">
<Itemandroid: drawable ="@ Drawable/anim1"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim2"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim3"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim4"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim5"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim6"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim7"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim8"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim9"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim10"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim11"Android: duration ="50"/>
<Itemandroid: drawable ="@ Drawable/anim12"Android: duration ="50"/>
</Animation-list>
Ii. MainActivity. java code:
Public ClassMainActivityExtendsActivity {
PrivateImageView imageView_main_show;
PrivateAnimationDrawable animationDrawable =Null;
@ Override
Protected VoidOnCreate (Bundle savedInstanceState ){
Super. OnCreate (savedInstanceState );
SetContentView (R. layout.Activity_main);
ImageView_main_show = (ImageView) findViewById (R. id.ImageView_main_show);
ImageView_main_show.setBackgroundResource (R. anim.Frame_animation);
AnimationDrawable = (AnimationDrawable) imageView_main_show.getBackground ();
}
Public VoidClickButton (View view ){
Switch(View. getId ()){
CaseR. id.Button_main_start:
If(! AnimationDrawable. isRunning ()){
// Whether a group of animations are played only once
AnimationDrawable. setOneShot (False);
AnimationDrawable. start ();
}
Break;
CaseR. id.Button_main_stop:
If(AnimationDrawable. isRunning ()){
AnimationDrawable. stop ();
}
Break;
}
}
@ Override
Public VoidOnWindowFocusChanged (BooleanHasFocus ){
Super. OnWindowFocusChanged (hasFocus );
If(! AnimationDrawable. isRunning ()){
AnimationDrawable. setOneShot (False);
AnimationDrawable. start ();
}
}
}
[Note:] as mentioned in the SDK, do not call start () in onCreate (), because AnimationDrawable is not completely associated with Window. If you want to start animation when the interface is displayed, you can call start () in onWindowFoucsChanged (). 4. Property Animation: (1) concept: Property animation, which was introduced in Android 3.0. The Property Animation parameter is used to change the attributes of an object through Animation. property Animation changes the actual attributes of an object. In View Animation (Tween Animation), it changes the rendering effect of the View, and the attributes of the View remain unchanged. For example, no matter how you scale the Button size, the position and size of the effective click area of the Button remain unchanged. In Property Animation, the actual attributes of the object are changed, for example, the scaling of the Button, the position and size of the Button, and the attribute values are changed. Property Animation can be applied not only to View, but also to any object. Property Animation only indicates a change in value within a period of time. When the value changes, you decide what to do. (2) common attributes: (3) related classes:
- In general, attribute animation is an animation execution class that sets the attributes, duration, start and end attribute values, and time difference of the animation operation object, then, the system dynamically changes the attributes of the Object Based on the set parameters.
(1) instance code:
I. res/anim/property_anim.xml code:
<Setxmlns: android =Http://schemas.android.com/apk/res/android"
Android: ordering ="Sequentially">
<ObjectAnimator
Android: duration ="4000"
Android: propertyName ="X"
Android: valueTo ="300"
Android: valueType ="IntType"/>
<ObjectAnimator
Android: duration ="4000"
Android: propertyName ="Y"
Android: valueTo ="400"
Android: valueType ="IntType"/>
<ObjectAnimator
Android: duration ="4000"
Android: propertyName ="X"
Android: valueTo ="0"
Android: valueType ="IntType"/>
<ObjectAnimator
Android: duration ="4000"
Android: propertyName ="Y"
Android: valueTo ="0"
Android: valueType ="IntType"/>
</Set>
Ii. MainActivity. java code:
PublicClassMainActivityExtendsActivity {
PrivateImageView imageView_main_obj;
PrivateMove move;
@ Override
PublicVoidOnCreate (Bundle savedInstanceState ){
Super. OnCreate (savedInstanceState );
SetContentView (R. layout.Activity_main);
ImageView_main_obj = (ImageView) findViewById (R. id.ImageView_main_obj);
Move =NewMove ();
ImageView_main_obj.setOnClickListener (NewOnClickListener (){
@ Override
PublicVoidOnClick (View v ){
SetTitle (move. getX () + ":" + move. getY ());
}
});
}
ClassMove {
PrivateIntY;
PrivateIntX;
PublicIntGetY (){
ReturnY;
}
PublicVoidSetY (IntY ){
This. Y = y;
ImageView_main_obj.layout (imageView_main_obj.getLeft (), y,
ImageView_main_obj.getRight (),
Y + imageView_main_obj.getMeasuredHeight ());
}
PublicIntGetX (){
ReturnX;
}
PublicVoidSetX (IntX ){
This. X = x;
ImageView_main_obj.layout (x, imageView_main_obj.getTop (), x
+ ImageView_main_obj.getMeasuredWidth (),
ImageView_main_obj.getBottom ());
}
}
PublicVoidClickButton (View view ){
// Load Property animation resources
AnimatorSet set = (AnimatorSet) AnimatorInflater.LoadAnimator(This,
R. animator.Property_anim);
// Set the object to be controlled
Set. setTarget (move );
// Start the animation
Set. start ();
}
}
[Note :]
<ObjectAnimator
Android: duration ="4000"
Android: propertyName ="X"
Android: valueTo ="300"
Android: valueType ="IntType"/>
[Note :]
- Android: ordering describes the execution sequence of a series of animation actions. There are two options: sequentially and together;
- ObjectAnimator is used to set the animation implementation object;
- Duration is the time from the start to the end of the animation;
- Android: repeatCount = "infinite" can be an integer or infinite
- Android: repeatMode = "restart" can be restart or reverse
- Android: valueFrom = "" integer | floating point | color