美文网首页
Android Tint

Android Tint

作者: 菠萝鱼_lc | 来源:发表于2017-08-11 10:53 被阅读0次

看了几篇关于tint的文章,各有千秋,今天总结了一下。
如果想要兼容6.0以下版本,要用代码的方式来着色。

public class TestTintActivity extends Activity implements View.OnClickListener{
    private ImageView mImageView, mImageView2, mImageSelect, mImageView5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tint);
        mImageView = (ImageView) findViewById(R.id.image);
        mImageView2 = (ImageView) findViewById(R.id.image2);
        mImageSelect = (ImageView) findViewById(R.id.image_select);
        mImageView5 = (ImageView) findViewById(R.id.image5);
        initDemo1();
        initDemo2();
        initDemo3();

    }

    //基础的改变图片颜色的代码
    private void initDemo1() {
        Drawable originalDrawable = ContextCompat.getDrawable(this, R.drawable.ic_star_big_normal);
        Drawable.ConstantState state = originalDrawable.getConstantState();
        Drawable tintDrawable = DrawableCompat.wrap(state == null ? originalDrawable : state.newDrawable()).mutate();
        DrawableCompat.setTint(tintDrawable, Color.parseColor("#ff0000"));
        mImageView.setImageDrawable(tintDrawable);
        mImageView2.setImageDrawable(originalDrawable);
        //Android 系统为了减少内存消耗,将应用中所用到的相同
        //drawable (可以理解为相同资源)共享同一个 state,并称之为 constant state。
        //这里用图表演示一下,两个 View 加载同一个图片资源,创建两个 drawables 对象,
        //但是共享同一个 constant state 的场景:
    }

    /**
     * 如何使用selector + tint
     * 文件selector_home_drawable
     * <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true" android:drawable="@drawable/ic_star_big_normal"/>
     <item android:drawable="@drawable/ic_star_big_normal"/>
     </selector>
     */
    private void initDemo2() {
        Drawable originalDrawable = ContextCompat.getDrawable(this, R.drawable.selector_home_drawable);
        Drawable.ConstantState selectState = originalDrawable.getConstantState();
        Drawable tintDrawable = DrawableCompat.wrap(selectState == null ? originalDrawable : selectState.newDrawable()).mutate();
        DrawableCompat.setTintList(tintDrawable, ContextCompat.getColorStateList(this, R.color.selector_home));
        mImageSelect.setImageDrawable(tintDrawable);
        mImageSelect.setOnClickListener(this);
    }

    /**
     * 如何改变图片底色,保持上面内容不变
     */
    private void initDemo3() {
        Drawable originalDrawable = ContextCompat.getDrawable(this, R.drawable.lingqian_gray);
        Drawable.ConstantState selectState = originalDrawable.getConstantState();
        Drawable tintDrawable = DrawableCompat.wrap(selectState == null ? originalDrawable : selectState.newDrawable()).mutate();
        DrawableCompat.setTintMode(tintDrawable, PorterDuff.Mode.OVERLAY);
        DrawableCompat.setTint(tintDrawable, Color.parseColor("#ff0000"));
        mImageView5.setImageDrawable(tintDrawable);
    }

    @Override
    public void onClick(View v) {

    }
}

相关文章

网友评论

      本文标题:Android Tint

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