自定义眨眼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.布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <!--    android:background="@drawable/toast_custom_common_bg"-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/bg_white_20_bg"
        android:gravity="center"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_20"
            android:gravity="center">

            <ImageView
                android:id="@+id/iv_ic_launcher1"
                android:layout_width="@dimen/dp_8"
                android:layout_height="@dimen/dp_16"
                android:src="@drawable/bg_black_5_bg" />

            <ImageView
                android:id="@+id/iv_ic_launcher2"
                android:layout_width="@dimen/dp_8"
                android:layout_height="@dimen/dp_16"
                android:layout_marginLeft="@dimen/dp_10"
                android:layout_toRightOf="@id/iv_ic_launcher1"
                android:src="@drawable/bg_black_5_bg" />
        </RelativeLayout>

        <TextView
            android:layout_marginRight="@dimen/dp_20"
            android:id="@+id/tvCustomToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:paddingLeft="@dimen/dp_5"
            android:paddingTop="@dimen/dp_15"
            android:paddingBottom="@dimen/dp_15"
            android:textColor="@color/c_2c2b"
            android:textSize="@dimen/dp_17" />
    </LinearLayout>

</LinearLayout>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值