Android Basics Getting Started tutorial--8.4.1 Android Animation collection frame animation
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
Starting with this section we'll explore the animations in Android, after all, adding some animations to the app will make our app
Very cool, such as the simplest to close activity, of course, custom control animation is definitely necessary ~ and Android animation
Divided into three categories, frame -by-frames and motion tweens (Tween), as well as animation of properties introduced after Android 3.0
(property), and this section brings you the first animation--one basic use of frame-wise animation ~
1. Frame Animation Concepts and usage
Frame animation is very easy to understand, in fact, is simply by the n-static picture collection, and then we show through the control
These pictures, because the human eye "visual residue" causes, will let us create the animation "illusion", with the film principle!
and the implementation of frame animation in Android, generally we will use the previous explanation of a drawable:animationdrawable
Write the drawable first, then call Start () in the Code and stop () to start or stop playing the animation ~
Of course we can also create a frame-by-step animation in Java code, create a Animationdrawable object, and then call
Addframe (drawable frame,int duration) adds frames to the animation, then calls start () and stop () ~
Let's write two examples to understand the effect of the next frame animation and the familiar usage
2. Examples of use:
Example One: The simplest example:
Run :
Code Implementation :
It's very simple to write our animation file first, create a anim directory under Res, and then start our
Animation files:miao_gif.xml:
The android:oneshot here is to set the animation to play only once, true to play only once, and false to loop!
<?xml version= "1.0" encoding= "Utf-8"?><animation-list xmlns:android="Http://schemas.android.com/apk/res/android" android:oneshot="false"> <itemandroid:drawable="@mipmap/img_miao1"android:duration="80" /> <itemandroid:drawable= "@mipmap/img_miao2"android:duration=" "/>" <itemandroid:drawable="@mipmap/img_miao3"android:duration= "/>" <!--is limited to space, omit other item, fill it up- -...</animation-list>
The animation file has, followed by our layout file:activity_main.xml:
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" Android:orientation="vertical"> <button android:id = "@+id/btn_start" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "start" /> <button android:id = "@+id/btn_stop" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "stop" /> <ImageViewandroid:id="@+id/img_show"android:layout_width="120DP "android:layout_height=" 120DP "android:layout_gravity=" center " Android:background="@anim/miao_gif" /> </linearlayout>
Finally, here is our Mainactivity.java, where we control the start and pause of the animation:
Public class mainactivity extends appcompatactivity implements View . Onclicklistener { PrivateButton Btn_start;PrivateButton Btn_stop;PrivateImageView img_show;PrivateAnimationdrawable Anim;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Bindviews (); Anim = (animationdrawable) img_show.getbackground (); }Private void bindviews() {Btn_start = (Button) Findviewbyid (R.id.btn_start); Btn_stop = (Button) Findviewbyid (r.id.btn_stop); Img_show = (ImageView) Findviewbyid (r.id.img_show); Btn_start.setonclicklistener ( This); Btn_stop.setonclicklistener ( This); }@Override Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.btn_start:anim.start (); Break; CaseR.id.btn_stop:anim.stop (); Break; } }}
Yes, it's very simple, huh?
Example two: Play a frame animation at a specified place
Run :
Code Implementation :
is still first on our animation file:anim_zhuan.xml:
<animation-list xmlns:android="Http://schemas.android.com/apk/res/android" android:oneshot="true"> <itemandroid:drawable= "@mipmap/img_zhuan1"android:duration=" "/>" <itemandroid:drawable="@mipmap/img_zhuan2"android:duration ="/>" <itemandroid:drawable="@mipmap/img_zhuan3"android:duration="80" /> <!--is limited to space, omit other item, fill it up- -...</animation-list>
Then we'll write a custom ImageView:Frameview.java, where the currently playing frame is obtained by reflection,
Then if it is the last frame, then hide the control!
/** * Created by Jay on 2015/11/15 0015. * * Public class frameview extends ImageView { PrivateAnimationdrawable Anim; Public Frameview(Context context) {Super(context); } Public Frameview(context context, AttributeSet attrs) {Super(context, attrs); } Public Frameview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); } Public void Setanim(Animationdrawable anim) { This. Anim = Anim; } Public void setlocation(intTopintleft) { This. Setframe (Left,top,left + $, Top + $); }@Override protected void OnDraw(Canvas canvas) {Try{//Reflection calls the Mcurframe value in AnimationdrawableField field = Animationdrawable.class. Getdeclaredfield ("Mcurframe"); Field.setaccessible (true);intCurframe = Field.getint (anim);//Gets the current frame of the Anim animation if(Curframe = = Anim.getnumberofframes ()-1)//If the last frame has been reached{//Let the view hideSetvisibility (view.invisible); } }Catch(Exception e) {E.printstacktrace ();}Super. OnDraw (canvas); }}
And finally our Mainactivity.java, create a framelayout, add view, press the touch event
Event processing, displaying controls and opening animations ~
Public class mainactivity extends appcompatactivity { PrivateFrameview Fview;PrivateAnimationdrawable Anim =NULL;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Framelayout Fly =NewFramelayout ( This); Setcontentview (fly); Fview =NewFrameview ( This); Fview.setbackgroundresource (R.anim.anim_zhuan); Fview.setvisibility (view.invisible); Anim = (animationdrawable) fview.getbackground (); Fview.setanim (ANIM); Fly.addview (Fview); Fly.setontouchlistener (NewView.ontouchlistener () {@Override Public Boolean OnTouch(View V, motionevent event) {//Set animation effect when pressed if(event.getaction () = = Motionevent.action_down) {anim.stop ();floatx = Event.getx ();floaty = event.gety (); Fview.setlocation ((int) Y- +,(int) x- -);//view the location shownFview.setvisibility (view.visible); Anim.start ();//Turn on animation}return false; } }); }}
All right, it's just as simple, huh?
3. This section sample code and GIF frame extraction tool download
Animationdemo1.zip
Animationdemo2.zip
GIF Frame Extraction Tool
This section summarizes:
OK, this section introduces you to the simplest frame animations in Android Three animations, which are not much used in actual development.
But learn more things, later also a number of ideas ah ~ Good, this section on here, thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--8.4.1 Android Animation collection frame animation