Android uses frame animations to implement custom loading loading layouts

Source: Internet
Author: User


In the project development process, we always need to customize some of the project style similar to the loading page, when we can consider using frame animation to complete this function

If we want to implement the frame animation loading effect as shown:

We can select three frames of picture:

How to use it in frame animation?

An animationdrawable defined in XML consists of a single <animation-list> element, and a series of nested <item> tags. Each item defines a frame of the animation. See the example below.

The code for our res/drawable/loading_animation is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/selected "    android:oneshot=" false ">    <item        android:drawable=" @ DRAWABLE/LOADING_BIG1 "        android:duration=" "/>    <item        android:drawable=" @drawable/loading_ Big2 "        android:duration="/>    <item        android:drawable= "@drawable/loading_big3"        android: duration= "/></animation-list>"
There's not much to explain here, and then we'll look at our custom durianloading.

public class Durianloading extends Relativelayout {private Animationdrawable frameanimation;//object for creating frame animations private Imag    Eview loadingimg;//loading picture private TextView textview;//loaded word private Context mcontext;        Public durianloading (Context context) {super (context);    Initview (context);        } public durianloading (context context, AttributeSet Attrs) {Super (context, attrs);    Initview (context);        }//Stop animation private void Stoploadinganimation () {setvisibility (view.gone);            if (frameanimation! = null) {if (frameanimation.isrunning ()) {frameanimation.stop ();  }}}/** * pictures and text in layouts are handled dynamically by loading * This adds a picture and a textview */private void Initview (Context        Context) {Mcontext = context; Layoutparams IMGLP = new Layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONT        ENT); Imglp.addrule (relativelayout.center_horizontal);/Set picture horizontally centered loadingimg = new ImageView (context);        Loadingimg.setid (Integer.parseint ("1"));//set a coordinate for the loadingimg, which can be used as the reference position of the following space Loadingimg.setlayoutparams (IMGLP);        AddView (LOADINGIMG); Layoutparams TEXTLP = new Layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CON        TENT);        Textlp.addrule (relativelayout.center_in_parent);        Textlp.addrule (Relativelayout.below, Loadingimg.getid ());        TextView = new TextView (context);        Setloadui (NULL);        Textview.setlayoutparams (TEXTLP);    AddView (TextView);        }/** * Set load text */private void Setloadui (String loadtext) {textview.settextcolor (color.black);        Textview.settextsize (16);        if (LoadText = = null) {Showloadui (false, 0);        } else {Showreloadui ();     }}/** * Show load Animation * * @param isstopanim need to stop animation * @param loadtextstringid loaded text, if not custom loaded text, this value is 0 */Public VOID Showloadui (boolean isstopanim, int loadtextstringid) {if (Isstopanim) {stoploadinganimation ();            } else {setvisibility (view.visible); if (Loadtextstringid! = 0) {textview.settext ("Load data in ...            0.0 ");            } else {Textview.settext ("Load data ...");            }//Set the image to load animation Loadingimg.setbackgroundresource (r.drawable.loading_animation);            Frameanimation = (animationdrawable) loadingimg.getbackground ();        Start animation Frameanimation.start ();        }}/** * Display reload Page * Purpose: When loading data fails to show, we set a click Response event to the current layout, continue to load data */public void Showreloadui () {        Setvisibility (view.visible);        Textview.settext ("Reload");    Loadingimg.setbackgroundresource (R.drawable.ic_launcher); /** * Release Resource * call */public void release () {if (frameanimation! = NULL in Destroy or Destroy view)         && frameanimation.isrunning ()) {   Frameanimation.stop ();        } frameanimation = null;        if (loadingimg! = null) {loadingimg.setimagedrawable (null); }    }}
Then we can use it in the layout file that we need.

<com.storm.durian.view.durianloading        android:id= "@+id/tel_login_loading_img"        android:layout_width= " Wrap_content "        android:layout_height=" wrap_content "        android:layout_centerinparent=" true "        Android: visibility= "Gone"/>

Then just:

Call Showloadui (False, 0) where it needs to be shown; Note: Loading text here can be customized

Call Showloadui (True, 0) where it needs to be canceled;

Call Showreloadui () in places where overloading is required;

Call Release () where the resource needs to be freed;


It's quite simple! Hope to help everyone!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses frame animations to implement custom loading loading layouts

Related Article

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.