TextToast -- 自訂Toast源碼

來源:互聯網
上載者:User

標籤:

 

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();
}
};
}

TextToast -- 自訂Toast源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.