如何为drawable着色
来源:互联网 发布:c语言 文件打开 编辑:程序博客网 时间:2024/06/04 19:48
如果 UI 对同一种图片 选中 非选中状态 只是颜色的变化 ,有的还有夜间模式,想一想一个app中药多少对应的套图,apk的包越来越大,老板不满意了,这时想杀产品狗和UI设计的心都有,不过天无绝人之路:请继续阅读:
核心 为.png 着色,前提是 白色和透明图片。
package com.xuan.drawabletinted;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LightingColorFilter;import android.graphics.Paint;import android.graphics.drawable.BitmapDrawable;public final class TintedBitmapDrawable extends BitmapDrawable { private int tint; private int alpha; public TintedBitmapDrawable(final Resources res, final Bitmap bitmap, final int tint) { super(res, bitmap); this.tint = tint; this.alpha = Color.alpha(tint); } public TintedBitmapDrawable(final Resources res, final int resId, final int tint) { super(res, BitmapFactory.decodeResource(res, resId)); this.tint = tint; this.alpha = Color.alpha(tint); } public void setTint(final int tint) { this.tint = tint; this.alpha = Color.alpha(tint); } @Override public void draw(final Canvas canvas) { final Paint paint = getPaint(); if (paint.getColorFilter() == null) { paint.setColorFilter(new LightingColorFilter(tint, 0)); paint.setAlpha(alpha); } super.draw(canvas); }}
intedBitmapDrawable,它重写了draw() 方法,实用aLightingColorFilter来处理着色的问题。它只包含三个函数,因此不用担心会增加太多方法个数。颜色可以在额外增加的两个构造函数中指定,也可以通过setTint()方法。
package com.xuan.drawabletinted;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.widget.ImageView;public class MainActivity extends Activity { ImageView imageView, imageView2, imageView3, imageView4, imageView5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.img_view); imageView.setImageDrawable(new TintedBitmapDrawable(getResources(), R.mipmap.ic_add_course, Color.BLUE)); imageView2 = (ImageView) findViewById(R.id.img_view2); imageView2.setImageDrawable(new TintedBitmapDrawable(getResources(), R.mipmap.ic_add_course, Color.YELLOW)); imageView3 = (ImageView) findViewById(R.id.img_view3); imageView3.setImageDrawable(new TintedBitmapDrawable(getResources(), R.mipmap.ic_add_course, Color.BLACK)); imageView4 = (ImageView) findViewById(R.id.img_view4); imageView4.setImageDrawable(new TintedBitmapDrawable(getResources(), R.mipmap.ic_add_course, Color.GREEN)); imageView5 = (ImageView) findViewById(R.id.img_view5); imageView5.setImageDrawable(new TintedBitmapDrawable(getResources(), R.mipmap.ic_add_course, Color.MAGENTA)); }}
说到这里,有的人可能不乐意了:Lollipop不是支持着色了么?回答:这是兼容Lollipop一下的版本
可能又要问,图片的圆形 阴影 标签什么的...没关系,这么简单的代码,相信你能改出来
0 0
- 如何为drawable着色
- Tint Drawable为图标着色
- Drawable着色问题
- Drawable图标着色
- Drawable 着色的后向兼容方案
- Android05--Android之Drawable的着色兼容问题
- 如何做出flat着色
- Bitmap转换为Drawable
- byte[]转换为Drawable
- 为TextView添加Drawable
- Android学习-----DrawableCompat(给Drawable 着色)的使用
- 如何放大一个drawable
- 实用教程:如何着色更高效
- ChemBioDraw 如何给文本着色
- Android Drawable 转化为 Bitmap
- KEIL中为自己的关键字着色
- 【译文】利用Palette为android应用着色
- 着色
- SB/XIB使用过程报错处理
- Mindjet -- 项目需求整理分析
- Docker学习笔记之一,搭建一个JAVA Tomcat运行环境
- 按照字典里面某一个字段排序实现
- 1054: [HAOI2008]移动玩具
- 如何为drawable着色
- Cannot find new threads: generic error
- 变量(查询到的数据)不存在:not available,但是需要用到,用try{}catch(e){}来处理
- MapReduce操作二度人脉二
- 老吴的BUG
- opengl es2.0 渲染文字
- 代理
- DB2常用函数详解(一):字符串函数
- [Android] ImageView.ScaleType设置图解