夸克浏览器是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>