通過一張圖片和一個顏色定製view的選擇器

    /**
     * @param picId   點擊狀態之前的圖片ID
     * @param colorId 點擊狀態之後顏色ID
     */
    public void setSelector(@NonNull View view, @DrawableRes int picId, @ColorRes int colorId) {
        BitmapDrawable normalDraw = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), picId));
        Drawable pressedDraw = DrawableCompat.wrap(normalDraw);
        DrawableCompat.setTintList(pressedDraw, getResources().getColorStateList(colorId));

        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDraw);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), picId));
        stateListDrawable.addState(new int[]{}, bitmapDrawable);
        view.setBackgroundDrawable(stateListDrawable);


    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章