第9例:帶圖標(biāo)的信息提示彈框

核心思想知識(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());
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市赔嚎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尤误,老刑警劉巖叶圃,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異掺冠,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)德崭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來眉厨,“玉大人,你說我怎么就攤上這事憾股』郏” “怎么了茴恰?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)往枣。 經(jīng)常有香客問我,道長(zhǎng)分冈,這世上最難降的妖魔是什么圾另? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任雕沉,我火速辦了婚禮,結(jié)果婚禮上蘑秽,老公的妹妹穿的比我還像新娘。我一直安慰自己肠牲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布渡嚣。 她就那樣靜靜地躺著,像睡著了一般肥印。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上深碱,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音功咒,去河邊找鬼绞蹦。 笑死力奋,一個(gè)胖子當(dāng)著我的面吹牛幽七,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播猿挚,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼亭饵!你這毒婦竟也來了梁厉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤八秃,失蹤者是張志新(化名)和其女友劉穎肉盹,沒想到半個(gè)月后昔驱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體上忍,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡窍蓝,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吓笙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡絮蒿,死狀恐怖叁鉴,靈堂內(nèi)的尸體忽然破棺而出土涝,到底是詐尸還是另有隱情幌墓,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布茵肃,位于F島的核電站袭祟,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏巾乳。R本人自食惡果不足惜鸟召,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一氨鹏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧仆抵,春花似錦、人聲如沸镣丑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽趣竣。三九已至,卻和暖如春遥缕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背通砍。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留迹冤,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓泡徙,卻偏偏與公主長(zhǎng)得像膜蠢,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子挑围,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,185評(píng)論 25 707
  • 1杉辙、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,982評(píng)論 3 119
  • 我想和你講一個(gè)故事, 一個(gè)我永遠(yuǎn)記得综看, 你一定記不得的故事岖食。 曾經(jīng)有過一些年红碑, 我們深愛著對(duì)方析珊, 你會(huì)因?yàn)榭吹轿衣?..
    米馬閱讀 430評(píng)論 0 3
  • 橘皮土鴨蘿卜湯 味道咸鮮兔毙,具有養(yǎng)胃澎剥,消食,強(qiáng)身健體的功效哑姚。 材料:土鴨叙量、橘皮九串、蘿卜猪钮、料酒、生姜 1.鴨肉斬件肘交,鍋燒...
    萬味林凈菜閱讀 222評(píng)論 0 0
  • 微風(fēng)正好扑馁,陽光不燥腻要,難得好天氣! 心情也隨著天氣變得好起來雄家! 忙忙碌碌一天又一天!...
    落雪小依閱讀 224評(píng)論 0 0