美文网首页
Android着色探究笔记

Android着色探究笔记

作者: 卖梦想的男孩 | 来源:发表于2017-02-20 17:34 被阅读27次

    图片的着色功能可以缩减资源,动态调整图片的混合色值。

    1. 使用ColorFilter
      ColorFilter支持Drawable和Paint,一般配合Matrix和Mode可以实现更为强大的着色功能,自由度更高
      如果觉得使用矩阵麻烦,利用ColorMatrix提供的api可以更方便些
    • setRotate 方法设置色调
    • setSaturation 方法设置饱和度
    • setScale 方法设置亮度
    • postConcat 组合设置
      栗子:
     Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0);//饱和度为0,直接设置成灰度图像
     paint.setColorFilter(new ColorMatrixColorFilter(matrix));
     canvas.drawBitmap(bitmap,0,0,paint);
    

    像ImageView和Drawable等都直接提供了setColorFilter。

    1. 使用tint
      tint是为了更方便的直接给图片、背景、前景设置着色功能,tint也支持设置color和mode(查看源码其实最终也是使用了ColorFilter),为了兼容可以使用DrawableCompat来动态设置。
    Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_book_black_24dp);
    ColorStateList colors = ContextCompat.getColorStateList(this, R.color.input_tint_colors);
    //mutate可以拷贝一份着色配置,否则多个组件使用同一张图片的时候,会一起被修改,不过也是个可以利用的路子
    Drawable result = DrawableCompat.wrap(drawable).mutate();
    DrawableCompat.setTintList(result, colors);
    mTintView.setImageDrawable(result);
    

    相关文章

      网友评论

          本文标题:Android着色探究笔记

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