先把自定义类赠上,如果您觉得有用,请赞!
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毫秒
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-07 08:13:58