Use of frameanimation animations in Android

Source: Internet
Author: User

frame Animation, which is a pre-made image in sequential play, is like a movie , The Android SDK provides another class animationdrawable to define the use of frame Animation.

Let's take a look at how to use frame animation.

First create a new Frame.xml file under the drawable directory:

<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item        android:drawable=" @drawable/A "        android:duration=" >    </item>    <item        android:drawable= "@drawable/b"        android:duration= ">"    </item>    < Item        android:drawable= "@drawable/C" android:duration= "$"        >    </item></animation-list >

The above XML file sets the animation changes during the various pictures and the duration of the time below to see how the Java code file implementation of this animation:
public void Frameanimation () throws FileNotFoundException {TextView Mtextview = (TextView) Findviewbyid (r.id.tv); Mtextview.setbackgroundresource (r.drawable.frame);//Set the Frame animation resource set in XML to the control animationdrawable animdrawable = ( animationdrawable) Mtextview.getbackground ();//Gets the background of the control drawable object Bitmap bitmap = Bitmapfactory.decodestream (new FileInputStream (Environment.getexternalstoragedirectory () + "/download/aaa.jpg"));//Add a background map dynamically in your code, You can also dynamically add animdrawable.addframe (new bitmapdrawable (null, bitmap), 2000) from the input stream fetched by the server,//Set duration Animdrawable.setalpha (180 )///Set the transparency of the picture Animdrawable.setoneshot (false);//Set whether to run only once, set to True for Loop run Animdrawable.start ();}

This is also clear in the comments, you can add images through the XML file can also be added dynamically through the code. Put a running diagram on it:


Finally summarize the steps to use Frameanimation: 1, in the XML file is defined to display the picture 2, in the Java file to get the background of the control Animationdrawable object 3, Call the Start () method of the Animationdrawable object to turn on this frame animation



Use of frameanimation animations in Android

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.