1. package guide.yunji.com.guide.view;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.ColorMatrix;
  6. import android.graphics.ColorMatrixColorFilter;
  7. import android.graphics.drawable.Drawable;
  8. import android.support.annotation.Nullable;
  9. import android.support.v7.widget.AppCompatImageView;
  10. import android.util.AttributeSet;
  11.  
  12. import guide.yunji.com.guide.R;
  13.  
  14. public class HSBImageView extends AppCompatImageView {
  15. private static ColorMatrix colorMatrix = new ColorMatrix();
  16. /**
  17. * 色调,改变颜色
  18. */
  19. private static ColorMatrix hueMatrix = new ColorMatrix();
  20. /**
  21. * 饱和度,改变颜色的纯度
  22. */
  23. private static ColorMatrix saturationMatrix = new ColorMatrix();
  24. /**
  25. * 亮度,控制明暗
  26. */
  27. private static ColorMatrix brightnessMatrix = new ColorMatrix();
  28.  
  29. private float hueValue = 0f;
  30. private float saturationValue = 1f;
  31. private float brightnessValue = 1f;
  32.  
  33. public HSBImageView(Context context) {
  34. super(context);
  35. }
  36.  
  37. public HSBImageView(Context context, AttributeSet attrs) {
  38. this(context, attrs, 0);
  39. }
  40.  
  41. public HSBImageView(Context context, AttributeSet attrs, int defStyleAttr) {
  42. super(context, attrs, defStyleAttr);
  43. TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.HSBImageView);
  44. hueValue = array.getFloat(R.styleable.HSBImageView_hueValue, 0f);
  45. saturationValue = array.getFloat(R.styleable.HSBImageView_saturationValue, 1f);
  46. brightnessValue = array.getFloat(R.styleable.HSBImageView_brightnessValue, 1f);
  47. array.recycle(); //释放资源
  48. setHSB(hueValue, saturationValue, brightnessValue);
  49. }
  50.  
  51. @Override
  52. public void setImageResource(int resId) {
  53. super.setImageResource(resId);
  54. setHSB(hueValue, saturationValue, brightnessValue);
  55. }
  56.  
  57. private void setHSB(float hueValue, float saturationValue, float brightnessValue) {
  58. //设置色相,为0°和360的时候相当于原图
  59. hueMatrix.reset();
  60. hueMatrix.setRotate(0, hueValue);
  61. hueMatrix.setRotate(1, hueValue);
  62. hueMatrix.setRotate(2, hueValue);
  63.  
  64. //设置饱和度,为1的时候相当于原图
  65. saturationMatrix.reset();
  66. saturationMatrix.setSaturation(saturationValue);
  67.  
  68. //亮度,为1的时候相当于原图
  69. brightnessMatrix.reset();
  70. brightnessMatrix.setScale(brightnessValue, brightnessValue, brightnessValue, 1);
  71.  
  72. //将上面三种效果和选中的模式混合在一起
  73. colorMatrix.reset();
  74. colorMatrix.postConcat(hueMatrix);
  75. colorMatrix.postConcat(saturationMatrix);
  76. colorMatrix.postConcat(brightnessMatrix);
  77.  
  78. setColorFilter(new ColorMatrixColorFilter(colorMatrix));
  79. }
  80. }
  1. <declare-styleable name="HSBImageView">
  2. <!--name:自定义属性名,format:自定义属性数据类型-->
  3. <attr name="hueValue" format="float"></attr>
  4. <attr name="saturationValue" format="float"></attr>
  5. <attr name="brightnessValue" format="float"></attr>
  6. </declare-styleable>
  1. <guide.yunji.com.guide.view.HSBImageView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. app:saturationValue="1.8"
  5. app:brightnessValue="1.0"
  6. android:id="@+id/shade"/>

可以设置HSB值的imageview

HSBImageView--android--可以设置HSB值的imageview的更多相关文章

  1. android 动态设置TextView值,例:金额添加

    一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...

  2. android 动态设置TextView值,例:金额增加

    一说到动态递增设置TextView值,很多人应该马上就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值但是效率不咋 ...

  3. [android] setOnTouchEvent 设置返回值为true 和 false的区别

    今天在做自定义的可选文本的 TextView 类时,用到了 View 类的 setOnTouchListener(OnTouchListener l)事件监听,在构造 OnTouchListener ...

  4. Android 设置alpha值来制作透明与渐变效果的实例

    Android系统支持的颜色是由4个值组成的,前3个为RGB,也就是我们常说的三原色(红.绿.蓝),最后一个值是A,也就是Alpha.这4个值都在0~255之间.颜色值越小,表示该颜色越淡,颜色值越大 ...

  5. android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用

    android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用,只要把Imageview的src给去掉就成了,src捕获了bac ...

  6. Android控件设置半透明+EditText设置默认值+ 控件居中

    Android控件设置半透明 效果 代码: android:background="#50FFFFFF" 50表示50%透明 Android:EditText设置默认值 andro ...

  7. Android中设置TextView的颜色setTextColor

    tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...

  8. Android RadioGroup设置默认选中项

    今天有人问.Android 里面 RadioGroup里面有两个RadioButton怎么设置默认值? 第一个RadioButton设置 android:checked="true" ...

  9. Browser设置UA值

    SWE Browser中的OptionMenu是Controller通过onKeyDown监听KEYCODE_MENU来显示的 public boolean onKeyDown(int keyCode ...

随机推荐

  1. Django 配置mysql遇到问题(一)

    问题一: django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have ...

  2. MySql操作命令创建学生管理系统

    1.创建学生管理系统数据库xscj create detabase 数据库名: 2.打开数据库 use 数据库名: //创建数据库之后,该数据库不会自动成为当前数据库需要用use来指定 3.创建表名 ...

  3. web模拟终端 --使用shellinabox

    关于shellinabox ShellInABox实现了一个Web服务器,可以将任意命令行工具导出到基于Web的终端仿真器.任何支持JavaScript和CSS的Web浏览器都可以访问此模拟器,并且不 ...

  4. 性能测试基础---LR参数化相关

    性能测试脚本的增强:·参数化·关联·事务·检查点·思考时间·集合点 ·参数化:模拟不同用户的不同请求. ·为什么要做参数化? ·功能:通常来说,系统的某些业务数据具有唯一性的要求. ·性能:一般来说, ...

  5. php长连接和短连接区别

    短连接 连接->传输数据->关闭连接(推荐学习:PHP编程从入门到精通) 比如HTTP是无状态的的短链接,浏览器和服务器每进行一次HTTP操作,就建立一次连接,但任务结束就中断连接. 具体 ...

  6. python应用-n颗骰子的和出现的次数

    from random import randint def roll_dice(n): total=0 for _ in range (n): num=randint(1,6) total+=num ...

  7. 51Nod1686 第K大区间 和 NOI2016 区间

    谈一下尺取法的经典题. 第K大区间 定义一个区间的值为其众数出现的次数. 现给出n个数,求将所有区间的值排序后,第K大的值为多少. 众数:区间里出现次数最多的数字,例如:1 1 2 2 2,区间[1 ...

  8. python基础语法7 闭包函数与装饰器

    闭包函数: 1.闭包函数必须在函数内部定义 2.闭包函数可以引用外层函数的名字 闭包函数是 函数嵌套.函数对象.名称空间与作用域 结合体. # 直接传参 def func(x): print(x) f ...

  9. iis站点设置错误页面返回http状态码为404而不是302或其他

    今天一位客户说网站错误页面返回的状态码是302而不是404,问ytkah要如何处理.这个应该是设置没有正确的原因.我们一步步来排查一下.1.首先打开iis管理器,左侧选择具体的站点,在右侧窗口中点击4 ...

  10. Mac下用命令行获取苹果手机的UDID

    在终端输入命令行:system_profiler SPUSBDataType | grep "Serial Number:.*" | sed s#".*Serial Nu ...