zoukankan      html  css  js  c++  java
  • TextToast -- 自定义Toast源码

    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.PixelFormat;
    import android.os.Handler;
    import android.util.DisplayMetrics;
    import android.util.TypedValue;
    import android.view.Gravity;
    import android.view.WindowManager;
    import android.widget.TextView;

    /**
    * Created by John on 2016/4/15.
    * 支持设置显示时长,背景,及对齐方式,文字颜色等功能的Toast
    */
    public class TextToast {

    /** 默认显示的时间 ms */
    private static final int DURATION = 3000;


    /** top或Bottom对齐时,默认y偏移的值*/
    private static final int DY = (int) dp2px(40);

    private TextView mTextView;
    private final WindowManager mWM;
    private final Handler mHanlder = new Handler();
    private final WindowManager.LayoutParams mParams;
    private int mDuration = DURATION;

    private TextToast(Context context) {

    int dp4 = (int) dp2px(4);
    int dp2 = (int) dp2px(2);

    mTextView = new TextView(context);
    mTextView.setTextColor(Color.argb(0xff, 0x00, 0x00, 0x00));
    mTextView.setBackgroundColor(Color.argb(0x88, 0xff, 0xff, 0xff));
    mTextView.setPadding(dp4, dp2, dp4, dp2);
    mTextView.setGravity(Gravity.CENTER_HORIZONTAL);

    mParams = new WindowManager.LayoutParams();
    mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mParams.format = PixelFormat.TRANSLUCENT;
    mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
    mParams.setTitle("Toast");
    mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams
    .FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

    mWM = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    }

    /** 创建一个Toast对象*/
    public static TextToast makeText(Context context, CharSequence s) {
    TextToast toast = new TextToast(context);
    toast.setText(s);
    return toast;
    }

    public TextToast setText(CharSequence s) {
    mTextView.setText(s);
    return this;
    }

    public TextToast setBackgroundResource(int resId) {
    mTextView.setBackgroundResource(resId);
    return this;
    }

    public TextToast setBackgroundColor(int color) {
    mTextView.setBackgroundColor(color);
    return this;
    }

    public TextToast setCenter() {
    mParams.gravity = Gravity.CENTER;
    mParams.y = 0;
    return this;
    }

    public TextToast setTop() {
    mParams.gravity = Gravity.TOP;
    mParams.y = DY;
    return this;
    }

    public TextToast setBottom() {
    mParams.gravity = Gravity.BOTTOM;
    mParams.y = DY;
    return this;
    }

    public TextToast setGravity(int gravity, int x, int y) {
    mParams.gravity = gravity;
    mParams.x = x;
    mParams.y = y;
    return this;
    }

    public TextToast setTextColor(int color) {
    mTextView.setTextColor(color);
    return this;
    }

    public TextToast setDuration(int duration) {
    mDuration = duration;
    return this;
    }

    public void show() {
    mHanlder.removeCallbacks(mHide);
    mHanlder.post(mShow);
    mHanlder.postDelayed(mHide, mDuration);
    }

    public void cancel() {
    mHanlder.removeCallbacks(mHide);
    if (mTextView != null && mTextView.getParent() != null)
    mWM.removeViewImmediate(mTextView);
    }

    private static float dp2px(float dp) {
    DisplayMetrics dm = new DisplayMetrics();
    dm.setToDefaults();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, dm);
    }

    private final Runnable mShow = new Runnable() {
    @Override
    public void run() {
    if (mTextView.getParent() != null)
    mWM.removeView(mTextView);
    mWM.addView(mTextView, mParams);
    }
    };

    private Runnable mHide = new Runnable() {
    @Override
    public void run() {
    cancel();
    }
    };
    }
  • 相关阅读:
    微信小程序
    正则常用表达式
    nodejs基本
    node初学制作登录服务器实例
    前端面试题集锦(三)
    编程:
    js常见编程题
    前端面试题集锦(二)
    细节问题
    前端面试题集锦(一)
  • 原文地址:https://www.cnblogs.com/diysoul/p/5397431.html
Copyright © 2011-2022 走看看