Simple Android practice tutorial-56th guns (simulate the progress prompt box of the Meituan client), and android practice

Source: Internet
Author: User

Simple Android practice tutorial-56th guns (simulate the progress prompt box of the Meituan client), and android practice

Our friends who have used the Meituan client know that the loading wait prompt of Meituan is very interesting and is displayed as an animation. Next we will understand the principles behind it, and then implement your own waiting animation effect.

First, we prepare two images:


The two images look exactly the same? Careful friends will find that the only difference lies in the foot. OK. We will use the rotation of the two pictures to achieve the animation effect. Let's take a look at the code below:

1. animation file frame_meituan.xml:

<?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/progress_loading_image_01"        android:duration="150"/>    <item        android:drawable="@drawable/progress_loading_image_02"        android:duration="150"/></animation-list>

You can switch between images in 150 milliseconds to simulate the animation effect.

2. A simple custom control-MeituanProgressDialog. java:

Package com. finddreams. runningman; import android. app. progressDialog; import android. content. context; import android. graphics. drawable. animationDrawable; import android. OS. bundle; import android. widget. imageView; import android. widget. textView; import com. example. runningman. r;/*** @ Description: Custom dialog box * @ author http://blog.csdn.net/yayun0516 */public class MeituanProgressDialog 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; /***** @ param context * Context object * @ param content * display text message content * @ param id * animation id */public MeituanProgressDialog (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 (); mImageView. 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); // display the mLoadingTv = (TextView) findViewById (R. id. loadingTv); mImageView = (ImageView) findViewById (R. id. loadingIv );}}

The preceding prompt layout file progress_dialog.xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center" android: orientation = "vertical"> <ImageView android: id = "@ + id/loadingIv" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ anim/frame_meituan"/> <TextView android: id = "@ + id/loadingTv" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBottom = "@ + id/loadingIv" android: layout_centerHorizontal = "true" android: textSize = "20sp" android: text = "loading .. "/> </RelativeLayout>

Finally, call in Activity:

Package com. finddreams. runningman; import com. example. runningman. r; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. view. view;/*** @ Description: Specifies the animation progress bar dialog box of the runner, which can be used to load the data interface * @ author http://blog.csdn.net/yayun0516 */public class MeiTuanManActivity extends Activity {private MeituanProgressDialog dialog; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. meituan_progressdialog);}/*** show the progress dialog box * @ param v */public void showmeidialog (View v) {dialog = new MeituanProgressDialog (this, "loading ", r. anim. frame_meituan); dialog. show (); Handler handler = new Handler (); handler. postDelayed (new Runnable () {@ Overridepublic void run () {dialog. dismiss () ;}}, 3000); // call the dismiss method 3 seconds later to hide it ;}}

Finally, let our program run:


OK, it's running. If you want to join your project, you only need to prepare two images and replace them to simulate the animation.

Free source code download

Finally, let me know. Thank you!


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.