Objective
In Android, there are two ways to implement animation Animation: Tween Animation (motion Tween) and frame Animation (frame animation). Gradient animation is the animation effect of the object in the scene by constantly making the image transform (panning, zooming, rotating, etc.). Frame animations are animated by sequentially playing an image prepared in advance, similar to a movie.
Let's learn the basics of frame-by-step animation in Android.
principle: "visual temporary retention" of human eyes
Way:
1. In Java code( new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200);)
The SDK seems to require that the minimum version must be >=21
2. Define an animation resource in an XML file
The effect chart is as follows
Code
1. Prepare picture Resources
Put the picture resource in the drawable-hdpi directory
2. Create a new animation-list type file in the drawable directory
Anim_frame.xml
<?xml version= "1.0" encoding= "Utf-8"?> <animation-list xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:oneshot=" false ">
<item android:drawable=" @drawable/A "android:duration=" 100 " />
<item android:drawable= "@drawable/b" android:duration= "/> <item
" @ drawable/c "android:duration="/>
</animation-list>
3. In the layout file, add ImageView
, set its background
properties to animation resource XML
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:orientation=" vertical "
android:layout_width=" match_parent "
android:layout" _height= "Match_parent" >
<button
android:id= "@+id/btn_start"
android:text= "Start dancing"
Android: Textsize= "25SP"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"/>
<button
android:id= "@+id/btn_stop"
android:text= "End Dancing"
android:textsize= "25SP"
android: Layout_width= "Wrap_content"
android:layout_height= "wrap_content"/> <imageview android:id=
"@+id/image"
android:background= "@drawable/anim_frame"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
</LinearLayout>
4. In Java, get animation resources, invoke animation start( )
, stop( )
stop animation
Package Com.lyp.frameanim;
Import android.graphics.drawable.AnimationDrawable;
Import android.graphics.drawable.Drawable;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.ImageView;
public class Mainactivity extends appcompatactivity implements view.onclicklistener{private Button Mbtnstart;
Private Button mbtnstop;
Private ImageView Mimage;
Private Animationdrawable Manim;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
New Animationdrawable (). Addframe (Getdrawable (R.DRAWABLE.A), 200);
Initview ();
Manim = (animationdrawable) mimage.getbackground ();
private void Initview () {mbtnstart= (Button) Findviewbyid (R.id.btn_start);
mbtnstop= (Button) Findviewbyid (r.id.btn_stop);
Mimage= (ImageView) Findviewbyid (r.id.image);
Mbtnstart.setonclicklistener (this); Mbtnstop.setoncLicklistener (this);
@Override public void OnClick (View v) {switch (V.getid ()) {case R.id.btn_start:manim.start ();
Break
Case R.id.btn_stop:manim.stop ();
Break }
}
}
Summarize
This is the full content of this article, I hope you can help Android developers, if you have questions you can leave a message exchange.