Custom toast, fall from the top, and then bounce

Source: Internet
Author: User

First, the custom class gift, if you feel useful, please praise!

ImportJava.util.Timer;ImportJava.util.TimerTask;ImportCOM.EXAMPLE.VIEWPAGEINDICATOR.R;ImportAndroid.content.Context;ImportAndroid.graphics.PixelFormat;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.WindowManager;ImportAndroid.view.WindowManager.LayoutParams;ImportAndroid.widget.Toast; Public  class mytoast {    PrivateLayoutparams Mparams;Private BooleanMshowtime;Private BooleanMisshow;PrivateWindowManager MWDM;PrivateView Mtoastview;PrivateTimer Mtimer;Private Mytoast(Context context, String text,BooleanShowTime) {mshowtime = ShowTime;//Record the length of toast display typeMisshow =false;//Record 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 through a toast instanceMtoastview = Toast.maketext (context, text, Toast.length_short). GetView (); Mtimer =NewTimer ();//Set layout parametersSetParams (); } Public StaticMytoastMaketext(Context context, String text,BooleanShowTime) {Mytoast result =NewMytoast (context, text, showTime);returnResult }Private void SetParams() {//TODO auto-generated method stubMparams =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|          Gravity.bottom; Mparams.y = $; } Public void Show(){if(!misshow) {//If toast is not displayed, start loading the displayMisshow =true; Mwdm.addview (Mtoastview, mparams);//load it onto the WindowManagerMtimer.schedule (NewTimerTask () {@Override               Public void Run() {Mwdm.removeview (Mtoastview); Misshow =false; }            }, (Long) (Mshowtime?3500: -)); }        }}

Animated XML Anim_view

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="anim_view">        <item name="@android:windowEnterAnimation">@anim/anim_in</item>        <item name="@android:windowExitAnimation">@anim/anim_out</item>    </style></resources>

anim_in Animation XML

<?xml version= "1.0" encoding= "Utf-8"?><set xmlns:android="Http://schemas.android.com/apk/res/android">  <translateandroid:fromxdelta="0"android:fromydelta="0" Android:toxdelta="0"android:toydelta="android:duration" ="/>"                       <translate  android: Fromxdelta  = "0"  android:fromydelta  =< Span class= "Hljs-value" > "0"  android:toxdelta  =     "0"  android:toydelta  = " -200"  android:duration  ="  class= Hljs-attribute Android:fillafter  = "true"  android: Interpolator  = "@android: Anim/decelerate_interpolator" />     <translate  android: Fromxdelta  = "0"  android:fromydelta  =< Span class= "Hljs-value" > "0"  android:toxdelta  =     "0"  android:toydelta  = "$"  android:duration  =/>       <translate  android: Fromxdelta  = "0"  android:fromydelta  =< Span class= "Hljs-value" > "0"  android:toxdelta  =     "0"  android:toydelta  = " -100"  android:duration  ="  class= Hljs-attribute Android:fillafter  = "true"  android: Interpolator  = "@android: Anim/decelerate_interpolator" />   <Alphaandroid:fromalpha="0"android:toalpha= "1"android: Duration="/>"              </Set>

Anim_out Animation XML

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <alpha         android:fromAlpha="1"        android:toAlpha="0"        android:duration="800"/></set>

Fade out the interface!

Call method The third argument is true and false true if 3500 milliseconds false is 2000 milliseconds

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

Custom toast, fall from the top, and then bounce

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.