核心思想知識(shí)點(diǎn):
1)匿沛、自定義Toast
2)、build構(gòu)建者模式
3)逃呼、Rxbinding、butterknife的使用
效果圖如下
GIF.gif
功能實(shí)現(xiàn)過程
1苏揣、build.gradle
image.png
image.png
2推姻、BackgroundDrawable.java(設(shè)置彈框的背景顏色、圓角等)
public class BackgroundDrawable extends Drawable{
private static final String TAG = BackgroundDrawable.class.getSimpleName();
private Paint paint;
private Context mContext;
public BackgroundDrawable(@ColorInt int color,Context context) {
mContext = context.getApplicationContext();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void draw(Canvas canvas) {
Log.d(TAG,"draw===============");
int width = canvas.getWidth();
int height = canvas.getHeight();
Log.d(TAG,"width==============="+width);
Log.d(TAG,"height==============="+height);
canvas.drawRoundRect(0,0,width,height,dp2px(20),dp2px(20),paint);
}
@Override
public void setAlpha(int alpha) {
Log.d(TAG,"setAlpha===============");
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
Log.d(TAG,"setColorFilter===============");
paint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
Log.d(TAG,"getOpacity===============");
return PixelFormat.TRANSLUCENT;
}
private int dp2px(int values){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,values,
mContext.getResources().getDisplayMetrics());
}
}
3增炭、toast_layout.xml(彈框布局文件)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/toast_icon"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/toast_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
4拧晕、MyToast.java(自定義Toast)
public final class MyToast {
public final Toast toast;//Toast對(duì)象
public final View view;//Toast的UI效果
public final ImageView icon;//圖標(biāo)
public final TextView message;//內(nèi)容
private MyToast(Context context) {
toast = new Toast(context);
view = LayoutInflater.from(context).inflate(R.layout.toast_layout,null);
icon = (ImageView) view.findViewById(R.id.toast_icon);
message = (TextView) view.findViewById(R.id.toast_message);
}
/**
* 顯示
*/
public void show(){
this.toast.show();
}
public static class Builder{
private Bitmap icon;//圖標(biāo)圖片
private int iconID = R.mipmap.ic_launcher;//圖標(biāo)資源ID
private String message;//內(nèi)容
private int backgroundColor = 0x56000000;//背景顏色
private Context mContext;//上下文
private int duration = Toast.LENGTH_SHORT;//設(shè)置時(shí)間
private MyToast mine;
private int gravity = Gravity.NO_GRAVITY;//設(shè)置位置
private int offsetX = 0;//設(shè)置偏移度X
private int offsetY = 0;//設(shè)置偏移度Y
private boolean isShowIcon;//是否顯示圖標(biāo)
public Builder(Context context) {
this.mContext = context;
}
/**
* 設(shè)置ICON
* @param bitmap
* @return
*/
public Builder setIcon(Bitmap bitmap){
this.icon = bitmap;
return this;
}
public Builder setIcon(@DrawableRes int resId){
this.iconID = resId;
return this;
}
public Builder showIcon(boolean showIcon){
this.isShowIcon = showIcon;
return this;
}
/**
* 設(shè)置內(nèi)容
*/
public Builder setMessage(String hintMessage){
this.message = hintMessage;
return this;
}
/**
* 設(shè)置吐司時(shí)長(zhǎng)
*/
public Builder setDuration(int type){
this.duration = type;
return this;
}
/**
* 設(shè)置背景
*/
public Builder setBackgroundColor(@ColorInt int color){
this.backgroundColor = color;
return this;
}
/**
* 設(shè)置位置
*/
public Builder setGravity(int gravity){
this.gravity = gravity;
return this;
}
/**
* 偏移量
*/
public Builder setOffsetX(int x){
this.offsetX = x;
return this;
}
public Builder setOffsetY(int y){
this.offsetY = y;
return this;
}
/**
* 創(chuàng)建MyToast對(duì)象
*/
public MyToast build(){
if (null == mine){
mine = new MyToast(mContext);//創(chuàng)建對(duì)象
}
if (isShowIcon){
//隱藏圖標(biāo)
mine.icon.setVisibility(View.VISIBLE);
if (null != icon){//判斷是否顯示圖標(biāo)
mine.icon.setImageBitmap(icon);//設(shè)置圖片
}else {
//設(shè)置圖片
mine.icon.setBackgroundResource(iconID);
}
}
if (!message.isEmpty()){//判斷內(nèi)容是否為空
mine.message.setText(message);
}else {
mine.message.setText("");
}
//設(shè)置背景
mine.view.setBackground(new BackgroundDrawable(backgroundColor,mContext));
mine.toast.setDuration(duration);//設(shè)置時(shí)長(zhǎng)
mine.toast.setView(mine.view);//添加自定義效果
mine.toast.setGravity(gravity,offsetX,offsetY);//設(shè)置偏移量
return mine;
}
}
}
5输玷、MainActivity.java(Toast的創(chuàng)建使用)
public class MainActivity extends AppCompatActivity {
@BindView(R.id.hint_toast)
Button hintToast;
private MyToast myToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//創(chuàng)建Toast
myToast = new MyToast.Builder(this)
.setMessage("自定義Toast效果靡馁!")//設(shè)置提示文字
.setBackgroundColor(0xe9ff4587)//設(shè)置背景顏色
.setGravity(Gravity.CENTER)//設(shè)置吐司位置
.showIcon(true)//是否顯示圖標(biāo)
.build();//創(chuàng)建吐司
//按鈕的點(diǎn)擊事件
RxView.clicks(hintToast)
.throttleFirst(2, TimeUnit.SECONDS)
.subscribe(aVoid -> myToast.show());
}
}