自定义眨眼Toast(仿夸克浏览器退出Toast)

夸克浏览器是UC推出的纯净版无广告浏览器,发现他的Toast很Nice,索性也写了一个。

样式如下:

                                               

代码如下行:

/**
 * 吐司
 *
 * @param ctx
 * @param content
 */
public void showToast(final Context ctx, final String content) {

    new Thread(new Runnable() {

        @Override
        public void run() {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {

                @Override
                public void run() {
                    if (mToast == null) {
                        mToast = new Toast(ctx);
                        //设置toast显示的位置,居中
                        mToast.setGravity(Gravity.CENTER, 0, DisplayUtils.getScreenHeight(this) / 4);
                        mToast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
                        View root = LayoutInflater.from(ctx).inflate(R.layout.toast_custom_common, null);
                        mTvToast = (TextView) root.findViewById(R.id.tvCustomToast);
                        ImageView mIvLauncher1 = (ImageView) root.findViewById(R.id.iv_ic_launcher1);
                        ImageView mIvLauncher2 = (ImageView) root.findViewById(R.id.iv_ic_launcher2);
                        mToast.setView(root);
                        mTvToast.setText(content);
                        setAnimation(mIvLauncher1);
                        setAnimation(mIvLauncher2);
                        mToast.show();
                    } else {
                        mToast.setGravity(Gravity.CENTER, 0, DisplayUtils.getScreenHeight(this) / 4);
                        mToast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
                        View root = LayoutInflater.from(ctx).inflate(R.layout.toast_custom_common, null);
                        mTvToast = (TextView) root.findViewById(R.id.tvCustomToast);
                        mToast.setView(root);
                        mTvToast.setText(content);
                        ImageView mIvLauncher1 = (ImageView) root.findViewById(R.id.iv_ic_launcher1);
                        ImageView mIvLauncher2 = (ImageView) root.findViewById(R.id.iv_ic_launcher2);
                        setAnimation(mIvLauncher1);
                        setAnimation(mIvLauncher2);
                        mToast.show();
                    }
                }
            });
        }
    }).start();
}
//眨眼效果
private void setAnimation(ImageView iv_chat_head) {
    final Animation animation = new AlphaAnimation(1, 0); 
    animation.setDuration(500);
    animation.setInterpolator(new LinearInterpolator()); 
    animation.setRepeatCount(Animation.INFINITE); 
    animation.setRepeatMode(Animation.REVERSE); 
    iv_chat_head.setAnimation(animation);
}
3.布局文件:




    

    

        

            

            
        

        
    

你可能感兴趣的:(自定义,炫酷Toast,定义眨眼Toast)