Skip to content

Instantly share code, notes, and snippets.

@oksep
Last active June 15, 2016 11:45
Show Gist options
  • Save oksep/244f67a4c13dfa2c7b9e44c4943d5e15 to your computer and use it in GitHub Desktop.
Save oksep/244f67a4c13dfa2c7b9e44c4943d5e15 to your computer and use it in GitHub Desktop.
CustomToast.java
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* 自定义Toast
*/
public class CustomToast {
private WindowManager mWindowManager;
private static CustomToast instance;
private View mView;
private Handler mHandler;
private Context mContext;
private ShowTask mShowTask;
private CancelTask mCancelTask;
public CustomToast(Context context) {
mContext = context.getApplicationContext();
mHandler = new Handler();
mShowTask = new ShowTask();
mCancelTask = new CancelTask();
}
public static CustomToast getInstance(Context context) {
if (instance == null) {
synchronized (CustomToast.class) {
if (instance == null) {
instance = new CustomToast(context);
}
}
}
return instance;
}
public void cancel() {
mHandler.post(mCancelTask);
}
public void show(String content, int duration) {
mShowTask.setContent(content);
mShowTask.setDuration(duration == Toast.LENGTH_SHORT ? 2000 : 3500);
mHandler.removeCallbacks(mCancelTask);
mHandler.post(mShowTask);
}
private class ShowTask implements Runnable {
private String content;
private int duration;
public void setContent(String cont) {
content = cont;
}
@Override
public void run() {
show(content);
mHandler.postDelayed(mCancelTask, duration);
}
private void show(String content) {
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = getWindowLayoutParams();
if (mView == null) {
initView(content, params);
} else {
updateView(content, params);
}
}
public void setDuration(int duration) {
this.duration = duration;
}
}
private void updateView(String content, WindowManager.LayoutParams params) {
TextView view = (TextView) mView.findViewWithTag("tag_content_view");
if (view != null) {
view.setBackgroundColor(Color.TRANSPARENT);
view.setText(content);
}
try {
mWindowManager.updateViewLayout(mView, params);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initView(String content, WindowManager.LayoutParams params) {
Context context = mContext;
LinearLayout layout = new LinearLayout(context);
layout.setBackgroundColor(0xff0088dd);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER);
TextView tv = new TextView(context);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(-1, -2);
lpTextView.bottomMargin = lpTextView.topMargin = 16;
lpTextView.rightMargin = lpTextView.leftMargin = 2 * lpTextView.bottomMargin;
tv.setTag("tag_content_view");
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tv.setTextColor(Color.WHITE);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 34);
tv.setText(content);
layout.addView(tv, lpTextView);
mView = layout;
try {
mWindowManager.addView(mView, params);
} catch (Exception e) {
e.printStackTrace();
}
}
@NonNull
private WindowManager.LayoutParams getWindowLayoutParams() {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.format = PixelFormat.RGBA_8888;
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.WRAP_CONTENT;
params.y = 730;
params.type |= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
params.windowAnimations = android.R.style.Animation_Toast;
return params;
}
private class CancelTask implements Runnable {
@Override
public void run() {
if (mWindowManager != null && mView != null) {
try {
mWindowManager.removeView(mView);
} catch (Exception e) {
e.printStackTrace();
}
mView = null;
mWindowManager = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment