Zhao Yazhi _android_frame Animation

Source: Internet
Author: User

Before starting an instance, refer to a paragraph in the official documentation:

Frame animation is a series of pictures in a certain order to show the process, and the mechanism of the film is very similar to, we call frame-wise animation. Frame animations can be defined in an XML file or fully coded to be implemented.

If it is defined in an XML file, we can place it in the Anim or drawable directory under/res (/res/[anim | drawable]/filename.xml), the file name can be referenced in the code as a resource ID, and if it is implemented entirely by encoding, We need to use the Animationdrawable object.

If the animation is defined in an XML file, the syntax is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <animation-list  xmlns:android= "http://schemas.android.com/apk /res/android "      android:oneshot=[" true "| "False" >      <item          android:drawable= "@[package:]drawable/drawable_resource_name"          android: duration= "integer"/>  </animation-list>  

    1. Configure Picture resource Files
    2. Implementing frame animations in activity
Example: Running effect:

Steps to put the picture in the Res/drawable directory, respectively named: A1.png,a2.png,a3.png,a4.png,a5.png,a6.png.
Create an XML configuration file under the Res/anim directorywe can put the Frame.xml file in the drawable or Anim directory, the official document is placed in the drawable, people can be placed according to preferences, placed in both directories can be run.
<?xml version= "1.0" encoding= "Utf-8"?>    <!--       root tag animation-list    which oneshot represents whether to show only once, Set to False to cycle through the animation      root tab, declare each picture in the animation by the item tag    android:duration indicates the length of time that the picture was used--  < Animation-list xmlns:android= "Http://schemas.android.com/apk/res/android" android:oneshot= "false" >      < Item android:drawable= "@drawable/a1" android:duration= "></item>      <item android:drawable=" @ Drawable/a2 "android:duration=" ></item>      <item android:drawable= "@drawable/a3" Android: duration= "></item>      <item android:drawable=" @drawable/a4 "android:duration=" ></" item>      <item android:drawable= "@drawable/a5" android:duration= "></item> <item"      android:drawable= "@drawable/a6" android:duration= "></item>  </animation-list>   

    • Animation-list: The total label of the animation, which contains frame animations <item> tags
      • OneShot represents whether to show it only once.
        • True indicates that the animation is advertised only once
        • False will not stop looping the animation
    • Item: Record information for each frame and declare each picture in the animation
      • Android:drawable= "@drawable/A" means that the picture used for this frame is "a", and so on.
      • The android:duration= "1000" indicates that the frame lasts 1000 milliseconds and can be adjusted to the speed of the animation.
Create the layout configuration file under the Res/layout directory Activity_main.xml
<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 ">    <ImageView        android:id=" @+id/imageview1 "        android:layout_width= "wrap_content"         android:layout_height= "Wrap_content"         android:layout_centerhorizontal= "true"/>    <Button      & nbsp Android:id= "@+id/btn_begin"         android:layout_width= "wrap_content"     &NBSP   android:layout_height= "wrap_content"         android:layout_below= "@+id/imageview1"         android:layout_margintop= "30DP"         android:layout_torightof= "@+id/btn_ Codebegin "        android:onclick=" click "        android:text=" Start "/>  & nbsp <Button        android:id= "@+id/btn_stop"         android:layout_width= "wrap _content "        android:layout_height=" wrap_content "        Android:layout_ Alignbottom= "@+id/btn_begin"         android:layout_marginright= "15DP"         android:onclick= "click"         android:text= "Stop"/>    <RadioGroup    & nbsp   android:id= "@+id/rg_num"         android:layout_width= "wrap_content"         android:layout_height= "Wrap_content"   &nbsp     android:layout_below= "@+id/btn_stop"         android:orientation= "Horizontal" >         <RadioButton            android:id= "@+id/rb_one"   &nbsp ;         android:layout_width= "wrap_content"             Android:layout_ height= "Wrap_content"             android:checked= "true"           & nbsp Android:text= "single play"/>        <RadioButton            android:id= "@+id/rb_more"             android:layout_width= "wrap_content"         & nbsp   android:layout_height= "wrap_content"             android:text= "loop play"/>  & nbsp </RadioGroup>    <TextView        android:id= "@+id/textview1"         Android:laYout_width= "Wrap_content"         android:layout_height= "wrap_content"         android:layout_below= "@+id/rg_num"         android:text= "Drag the progress bar to modify the transparency (0-255) between"/>    & Lt seekbar        android:id= "@+id/sb_alpha"         android:layout_width= "Match_ Parent "        android:layout_height=" wrap_content "        Android:layout_ below= "@+id/textview1"/>    <Button        android:id= "@+id/btn_codebegin"         android:layout_width= "wrap_content"         android:layout_height= "Wrap_ Content "        android:layout_alignbottom=" @+id/btn_stop "        Android: layout_marginright= "15DP"         android:layout_torightof= "@+id/btn_stop"         android:text= "Code _ Start"         android:onclick= "Click "/></relativelayout> 


Activity Code
Package Com.example.lession13_frame;import Android.app.activity;import Android.graphics.drawable.animationdrawable;import Android.graphics.drawable.drawable;import Android.os.Bundle; Import Android.view.view;import Android.widget.imageview;import Android.widget.radiogroup;import Android.widget.radiogroup.oncheckedchangelistener;import Android.widget.seekbar;import Android.widget.seekbar.onseekbarchangelistener;import Android.widget.toast;public class SplashActivity extends Activity {private ImageView imageview;private animationdrawable animationdrawable;private radiogroup rgnum;private SeekBar sbalpha; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Frame animation ImageView = (ImageView) Findviewbyid (r.id.imageview1); rgnum = ( Radiogroup) This.findviewbyid (r.id.rg_num); Sbalpha = (SeekBar) This.findviewbyid (R.id.sb_alpha);//animation implemented in the first way/* * Animationdrawable = (animationdrawable) * getresources (). Getdrawable (R.aniM.framebyframe); * Imageview.setbackgrounddrawable (animationdrawable); *///the second way to implement the animation//Set Background resource Imageview.setbackgroundresource (r.anim.framebyframe); animationdrawable = ( animationdrawable) Imageview.getbackground ();//Animationdrawable.setoneshot (FALSE); loop Play// Animationdrawable.stop (); Stop play//animationdrawable.isrunning ();//whether to play//Animationdrawable.getnumberofframes ();// Play frame//Animationdrawable.getframe (index); Returns the Drawable object that made the index//animationdrawable.getduration (i); the time of the Stay Rgnum.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (radiogroup group, int checkedid) {//TODO Auto-generated method stubif (Checkedid = = R.id.rb_one) {//Set one-time playback animationdrawable.setoneshot (true);} else if ( Checkedid = = R.id.rb_more) {//Set loop playback Animationdrawable.setoneshot (false);} Set Playback to restart Animationdrawable.stop (); Animationdrawable.start ();}); /monitor progress bar Modify Transparency Sbalpha.setonseekbarchangelistener (new Onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch (SeekBar sEekbar) {} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {} @Overridepublic void onprogresschanged (SeekBar SeekBar, int Progress,boolean fromuser) {//TODO auto-generated method stub//Set Animation alpha value Animationdrawable.setalpha ( progress);//Notify ImageView to refresh the screen imageview.postinvalidate ();});} public void Click (View v) {int id = v.getid (), switch (ID) {case r.id.btn_begin:animationdrawable.start (); Break;case r.id. Btn_stop:animationDrawable.stop (); Break;case R.id.btn_codebegin:toast.maketext (Getapplicationcontext (), "------- -----------", 0). Show ();//full-code implementation of the animated effect for (int i = 1; I <= 6; i++) {//Get the corresponding resource in R.java according to the resource name and directory Idint picid = getresource S (). Getidentifier ("a" + I, "drawable", Getpackagename ());//Gets the Drawable object drawable drawable = Getresources () based on the resource ID. Getdrawable (PICID);//Add this frame to animationdrawable animationdrawable.addframe (drawable, 300);} Animationdrawable.setoneshot (FALSE); Set to Loopimageview.setbackgrounddrawable (animationdrawable); Set the animation to ImageView background animationdrawable.start (); //Start animation Break;default:break;}}} 



Animationdrawable is used to control this frame animation, which provides a number of methods.

    • Animationdrawable.start (); Start this animation
    • Animationdrawable.stop (); End this animation
    • Animationdrawable.setalpha (100); Set the animation's transparency, value range (0-255)
    • Animationdrawable.setoneshot (TRUE); Set up a single play
    • Animationdrawable.setoneshot (FALSE); Set Loop playback
    • Animationdrawable.isrunning (); Determine if the animation is playing
    • Animationdrawable.getnumberofframes (); Gets the number of frames for the animation.
When you drag the progress bar to set the alpha value, you must use Imageview.postinvalidate (); method to tell the UI thread to redraw the ImageView on the screen otherwise it will not see a transparent effect.

Pure Code Implementation:
The full-code implementation of the animated effect for          (int i = 1; I <= 6; i++) {              //based on the resource name and directory gets the corresponding resource ID in r.java              int picid = Getresources (). Getiden Tifier ("A" + I, "drawable", Getpackagename ());              Gets the Drawable object              drawable drawable = Getresources () based on the resource ID. getdrawable (picid);              Add this frame to animationdrawable              animationdrawable.addframe (drawable);          }          Animationdrawable.setoneshot (FALSE); Set to Loop          imageview.setbackgrounddrawable (animationdrawable);  Set the animation to ImageView background          animationdrawable.start ();   Start animation break     ;



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.