CToast类:
package com.pinkman.dota.util;
import com.pinkman.dota.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CToast {
public static final int LENGTH_SHORT = 2000;
public static final int LENGTH_LONG = 3500;
private final Handler mHandler = new Handler();
private int mDuration = LENGTH_SHORT;
private int mGravity = Gravity.BOTTOM;
private int mX, mY;
private float mHorizontalMargin;
private float mVerticalMargin;
private View mView;
private View mNextView;
private WindowManager mWM;
private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
public static CToast mToast;
//show only one Toast
public static void showSingleToast(Context context,View layout){
if (mToast != null) {
return ;
}
mToast = new CToast(context);
mToast.setView(layout);
mToast.show();
}
public static CToast makeToast(Context context, int resid, int text_id, int durationMills) {
return makeToast(context,resid,context.getString(text_id),durationMills);
}
public static CToast makeToast(Context context, int resid, CharSequence text, int durationMills) {
if (mToast != null) {
mToast.destroy();
}
mToast = new CToast(context);
// 外部矩形弧度
float[] outerR = new float[] { DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10),
DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10), DimenUtils.dip2px(context, 10) };
RoundRectShape s = new RoundRectShape(outerR, null, null);
ShapeDrawable shapedrawble = new ShapeDrawable(s);
shapedrawble.getPaint().setColor(Color.parseColor("#56000000"));
shapedrawble.getPaint().setStyle(Paint.Style.FILL);
LinearLayout mLayout = new LinearLayout(context);
mLayout.setBackground(shapedrawble);
mLayout.setPadding(DimenUtils.dip2px(context, 10), 0, DimenUtils.dip2px(context, 10), 0);
FrameLayout frameLayout = new FrameLayout(context);
FrameLayout.LayoutParams lp_image = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
lp_image.gravity = Gravity.CENTER;
ImageView circle = new ImageView(context);
circle.setImageResource(R.drawable.circle);
RotateAnimation animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
animation.setRepeatCount(Animation.INFINITE);
animation.setInterpolator(new LinearInterpolator());
circle.startAnimation(animation);
frameLayout.addView(circle, lp_image);
ImageView image = new ImageView(context);
image.setImageResource(resid);
frameLayout.addView(image, lp_image);
if (resid < 1) {
frameLayout.setVisibility(View.GONE);
}
mLayout.addView(frameLayout);
TextView tv = new TextView(context);
tv.setTextColor(Color.parseColor("#fdfdfd"));
tv.setTextSize(DimenUtils.sp2px(context, 25));
tv.setText(text);
tv.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, DimenUtils.sp2px(
context, 65));
mLayout.addView(tv, lp);
mToast.mNextView = mLayout;
mToast.mDuration = durationMills;
return mToast;
}
public CToast(Context context) {
init(context);
}
/**
* Set the view to show.
* @see #getView
*/
public void setView(View view) {
mNextView = view;
}
/**
* Return the view.
* @see #setView
*/
public View getView() {
return mNextView;
}
/**
* Set how long to show the view for.
* @see #LENGTH_SHORT
* @see #LENGTH_LONG
*/
public void setDuration(int duration) {
mDuration = duration;
}
/**
* Return the duration.
* @see #setDuration
*/
public int getDuration() {
return mDuration;
}
/**
* Set the margins of the view.
* @param horizontalMargin The horizontal margin, in percentage of the container width, between
* the container's edges and the notification
* @param verticalMargin The vertical margin, in percentage of the container height, between the
* container's edges and the notification
*/
public void setMargin(float horizontalMargin, float verticalMargin) {
mHorizontalMargin = horizontalMargin;
mVerticalMargin = verticalMargin;
}
/**
* Return the horizontal margin.
*/
public float getHorizontalMargin() {
return mHorizontalMargin;
}
/**
* Return the vertical margin.
*/
public float getVerticalMargin() {
return mVerticalMargin;
}
/**
* Set the location at which the notification should appear on the screen.
* @see android.view.Gravity
* @see #getGravity
*/
public void setGravity(int gravity, int xOffset, int yOffset) {
mGravity = gravity;
mX = xOffset;
mY = yOffset;
}
/**
* Get the location at which the notification should appear on the screen.
* @see android.view.Gravity
* @see #getGravity
*/
public int getGravity() {
return mGravity;
}
/**
* Return the X offset in pixels to apply to the gravity's location.
*/
public int getXOffset() {
return mX;
}
/**
* Return the Y offset in pixels to apply to the gravity's location.
*/
public int getYOffset() {
return mY;
}
/**
* schedule handleShow into the right thread
*/
public void show() {
mHandler.post(mShow);
if (mDuration > 0) {
mHandler.postDelayed(mHide, mDuration);
}
}
/**
* schedule handleHide into the right thread
*/
public void hide() {
mHandler.post(mHide);
}
private final Runnable mShow = new Runnable() {
public void run() {
handleShow();
}
};
private final Runnable mHide = new Runnable() {
public void run() {
handleHide();
}
};
private void init(Context context) {
final WindowManager.LayoutParams params = mParams;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Toast;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.setTitle("Toast");
mWM = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
}
private void handleShow() {
if (mView != mNextView) {
// remove the old view if necessary
handleHide();
mView = mNextView;
// mWM = WindowManagerImpl.getDefault();
final int gravity = mGravity;
mParams.gravity = gravity;
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
mParams.horizontalWeight = 1.0f;
}
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
mParams.verticalWeight = 1.0f;
}
mParams.x = mX;
mParams.y = mY;
mParams.verticalMargin = mVerticalMargin;
mParams.horizontalMargin = mHorizontalMargin;
if (mView.getParent() != null) {
mWM.removeView(mView);
}
mWM.addView(mView, mParams);
}
}
private void handleHide() {
if (mView != null) {
if (mView.getParent() != null) {
mWM.removeView(mView);
}
mView = null;
mToast = null;
}
}
private void destroy() {
mHandler.removeCallbacks(mHide);
mHandler.removeCallbacks(mShow);
handleHide();
}
}
DimenUtils:
package com.pinkman.dota.util;
import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
/**
* @author pinkman 尺寸转换及获取屏幕宽高工具类
*/
public class DimenUtils {
public static int sp2px(Context context, float spValue) {
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
public static int px2sp(Context context, float pxValue) {
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
public static int dip2px(Context context, int dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 获取屏幕宽度和高度,单位为px
* @param context
* @return
*/
public static Point getScreenMetrics(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int w_screen = dm.widthPixels;
int h_screen = dm.heightPixels;
return new Point(w_screen, h_screen);
}
/**
* 获取屏幕长宽比
* @param context
* @return
*/
public static float getScreenRate(Context context) {
Point P = getScreenMetrics(context);
float H = P.y;
float W = P.x;
return (H / W);
}
}