When the Android animation-like Meituan loads data and waits, the Progress animation dialog box (with SF courier running effect) and android progress

Source: Internet
Author: User

When the Android animation-like Meituan loads data and waits, the Progress animation dialog box (with SF courier running effect) and android progress
The first sentence is still the old one, you know! Finddreams :( http://blog.csdn.net/finddreams/article/details/43194799 ). We all know that there are two common Animation modes in Android: Frame Animation and Tween Animation ). Frame Animation provides a frame-by-frame method for playing images. It plays pre-prepared images. Similar to gif images, it is like playing a movie. The animation allows you to move, zoom in, zoom out, or gradient the View component. Today, we will mainly simulate the dialog box effect of the villain Running animation when loading data in the Meituan. An interesting name is Running Man. Run it, bro! Let's see if you want to achieve the effect first, and then decide whether to read it. As a programmer, our time is precious, after all, there are no girlfriends? (Ps: For technical reasons, the dynamic graph provided is not very effective, but it runs very well on the mobile phone. If you are interested, you can download the source code .)


The following describes the implementation principles. First, create an anim folder under the res directory of the project, create an xml file, and add the following code:

<?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/app_loading0"        android:duration="150"/>    <item        android:drawable="@drawable/app_loading1"        android:duration="150"/></animation-list>
Animation-list is an animation list. Many items are placed in the middle, that is, images that constitute frame animations. android: drawable [drawable] // loads the Drawable object android: duration [long] // duration of each animation frame (unit: ms)
Android: oneshot [boolean] // whether the animation runs only once, true runs once, and false repeats
After writing it, let's take a look at the custom dialog box to automatically load the running animation when opening the dialog box. See the Code:
/*** @ Description: Custom dialog box * @ author http://blog.csdn.net/finddreams */public class CustomProgressDialog extends ProgressDialog {private AnimationDrawable mAnimation; private Context mContext; private ImageView mImageView; private String mLoadingTip; private TextView mLoadingTv; private int count = 0; private String oldLoadingTip; private int mResid; public CustomProgressDialog (Context context, String content, int id) {super (context); this. mContext = context; this. mLoadingTip = content; this. mResid = id; setCanceledOnTouchOutside (true) ;}@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); initView (); initData ();} private void initData () {mImageView. setBackgroundResource (mResid); // obtain the AnimationDrawablemAnimation = (AnimationDrawable) mImageView displayed in the background through the ImageView object. getBackground (); // to prevent mImageView from displaying only the first frame in the onCreate method. post (new Runnable () {@ Overridepublic void run () {mAnimation. start () ;}}); mLoadingTv. setText (mLoadingTip);} public void setContent (String str) {mLoadingTv. setText (str);} private void initView () {setContentView (R. layout. progress_dialog); mLoadingTv = (TextView) findViewById (R. id. loadingTv); mImageView = (ImageView) findViewById (R. id. loadingIv );}}

We can see that in the code, we use an imageview. post (Runnable r) method, because Frame Animation requires continuous re-painting, so it must be run in the thread; otherwise, only the first frame of the effect can be seen, which is the same as the principle of playing the game, as a person moves around, there is a thread controlling the continuous re-painting of images. Of course, there is another way to achieve it:
@Overridepublic void onWindowFocusChanged(boolean hasFocus) {// TODO Auto-generated method stubmAnimation.start(); super.onWindowFocusChanged(hasFocus);}
Finally, it is called in Activity. Details:
CustomProgressDialog dialog = new CustomProgressDialog (this, "loading", R. anim. frame); dialog. show ();

The custom dialog box class CustomProgressDialog is well encapsulated and convenient to call. You can quickly replace it with the desired effect, just change the image.
The final attachment source code, but not enough understanding of friends can download to see, hope to help you; http://download.csdn.net/detail/finddreams/8401429

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.