ImageView 实现Android colorPikcer 选择器的示例代码

 更新时间:2017年10月19日 09:08:25   作者:赤兔欢  
本篇文章主要介绍了ImageView 实现Android colorPikcer 选择器的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

本文介绍了ImageView 实现Android colorPikcer 选择器的示例代码,分享给大家,具体如下:

Android colorPikcer 选择器

环形的ColorPicker,主要思路是:

  1. Color 选在放在ImageView 的background上面,根据点击的位置判断选择的颜色。
  2. 重写onTouch,在onTouch 里面判断点击点的颜色。
  3. 根据当前选择的颜色设置图片的src.

获取Bitmap

在 ColorPickerView 构造函数中初始化 Bitmap。因为getBackground有多种drawable,然后获取Bitmap 的方式也不用,

void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
    Drawable drawable = getBackground();
    if(drawable instanceof BitmapDrawable){
      mBitmap = ((BitmapDrawable) drawable).getBitmap();
    } else if(drawable instanceof VectorDrawable){
      mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
      Canvas vectorCanvas = new Canvas(mBitmap);
      drawable.setBounds(0, 0, vectorCanvas.getWidth(), vectorCanvas.getHeight());
      drawable.draw(vectorCanvas);
    }

重写onTouch

根据Touch 事件的左边获取 Bitmap 对应点的颜色。

需要注意的是如果 View 的宽和高参数是 wrap_content, MotionEvent 的点击的点一定在Bitmap 的坐标内。但是如果不是wrap_content, 需要对坐标转换,利用矩阵Matrix 对点击点转换。

public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN && mBitmap != null) {
  
      float scaleX = mBitmap.getWidth()*1.0f/v.getWidth();
      float scaleY = mBitmap.getHeight()*1.0f/v.getHeight();

      float[] touchPoint = new float[] { event.getX(), event.getY() };
      Matrix matrix = new Matrix();
      matrix.setScale(scaleX, scaleY);
      matrix.mapPoints(touchPoint);

      mSelectColor = mBitmap.getPixel((int) touchPoint[0], (int) touchPoint[1]);
    }

    return false;
  }

完整的代码:

public class ColorPickerView extends android.support.v7.widget.AppCompatImageView implements View.OnTouchListener{

  private Bitmap mBitmap;
  private int mSelectColor = -1;
  private int mIndex = -1;
  private int[] mDrawableSelects;
  private int[] mColorArray;

  private OnColorSelectedListener mOnColorSelectedListener;


  public ColorPickerView(Context context) {
    this(context, null);
  }

  public ColorPickerView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ColorPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
  }

  void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
    Drawable drawable = getBackground();
    if(drawable instanceof BitmapDrawable){
      mBitmap = ((BitmapDrawable) drawable).getBitmap();
    } else if(drawable instanceof VectorDrawable){
      mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
      Canvas vectorCanvas = new Canvas(mBitmap);
      drawable.setBounds(0, 0, vectorCanvas.getWidth(), vectorCanvas.getHeight());
      drawable.draw(vectorCanvas);
    }

    TypedArray resTypeArray = context.obtainStyledAttributes(attrs, R.styleable.ColorPickerView);
    int colorPickerArrayId = resTypeArray.getResourceId(R.styleable.ColorPickerView_cp_selected_drawable_array, 0);
    resTypeArray.recycle();

    if (colorPickerArrayId != 0) {
      TypedArray typeArray = getResources().obtainTypedArray(colorPickerArrayId);
      mDrawableSelects = new int[typeArray.length()];
      for (int i = 0; i < typeArray.length(); i++) {
        mDrawableSelects[i] = typeArray.getResourceId(i, 0);
      }
      typeArray.recycle();
    }

    setOnTouchListener(this);
  }

  @Override
  public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN && mBitmap != null) {
      if(event.getX() > v.getWidth() || event.getX() < 0){
        return false;
      }

      if(event.getY() > v.getHeight() || event.getY() < 0){
        return false;
      }

      float scaleX = mBitmap.getWidth()*1.0f/v.getWidth();
      float scaleY = mBitmap.getHeight()*1.0f/v.getHeight();

      float[] touchPoint = new float[] { event.getX(), event.getY() };
      Matrix matrix = new Matrix();
      matrix.setScale(scaleX, scaleY);
      matrix.mapPoints(touchPoint);

      mSelectColor = mBitmap.getPixel((int) touchPoint[0], (int) touchPoint[1]);
      mIndex = getColorIndex(mSelectColor);

      if(mDrawableSelects.length > 0 && mIndex >=0 && mIndex < mDrawableSelects.length) {
        ((ImageView) v).setImageResource(mDrawableSelects[mIndex]);
      }

      if(mOnColorSelectedListener != null){
        mOnColorSelectedListener.onColorSelected(mIndex, mSelectColor);
      }
    }

    return false;
  }

  private int getColorIndex(int color){

    for (int i = 0 ; i < mColorArray.length; i++){
      if(color == mColorArray[i]){
        return i;
      }
    }

    return -1;
  }

  public void setSelectColorArray(int[] array) {
    mColorArray = array;
  }

  public void setSelectDrawableIdArray(int[] idArray){
    mDrawableSelects = idArray;
  }

  public int getIndex(){
    return mIndex;
  }

  public int getSelectColor(){
    return mSelectColor;
  }

  public void setOnColorSelectedListener(OnColorSelectedListener listener){
    mOnColorSelectedListener = listener;
  }

  public interface OnColorSelectedListener{
    void onColorSelected(int index , int color);
  }
}

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

相关文章

  • Android实现手势滑动多点触摸放大缩小图片效果

    Android实现手势滑动多点触摸放大缩小图片效果

    这篇文章主要介绍了Android实现手势滑动多点触摸放大缩小图片效果的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Android实现摄像头拍照功能

    Android实现摄像头拍照功能

    这篇文章主要为大家详细介绍了Android实现摄像头拍照功能,本文侧重摄像头拍照功能的调用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • 分享几个Android开发有用的程序代码

    分享几个Android开发有用的程序代码

    本文主要是给大家分享了几个常用而且很实用的程序代码片段,都是个人项目中提取出来的,有需要的小伙伴可以直接拿走使用
    2015-02-02
  • Android操作SQLite基本用法

    Android操作SQLite基本用法

    这篇文章主要介绍了Android操作SQLite基本用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-12-12
  • Android中访问sdcard路径的几种方式

    Android中访问sdcard路径的几种方式

    本文主要介绍了Android中访问sdcard路径的几种方式。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • Android MVP模式ListView中嵌入checkBox的使用方法

    Android MVP模式ListView中嵌入checkBox的使用方法

    这篇文章主要介绍了Android MVP模式ListView中嵌入checkBox的使用方法,如何在ListView中嵌入checkBox配合使用,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android仿qq消息拖拽效果

    Android仿qq消息拖拽效果

    这篇文章主要为大家详细介绍了Android仿qq消息拖拽效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android WorkManager使用以及源码分析

    Android WorkManager使用以及源码分析

    WorkManager 是适合用于持久性工作的推荐解决方案。如果工作始终要通过应用重启和系统重新启动来调度,便是持久性的工作。本文主要介绍了Android WorkManager的使用,需要的可以参考一下
    2023-02-02
  • Flutter配置代理抓包实现过程详解

    Flutter配置代理抓包实现过程详解

    这篇文章主要为大家介绍了Flutter配置代理抓包实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android studio点击跳转WebView详解

    Android studio点击跳转WebView详解

    这篇文章主要为大家详细介绍了Android studio点击跳转WebView的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09

最新评论