Android自定义view实现图片选色器

简介

本文介绍该自定义view的使用及实现的方法,主要实现以下几个功能:

- 选取圆盘选色图片上的颜色,实时监听
- 可设置选色指示图片,跟随触摸位置、指示所选颜色,示例中为白色圆环
- 可自己设置选色图片(目前只支持圆形图片)

github链接

使用效果

首先看下使用效果:

使用示例

在项目中导入该库

在工程的 build.gradle中加入:

allprojects {
  repositories {
   ...
   maven { url "https://jitpack.io" }
  }
 }

module的build.gradle中加入依赖:

dependencies {
   compile 'com.github.autume:ColorPickerView:1.0'
 }

xml

<RelativeLayout
  android:id="@+id/rl_picker"
  android:layout_below="@+id/img_color"
  android:layout_marginTop="30dp"
  android:layout_centerHorizontal="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <colorpickerview.oden.com.colorpicker.ColorPickerView
   android:id="@+id/color_picker"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

  <ImageView
   android:id="@+id/img_picker"
   android:layout_centerInParent="true"
   android:src="@mipmap/color_picker"
   android:layout_width="25dp"
   android:layout_height="25dp" />

 </RelativeLayout>

选色代码

private void initRgbPicker() {
  colorPickerView = (ColorPickerView) findViewById(R.id.color_picker);
  colorPickerView.setImgPicker(MainActivity.this, img_picker, 25); //最后一个参数是该颜色指示圈的大小(dp)
  colorPickerView.setColorChangedListener(new ColorPickerView.onColorChangedListener() {
   @Override
   public void colorChanged(int red, int blue, int green) {
    img_color.setColorFilter(Color.argb(255, red, green, blue));
   }

   @Override
   public void stopColorChanged(int red, int blue, int green) {

   }
  });
 }

对外公开的API

 public void setImgPicker(final Context context, final ImageView imgPicker, final int pickerViewWidth)
 public void setImgResource(final int imgResource)
 public void setColorChangedListener(onColorChangedListener colorChangedListener)

实现过程

attrs属性

可通过picture_resource属性设置用来选色的资源id,现仅支持圆形图片

 <declare-styleable name="ColorPickerView">
  <attr name="picture_resource" format="reference"/>
 </declare-styleable>

xml

布局中就是放入一个ImageView控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/rl_root"
 tools:background="@color/black"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

 <ImageView
  android:id="@+id/img_color_rang"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:src="@mipmap/lights_colors" />

</RelativeLayout>

属性获取及view初始化

private void initAttrs(Context context, AttributeSet attrs) {
  if (null != attrs) {
   TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorPickerView);
   imgResource = typedArray.getResourceId(R.styleable.ColorPickerView_picture_resource, 0);
   typedArray.recycle();
  }
 }

 private void initView(Context context) {
  View view = LayoutInflater.from(context).inflate(R.layout.color_picker, this);
  imgColorRang = (ImageView) view.findViewById(R.id.img_color_rang);
  rl_root = (RelativeLayout) view.findViewById(R.id.rl_root);

  if (imgResource != 0)
   imgColorRang.setImageResource(imgResource);

  bitmap = ((BitmapDrawable) imgColorRang.getDrawable()).getBitmap();//获取圆盘图片
 }

颜色回调监听

 private onColorChangedListener colorChangedListener;//颜色变换监听

 public void setColorChangedListener(onColorChangedListener colorChangedListener) {
  this.colorChangedListener = colorChangedListener;
 }

 /**
  * 颜色变换监听接口
  */
 public interface onColorChangedListener {
  void colorChanged(int red, int blue, int green);
  void stopColorChanged(int red, int blue, int green);
 }

触摸事件

触摸事件写在父控件上,可以统一处理用来选色的view及指示选色位置的view(imgPicker),imgPicker为指示显示位置的圆框,若设置了则跟随手指移动。

 private void initTouchListener() {
  rl_root.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {

    if (range_radius == 0) {
     range_radius = imgColorRang.getWidth() / 2; //圆盘半径
     centreX = imgColorRang.getRight() - range_radius;
     centreY = imgColorRang.getBottom() - imgColorRang.getHeight() / 2;
     select_radius = range_radius - pickerViewPadding/5;
    }

    float xInView = event.getX();
    float yInView = event.getY();
    Log.d(TAG, "xInView: " + xInView + ",yInView: " + yInView + ",left: " + imgColorRang.getLeft() + ",top: " + imgColorRang.getTop() + ",right: " +imgColorRang.getRight() + ",bottom: " + imgColorRang.getBottom());

    //触摸点与圆盘圆心距离
    float diff = (float) Math.sqrt((centreY - yInView) * (centreY - yInView) + (centreX - xInView) *
      (centreX - xInView));

    //在选色图片内则进行读取颜色等操作
    if (diff <= select_radius) {

     //选色位置指示,若设置了则移动到点取的位置
     if (imgPicker != null ) {
      int xInWindow = (int) event.getX();
      int yInWindow = (int) event.getY();
      int left = xInWindow + v.getLeft() - imgPicker.getWidth() / 2;
      int top = yInWindow + v.getTop() - imgPicker.getWidth() / 2;
      int right = left + imgPicker.getWidth();
      int bottom = top + imgPicker.getHeight();

      imgPicker.layout(left, top, right, bottom);
     }

     if ((event.getY() - imgColorRang.getTop()) < 0)
      return true;
     //读取颜色
     int pixel = bitmap.getPixel((int) (event.getX() - imgColorRang.getLeft()), (int) (event.getY() - imgColorRang.getTop())); //获取选择像素
     if (colorChangedListener != null) {
      if (event.getAction() == MotionEvent.ACTION_UP) {
       colorChangedListener.stopColorChanged(Color.red(pixel), Color.blue(pixel), Color.green(pixel));
      }else {
       colorChangedListener.colorChanged(Color.red(pixel), Color.blue(pixel), Color.green(pixel));
      }
     }
     Log.d(TAG, "radValue=" + Color.red(pixel) + " blueValue=" + Color.blue(pixel) + " greenValue" + Color.green(pixel));
    }
    return true;
   }
  });
 }

设置指示图标

设置图标,同时根据图标的大小设置控件的padding避免在边界处显示不全的问题。

public void setImgPicker(final Context context, final ImageView imgPicker, final int pickerViewWidth) {
  this.imgPicker = imgPicker;
  pickerViewPadding = dip2px(context, pickerViewWidth/2);
  new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
    rl_root.setPadding(pickerViewPadding, pickerViewPadding, pickerViewPadding, pickerViewPadding);
    bitmap = ((BitmapDrawable) imgColorRang.getDrawable()).getBitmap();//获取圆盘图片
   }
  },10);
 }

总结

ok,至此,一个比较简单的选色器就完成了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android自定义View实现颜色选取器

    Android 自定义View 颜色选取器,可以实现水平.竖直选择颜色类似 SeekBar 的方式通过滑动选择颜色. 效果图 xml 属性 1.indicatorColor 指示点颜色 2.indicatorEnable 是否使用指示点 3.orientation 方向 horizontal 水平 vertical 竖直 使用 复制 \library\src-\ColorPickerView.java 和 \library\src\main\res\values\attrs.xml 文件到你的项

  • android自定义View实现圆环颜色选择器

    最近工作需要,自定了一个颜色选择器,效果图如下: 颜色种类是固定的,圆环上有个指示器,指示选中的颜色,这个定义起来应该是很简单了,直接上代码. public class MyColorPicker extends View { private int mThumbHeight; private int mThumbWidth; private String[] colors ; private int sections; //每个小块的度数 private int sectionAngle; p

  • Android实现颜色选取圆盘

    本文实例为大家分享了Android实现颜色选取圆盘的具体代码,供大家参考,具体内容如下 先看效果图 xml布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android

  • Android支持国际化多语言那点事(支持8.0+)

    起因 我们在开发app可能会拓展国外市场,那么对包含英语在内的其它语言支持就很有必要了. 效果 思路 一:添加相关资源文件,并引用. 二:设置configuration,configuration里面指定语言类型. 三:在需要时候更换configuration即可. 实现 在res的values文件夹下新建相关语言类型的资源文件 右键新建资源文件,选择Locale,点击 >> 按钮 选择Language,以及地区(any region)即可 最后 文件名字和其他语言文件名字一样,strings

  • Android自定义控件实现颜色选择器

    ColorPickerView 是之前一个智能家居项目实战中所写的自定义控件,主要用于取得RGB 0~255范围的值,然后转换成十六进制0~FF的值,发送给网关控制RGB彩灯.参考的是网上一个朋友的源码写的,多的不说了,先看效果图 activity_mian.xml文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas

  • Android自定义view实现图片选色器

    简介 本文介绍该自定义view的使用及实现的方法,主要实现以下几个功能: - 选取圆盘选色图片上的颜色,实时监听 - 可设置选色指示图片,跟随触摸位置.指示所选颜色,示例中为白色圆环 - 可自己设置选色图片(目前只支持圆形图片) github链接 使用效果 首先看下使用效果: 使用示例 在项目中导入该库 在工程的 build.gradle中加入: allprojects { repositories { ... maven { url "https://jitpack.io" } }

  • Android自定义View圆形图片控件代码详解

    前言 在日常开发中,圆形的图片效果还是很常见的.可以通过给Paint设置Xfermode来实现,这里简单记录如下. 实现 实现圆形效果的核心是PorterDuffXfermode,对于PorterDuffXfermode,这里不展开,可以查询相关资料. 核心代码 //绘制背景 canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint); //设置模式为:显示背景层和上层的交集,且显示上层图像 mPaint.setXfermode(new

  • Android自定义View图片按Path运动和旋转

    本文实例为大家分享了Android自定义View图片按Path运动旋转的具体代码,供大家参考,具体内容如下 View: /** * author : stone * email : aa86799@163.com * time : 16/5/29 15 29 */ public class EarthPathView extends View { private Path mPath; private Paint mPaint; private Bitmap mBitmap; private P

  • Android自定义View实现跟随手指移动的小兔子

    本文实例为大家分享了Android自定义View实现跟随手指移动的小兔子,供大家参考,具体内容如下 自定义的View实现跟随手指的小兔子 按前面的例子新创建一个project,再在project中新创建一个module 将需要的背景图和兔子图片放入mipmap中 将布局管理器改为帧布局管理器 <?xml version="1.0" encoding="utf-8"?> //修改为帧布局管理器FrameLayout <FrameLayout xmln

  • Android自定义视图中图片的处理

    目录 1.使用Drawable对象 2.Bitmap和BitmapFactory 2.1 例子 2.2 额外知识点(assets) 2.3 代码更严谨 3.Android9新增的ImageDecoder 3.1 例子 所谓游戏,本质就是提供更逼真的.能模拟某种环境的用户界面,并根据某种规则来响应用户操作.为了提供更逼真的用户界面,需要借助于图形.图像处理. 从广义的角度来看,Android应用中的图片不仅包括*.png.*.jpg. *.gif等各种格式的位图,也包括使用XML资源文件定义的各种

  • Android自定义view实现阻尼效果的加载动画

    效果: 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减的振动,又称减幅振动.衰减振动.[1] 不论是弹簧振子还是单摆由于外界的摩擦和介质阻力总是存在,在振动过程中要不断克服外界阻力做功,消耗能量,振幅就会逐渐减小,经过一段时间,振动就会完全停下来.这种振幅随时间减小的振动称为阻尼振动.因为振幅与振动的能量有关,阻尼振动也就是能量不断减少的振动.阻尼振动是非简谐运

  • Android自定义View中attrs.xml的实例详解

    Android自定义View中attrs.xml的实例详解 我们在自定义View的时候通常需要先完成attrs.xml文件 在values中定义一个attrs.xml 然后添加相关属性 这一篇先详细介绍一下attrs.xml的属性. <?xml version="1.0" encoding="utf-8"?> <resources> //自定义属性名,定义公共属性 <attr name="titleText" for

  • Android自定义View实现等级滑动条的实例

     Android自定义View实现等级滑动条的实例 实现效果图: 思路: 首先绘制直线,然后等分直线绘制点: 绘制点的时候把X值存到集合中. 然后绘制背景图片,以及图片上的数字. 点击事件down的时候,换小图片为大图片.move的时候跟随手指移动. up的时候根据此时的X计算最近的集合中的点,然后自动吸附回去. 1,自定义属性 <?xml version="1.0" encoding="utf-8"?> <resources> <de

  • Android 自定义imageview实现图片缩放实例详解

    Android 自定义imageview实现图片缩放实例详解 觉得这个自定义的imageview很好用 性能不错  所以拿出来分享给大家  因为不会做gif图  所以项目效果 就不好贴出来了  把代码贴出来 1.项目结构图 2.Compat.class package com.suo.image; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.view.View; pu

随机推荐