通过一张图片和一个颜色定制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);


    }

 

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