1. 程式人生 > >自定義Toast,從頂部掉下,然後再彈一下

自定義Toast,從頂部掉下,然後再彈一下

先把自定義類贈上,如果您覺得有用,請贊!

import java.util.Timer;
import java.util.TimerTask;

import com.example.viewpageindicator.R;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import
android.widget.Toast; public class MyToast { private LayoutParams mParams; private boolean mShowTime; private boolean mIsShow; private WindowManager mWdm; private View mToastView; private Timer mTimer; private MyToast(Context context, String text, boolean showTime ){ mShowTime = showTime;//記錄Toast的顯示長短型別
mIsShow = false;//記錄當前Toast的內容是否已經在顯示 mWdm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); //通過Toast例項獲取當前android系統的預設Toast的View佈局 mToastView = Toast.makeText(context, text, Toast.LENGTH_SHORT).getView(); mTimer = new Timer(); //設定佈局引數
setParams(); } public static MyToast MakeText(Context context, String text, boolean showTime) { MyToast result = new MyToast(context, text, showTime); return result; } private void setParams() { // TODO Auto-generated method stub mParams = new WindowManager.LayoutParams(); mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.format = PixelFormat.TRANSLUCENT; mParams.windowAnimations = R.style.anim_view;//設定進入退出動畫效果 mParams.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 = 200; } public void show(){ if(!mIsShow){//如果Toast沒有顯示,則開始載入顯示 mIsShow = true; mWdm.addView(mToastView, mParams);//將其載入到windowManager上 mTimer.schedule(new TimerTask() { @Override public void run() { mWdm.removeView(mToastView); mIsShow = false; } }, (long)(mShowTime ? 3500 : 2000)); } } }

動畫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 動畫xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="100"
    android:duration="100"
    />
  <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="-200"
    android:duration="150"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    />
    <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="200"
    android:duration="100"
    />
      <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="-100"
    android:duration="150"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    />
  <alpha 
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="150"
    />

</set>

anim_out 動畫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>

淡出介面!

呼叫方法 第三個引數為true和false true則3500毫秒false則2000毫秒