美文网首页android精彩编程案例系列
第9例:带图标的信息提示弹框

第9例:带图标的信息提示弹框

作者: 追梦小乐 | 来源:发表于2018-07-02 15:24 被阅读72次

    核心思想知识点:
    1)、自定义Toast
    2)、build构建者模式
    3)、Rxbinding、butterknife的使用

    效果图如下

    GIF.gif

    功能实现过程

    1、build.gradle

    image.png image.png

    2、BackgroundDrawable.java(设置弹框的背景颜色、圆角等)

    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对象
        public final View view;//Toast的UI效果
        public final ImageView icon;//图标
        public final TextView message;//内容
    
        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;//图标图片
            private int iconID = R.mipmap.ic_launcher;//图标资源ID
            private String message;//内容
            private int backgroundColor = 0x56000000;//背景颜色
            private Context mContext;//上下文
            private int duration = Toast.LENGTH_SHORT;//设置时间
            private MyToast mine;
            private int gravity = Gravity.NO_GRAVITY;//设置位置
            private int offsetX = 0;//设置偏移度X
            private int offsetY = 0;//设置偏移度Y
            private boolean isShowIcon;//是否显示图标
            public Builder(Context context) {
                this.mContext = context;
            }
            /**
             * 设置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;
            }
            /**
             * 设置内容
             */
            public Builder setMessage(String hintMessage){
                this.message = hintMessage;
                return this;
            }
    
            /**
             * 设置吐司时长
             */
            public Builder setDuration(int type){
                this.duration = type;
                return this;
            }
            /**
             * 设置背景
             */
            public Builder setBackgroundColor(@ColorInt int color){
                this.backgroundColor = color;
                return this;
            }
            /**
             * 设置位置
             */
            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;
            }
            /**
             * 创建MyToast对象
             */
            public MyToast build(){
                if (null == mine){
                    mine = new MyToast(mContext);//创建对象
                }
                if (isShowIcon){
                    //隐藏图标
                    mine.icon.setVisibility(View.VISIBLE);
                    if (null != icon){//判断是否显示图标
                        mine.icon.setImageBitmap(icon);//设置图片
                    }else {
                        //设置图片
                        mine.icon.setBackgroundResource(iconID);
                    }
                }
                if (!message.isEmpty()){//判断内容是否为空
                    mine.message.setText(message);
                }else {
                    mine.message.setText("");
                }
                //设置背景
                mine.view.setBackground(new BackgroundDrawable(backgroundColor,mContext));
                mine.toast.setDuration(duration);//设置时长
                mine.toast.setView(mine.view);//添加自定义效果
                mine.toast.setGravity(gravity,offsetX,offsetY);//设置偏移量
                return mine;
            }
        }
    
    }
    
    

    5、MainActivity.java(Toast的创建使用)

    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);
            //创建Toast
            myToast = new MyToast.Builder(this)
                    .setMessage("自定义Toast效果!")//设置提示文字
                    .setBackgroundColor(0xe9ff4587)//设置背景颜色
                    .setGravity(Gravity.CENTER)//设置吐司位置
                    .showIcon(true)//是否显示图标
                    .build();//创建吐司
            //按钮的点击事件
            RxView.clicks(hintToast)
                    .throttleFirst(2, TimeUnit.SECONDS)
                    .subscribe(aVoid -> myToast.show());
        }
    }
    
    

    相关文章

      网友评论

        本文标题:第9例:带图标的信息提示弹框

        本文链接:https://www.haomeiwen.com/subject/uxlluftx.html