Android App series: Imitation MIUI toast animation effect implementation (there is the source map)

Source: Internet
Author: User
Tags translate function

Objective

Believe that some people have used MIUI, will find millet toast and Android traditional toast special is not the same, he will fly from the bottom up, and then fade away. It looks pretty good, but custom animations are not supported for Android native toast. So how did this effect come true? Here's to tell you ....

Analysis

If the park friends have read my other blog "Android: Profiling source, the control of the toast display", you will know that the native toast is infate out a view instance, and then load it onto the WindowManager to achieve the display effect. Many of us know that WindowManager is able to implement a view that hovers over all application interfaces without the focus, which is the core function that toast needs to have: the simplicity of the message is passed to the user without additional action ; Our effect is also based on WindowManager.

Body

We need to customize a toast class, but we don't need to inherit the toast. Now that you're copying a custom toast, we'll start with the toast entry to refine this custom toast.

To create a new class

 Public class Miuitoast {}

You don't have to inherit other classes (except for object ...). )

Start code

We generally use native Toast as direct toast.maketext (this, text, Toast.length_short). Show (); Where maketext This static method returns a toast instance and then calls the Show method to display the toast.

We'll take care of the Maketext method.

 Public Static Boolean showTime) {    new  miuitoast (context, text, showTime);     return result;}

Logic is simply rude, calling the constructor directly to instantiate a Miuitoast object and returning.

The next step is to construct the Miuitoast method.

PrivateMiuitoast (Context context, String text,BooleanshowTime) {Mshowtime= ShowTime;//record the length of toast display typeMisshow =false;//records whether the contents of the current toast are already displayedMWDM =(WindowManager) Context.getsystemservice (Context.window_service); //get the view layout of the default toast for the current Android system with a toast instanceMtoastview =Toast.maketext (context, text, Toast.length_short). GetView (); Mtimer=NewTimer (); //Setting Layout Parameterssetparams ();}

In the construction method, more is the initialization of the data, because the set layout parameters are more, so separate a function to

Look at the SetParams () method

Private voidSetParams () {Mparams=NewWindowmanager.layoutparams (); Mparams.height=WindowManager.LayoutParams.WRAP_CONTENT; Mparams.width=WindowManager.LayoutParams.WRAP_CONTENT; Mparams.format=pixelformat.translucent; Mparams.windowanimations= R.style.anim_view;//Set entry exit animation effectMparams.type =WindowManager.LayoutParams.TYPE_TOAST; Mparams.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; Mparams.gravity=Gravity.center_horizontal; Mparams.y= 250;}

This setting parameter is more of a reference to the type of setting parameters of the native toast in the source code, where we need to be aware that mparams.windowanimations = r.style.anim_view;//setting into the exit animation effect, This is the cornerstone of our MIUI toast animation. Other parameters are nothing to say, Google is so set their toast ha, next we have to look at the animation configuration XML.

What about this r.style.anim_view situation?

<Resources>    <stylename= "Anim_view">        <Itemname= "@android: Windowenteranimation">@anim/anim_in</Item>        <Itemname= "@android: Windowexitanimation">@anim/anim_out</Item>    </style></Resources>

This defines a reference to the animation and exit animation, and the next step is the moment we set the animation effect!

Anim_in:toast's entry animation

<Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <TranslateAndroid:fromxdelta= "0"Android:fromydelta= "0"Android:toxdelta= "0"Android:toydelta= "$"android:duration= "1"        />    <TranslateAndroid:fromxdelta= "0"Android:fromydelta= "0"Android:toxdelta= "0"Android:toydelta= " -105"android:duration= " the"Android:fillafter= "true"Android:interpolator= "@android: Anim/decelerate_interpolator"        />    <AlphaAndroid:fromalpha= "0"Android:toalpha= "1"android:duration= "+"        />    <TranslateAndroid:fromxdelta= "0"Android:fromydelta= "0"Android:toxdelta= "0"Android:toydelta= " the"android:duration= "a"Android:fillafter= "true"Android:startoffset= " the"        /></Set>

Here I have configured four animation effects, if you want to re-set the new animation effect, you can change in this area, ha. Roughly speaking, since before and after loading the animation, WindowManager through Mparams to determine the display position of the toast, so the first translate function is to let the toast at the beginning of loading to jump to the location, The other animation is to finish the toast from the bottom of the effect of flying, the period of time using the Android:startoffset control to achieve the effect of animation cohesion.

Anim_out:toast exiting an animation

<xmlns:android= "http://schemas.android.com/apk/res/android">    <Alpha         android:fromalpha= "1"        android:toalpha = "0"         android:duration= "/>"</set> 

A simple fade-out animation ....

Then let's take a look at the show () method.

 Public voidShow () {if(!misshow) {//If the toast does not appear, start loading the displayMisshow =true; Mwdm.addview (Mtoastview, mparams);//load it onto the WindowManagerMtimer.schedule (NewTimerTask () {@Override Public voidrun () {Mwdm.removeview (Mtoastview); Misshow=false; }        }, (Long) (Mshowtime? 3,500:2000)); }}

In the Show method we will judge Misshow whether the current toast is already displayed, and if it is showing we have no reason to believe that we would be so sb. To show him again.

Mwdm.addview (Mtoastview, mparams); Loads the view onto the WindowManager, achieves a similar suspension, starts the timer and removes it after a specified time, and the whole logic is like this.

Because--toast! It's so simple!

Give us a peek.

SOURCE please poke here (point me)

Enjoy wind chimes
Source: http://www.cnblogs.com/net168/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original connection, or next time not to you reproduced.

Android App series: Imitation MIUI toast animation effect implementation (there is the source map)

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.