自定义时间Toast(只弹一次)

来源:互联网 发布:麦克风变声软件 编辑:程序博客网 时间:2024/05/05 12:40

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













0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 家里门钥匙丢了怎么办 合租朝北晒衣服怎么办 卧室门钥匙丢了怎么办 邻居在我家防盗窗上凉被子怎么办 有钥匙打不开门怎么办 白色腈纶衣服洗完发黄怎么办 在部队有人整你怎么办 老公掉粪坑了你怎么办图片 好久没跑步腿疼怎么办 跑1000米要5分钟怎么办 孩子眼睛近视加散光怎么办 在部队混的差怎么办 2岁宝宝走路踮脚怎么办 宝宝走路膝盖弯曲不直怎么办 做了蛙跳大腿疼怎么办 跳完蛙跳腿疼怎么办 腿受凉了疼怎么办偏方 鸭子步蛙跳后腿疼怎么办 戴墨镜鼻子会红怎么办 校服黑色裙子染色了怎么办 新警培训时怀孕怎么办? 大学生欠了网贷怎么办? 车侧面底盘被刮怎么办 军人家属被打没人处理怎么办 孩子字写的难看怎么办 企业k宝锁死了怎么办 建行信用卡密码忘了怎么办 主卧对着卫生间怎么办 不会画农场的画怎么办 泰迪骨折了怎么办护理 狗狗的腿骨折了怎么办 狗狗摔了一下腿瘸了怎么办 狗狗前腿摔瘸了怎么办 狗摔跤腿瘸了怎么办 小狗的前腿弯了怎么办 两周的小狗缺钙怎么办 狗腿关节断了怎么办 五岁儿童抵抗力差怎么办 3岁儿童抵抗力差怎么办 四岁儿童抵抗力差怎么办 我孩子和我相冲怎么办