原理简介:
直接在利用Toast做动画比较难实现,本文主要是利用PopupWindow实现的一个Toast仿造效果。
AnimationToast代码:
- public class AnimationToast
- {
- static final String TAG = "AnimationToast";
- /**
- * Show the view or text notification for a short period of time. This time
- * could be user-definable. This is the default.
- * @see #setDuration
- */
- public static final int LENGTH_SHORT = 0;
- /**
- * Show the view or text notification for a long period of time. This time
- * could be user-definable.
- * @see #setDuration
- */
- public static final int LENGTH_LONG = 1;
- final Context mContext;
- int mDuration;
- PopupWindow mPopToast;
- View mParent;
- int width = 300;//toast初始宽度
- int height = 80;//toast初始高度
- public AnimationToast(Context context)
- {
- mContext = context;
- }
- public void show()
- {
- mPopToast.showAtLocation(mParent, Gravity.CENTER, 0, 0);
- //LONG→2000ms SHORT→1000ms
- long duration = mDuration== LENGTH_LONG ? 2000 : 1000;
- mParent.postDelayed(new Runnable()
- {
- @Override
- public void run()
- {
- cancel();
- }
- }, duration);
- }
- public void cancel()
- {
- mPopToast.dismiss();
- }
- /**
- * Set the view to show.
- * @see #getView
- */
- public void setView(View view)
- {
- mPopToast = new PopupWindow(view, width, height);
- mPopToast.setAnimationStyle(R.style.AnimationToast);
- mPopToast.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.toast_frame));
- mPopToast.setFocusable(false);
- mPopToast.setOutsideTouchable(true);
- }
- /**
- * Return the view.
- * @see #setView
- */
- public View getView()
- {
- return mPopToast.getContentView();
- }
- public void setParent(View parent)
- {
- mParent = parent;
- }
- public View getParent()
- {
- return mParent;
- }
- /**
- * Set how long to show the view for.
- * @see #LENGTH_SHORT
- * @see #LENGTH_LONG
- */
- public void setDuration(int duration)
- {
- mDuration = duration;
- }
- /**
- * Return the duration.
- * @see #setDuration
- */
- public int getDuration()
- {
- return mDuration;
- }
- public void setWidth(int w)
- {
- width = w;
- }
- public void setHeight(int h)
- {
- height =h;
- }
- /**
- * Make a standard toast that just contains a text view.
- *
- * @param context The context to use. Usually your {@link android.app.Application}
- * or {@link android.app.Activity} object.
- * @param text The text to show. Can be formatted text.
- * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
- * {@link #LENGTH_LONG}
- * @param parent AnimationToast use a PopupWindow, so need a parent.
- * suggestion → using activity.getWindow().getDecorView()
- *
- */
- public static AnimationToast makeText(Context context, CharSequence text, int duration, View parent)
- {
- AnimationToast result = new AnimationToast(context);
- TextView tv = new TextView(context);
- tv.setTextColor(Color.WHITE);
- tv.setGravity(Gravity.CENTER);
- tv.setText(text);
- result.setView(tv);
- result.setParent(parent);
- result.setDuration(duration);
- return result;
- }
- /**
- * Make a standard toast that just contains a text view with the text from a resource.
- *
- * @param context The context to use. Usually your {@link android.app.Application}
- * or {@link android.app.Activity} object.
- * @param resId The resource id of the string resource to use. Can be formatted text.
- * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
- * {@link #LENGTH_LONG}
- * @param parent AnimationToast use a PopupWindow, so need a parent.
- * suggestion → using activity.getWindow().getDecorView()
- *
- * @throws Resources.NotFoundException if the resource can't be found.
- */
- public static AnimationToast makeText(Context context, int resId, int duration, View parent)
- throws Resources.NotFoundException {
- return makeText(context, context.getResources().getText(resId), duration, parent);
- }
- }
测试代码:
- public class TestActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_test);
- Button button = (Button)findViewById(R.id.bt_show_toast);
- button.setOnClickListener(listener);
- }
- private View.OnClickListener listener = new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- AnimationToast.makeText(
- TestActivity.this,
- "动画Toast",
- AnimationToast.LENGTH_LONG,
- TestActivity.this.getWindow().getDecorView()
- ).show();
- }
- };
- }
styles中:
- <style name="AnimationToast" mce_bogus="1" parent="android:Animation">
- <item name="android:windowEnterAnimation">@anim/animation_toast_enter</item>
- <item name="android:windowExitAnimation">@anim/animation_toast_exit</item>
- </style>
anim文件中:
animation_toast_enter.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <alpha
- android:duration="500"
- android:fromAlpha="0.0"
- android:interpolator="@android:anim/decelerate_interpolator"
- android:toAlpha="1.0" />
- <translate
- android:duration="500"
- android:fromXDelta="0"
- android:fromYDelta="50%p"
- android:toXDelta="0"
- android:toYDelta="0" />
- </set>
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="500"
- android:fromXDelta="0"
- android:fromYDelta="0"
- android:toXDelta="0"
- android:toYDelta="50%p" />
- <alpha
- android:duration="500"
- android:fromAlpha="1.0"
- android:interpolator="@android:anim/decelerate_interpolator"
- android:toAlpha="0.0" />
- </set>
效果如下:
另一种动画:
自定义的Anim:
1.显示Toast的动画:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="300"
- android:fillAfter="true" >
- <alpha
- android:fromAlpha="0.0"
- android:toAlpha="1.0" />
- <translate
- android:fromYDelta="20%"
- android:toYDelta="0%" />
- <scale
- android:fromXScale="0.5"
- android:fromYScale="0.5"
- android:pivotX="50%"
- android:pivotY="50%"
- android:toXScale="1.0"
- android:toYScale="1.0" />
- </set>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="300"
- android:fillAfter="true" >
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0" />
- <translate
- android:fromYDelta="0%"
- android:toYDelta="20%" />
- </set>