Android获得线性渐变某点的颜色

 

安卓官方确实提供了好多非常强大的工具给我们了,例如我们最近经常在shape中加入gradient(渐变),像我的项目中用的是线性渐变,

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">

  3. <corners android:radius="5dip" />

  4. <gradient android:startColor="#262626" android:endColor="#ffbc1c" android:angle="0" />

  5. </shape>

转载请保留原文链接: http://blog.csdn.net/u010593680/article/details/50987671

 

并且这个图片作为SeekBar的背景,用来选择颜色,滑到哪就选择哪里的颜色,如图所示:

为此我想到了两种可能的实现方法:
1、获取SeekBar的背景的图片的bitmap,获取对应点来获得颜色
2、直接通过线性渐变的算法获得某个位置上的颜色
方法1 简单方便,对于其他的控件也可以使用,但是消耗的内存比较大
方法2 内存消耗非常少,但是需要了解线性渐变的算法,并且只能对线性渐变的图片有效,其他渐变方式都需要重新写,但是线性渐变往往用得比较多,所以打算使用第二个方法。

线性渐变是最简单的渐变,思想就是对应颜色A的R G B不断的往颜色B的R G B靠近,并且认识到的是:在java中,(很多其他语言也是)用一个int来存放颜色的RGB值,但是这个只是存储方式而已,实际上每个RGB是互不相关的,渐变时需要分别取出,分别变化。写了一个简易的两个颜色渐变的颜色选择器,代码如下:

 

 
  1. /**

  2. * Created by chenxiaoxuan1 on 16/3/25.

  3. */

  4. public class LinearGradientUtil {

  5. private int mStartColor;

  6. private int mEndColor;

  7.  
  8. public LinearGradientUtil(int startColor, int endColor) {

  9. this.mStartColor = startColor;

  10. this.mEndColor = endColor;

  11. }

  12.  
  13. public void setStartColor(int startColor) {

  14. this.mStartColor = startColor;

  15. }

  16.  
  17. public void setEndColor(int endColor) {

  18. this.mEndColor = endColor;

  19. }

  20. //获取某一个百分比间的颜色,radio取值[0,1]

  21. public int getColor(float radio) {

  22. int redStart = Color.red(mStartColor);

  23. int blueStart = Color.blue(mStartColor);

  24. int greenStart = Color.green(mStartColor);

  25. int redEnd = Color.red(mEndColor);

  26. int blueEnd = Color.blue(mEndColor);

  27. int greenEnd = Color.green(mEndColor);

  28.  
  29. int red = (int) (redStart + ((redEnd - redStart) * radio + 0.5));

  30. int greed = (int) (greenStart + ((greenEnd - greenStart) * radio + 0.5));

  31. int blue = (int) (blueStart + ((blueEnd - blueStart) * radio + 0.5));

  32. return Color.argb(255,red, greed, blue);

  33. }

  34. }

 

 

实现后并且用单元测试测试过,然后就正式的使用了,使用结果还是很满意的~

后来,有一个需求是做一个简单的条形颜色选择器,也是用SeekBar实现的,也是线性渐变,只是颜色多了一些而已。如图:

原理是一样的,代码如下:

 

 
  1. /**

  2. * 线性颜色取色器

  3. * Created by chenxiaoxuan1 on 16/3/25.

  4. */

  5. public class ColorPickGradient {

  6. //设置的颜色

  7. public static final int[] PICKCOLORBAR_COLORS = new int[]{0xFFFFFFFF,0xFFFF3030, 0xFFF4A460,

  8. 0xFFFFFF00, 0xFF66CD00,

  9. 0xFF458B00, 0xFF0000EE,

  10. 0xFF912CEE,0xFF000000};

  11. //每个颜色的位置

  12. public static final float[] PICKCOLORBAR_POSITIONS = new float[]{0f, 0.1f, 0.2f, 0.3f, 0.5f, 0.65f,0.8f,0.9f,1f};

  13.  
  14. private int[] mColorArr = PICKCOLORBAR_COLORS;

  15. private float[] mColorPosition = PICKCOLORBAR_POSITIONS;

  16.  
  17. public ColorPickGradient(int[] colorArr) {

  18. this.mColorArr = colorArr;

  19. }

  20.  
  21. public ColorPickGradient() {

  22. }

  23.  
  24. /**

  25. * 获取某个百分比位置的颜色

  26. * @param radio 取值[0,1]

  27. * @return

  28. */

  29. public int getColor(float radio) {

  30. int startColor;

  31. int endColor;

  32. if (radio >= 1) {

  33. return mColorArr[mColorArr.length - 1];

  34. }

  35. for (int i = 0; i < mColorPosition.length; i++) {

  36. if (radio <= mColorPosition[i]) {

  37. if (i == 0) {

  38. return mColorArr[0];

  39. }

  40. startColor = mColorArr[i - 1];

  41. endColor = mColorArr[i];

  42. float areaRadio = getAreaRadio(radio,mColorPosition[i-1],mColorPosition[i]);

  43. return getColorFrom(startColor,endColor,areaRadio);

  44. }

  45. }

  46. return -1;

  47. }

  48.  
  49. public float getAreaRadio(float radio, float startPosition, float endPosition) {

  50. return (radio - startPosition) / (endPosition - startPosition);

  51. }

  52.  
  53. /**

  54. * 取两个颜色间的渐变区间 中的某一点的颜色

  55. * @param startColor

  56. * @param endColor

  57. * @param radio

  58. * @return

  59. */

  60. public int getColorFrom(int startColor, int endColor, float radio) {

  61. int redStart = Color.red(startColor);

  62. int blueStart = Color.blue(startColor);

  63. int greenStart = Color.green(startColor);

  64. int redEnd = Color.red(endColor);

  65. int blueEnd = Color.blue(endColor);

  66. int greenEnd = Color.green(endColor);

  67.  
  68. int red = (int) (redStart + ((redEnd - redStart) * radio + 0.5));

  69. int greed = (int) (greenStart + ((greenEnd - greenStart) * radio + 0.5));

  70. int blue = (int) (blueStart + ((blueEnd - blueStart) * radio + 0.5));

  71. return Color.argb(255, red, greed, blue);

  72. }

  73.  
  74. }


使用代码如下:

 

 

 
  1. private SeekBar mSbColor;

  2.  
  3. //....

  4. //设置SeekBar的颜色

  5. private void initColorBar(){

  6. ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {

  7. @Override

  8. public Shader resize(int width, int height) {

  9. LinearGradient linearGradient = new LinearGradient(0, 0, width, height,

  10. ColorPickGradient.PICKCOLORBAR_COLORS, ColorPickGradient.PICKCOLORBAR_POSITIONS, Shader.TileMode.REPEAT);

  11. return linearGradient;

  12. }

  13. };

  14. PaintDrawable paint = new PaintDrawable();

  15. paint.setShape(new RectShape());

  16. paint.setCornerRadius(10);

  17. paint.setShaderFactory(shaderFactory);

  18. mSbColor.setProgressDrawable(paint);

  19. }

  20. //当SeekBar被滑动时,获取颜色

  21. mSbColor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

  22. @Override

  23. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

  24. float radio = (float)progress / mSbColor.getMax();

  25. mColor = mColorPickGradient.getColor(radio);

  26. }

  27.  
  28. @Override

  29. public void onStartTrackingTouch(SeekBar seekBar) {

  30.  
  31. }

  32.  
  33. @Override

  34. public void onStopTrackingTouch(SeekBar seekBar) {

  35.  
  36. }

  37. });


转载请保留原文链接: http://blog.csdn.net/u010593680/article/details/50987671

 

 

Android 用代码设置Shape,corners,Gradient

网上查找资料    记录学习

int strokeWidth = 5; // 3dp 边框宽度
    int roundRadius = 15; // 8dp 圆角半径
    int strokeColor = Color.parseColor("#2E3135");//边框颜色
    int fillColor = Color.parseColor("#DFDFE0");//内部填充颜色

    GradientDrawable gd = new GradientDrawable();//创建drawable
    gd.setColor(fillColor);
    gd.setCornerRadius(roundRadius);
    gd.setStroke(strokeWidth, strokeColor);
setBackgroundDrawable(gd);
如果想设置Gradient的渐变色:
方法是改变GradientDrawable的创建方法:
int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分别为开始颜色,中间夜色,结束颜色

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值