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


    }

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页