欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-15 10:11:28
本文介绍了imageview 实现android colorpikcer 选择器的示例代码,分享给大家,具体如下: android colorpikcer 选择器...

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

android colorpikcer 选择器

ImageView 实现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);
  }
}

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

上一篇:

下一篇: