真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

ImageView實現(xiàn)AndroidcolorPikcer選擇器的示例代碼

本文介紹了ImageView 實現(xiàn)Android colorPikcer 選擇器的示例代碼,分享給大家,具體如下:

創(chuàng)新互聯(lián)是一家專業(yè)提供宿遷企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、網(wǎng)站制作、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為宿遷眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進行中。

Android colorPikcer 選擇器

ImageView 實現(xiàn)Android colorPikcer 選擇器的示例代碼

環(huán)形的ColorPicker,主要思路是:

  1. Color 選在放在ImageView 的background上面,根據(jù)點擊的位置判斷選擇的顏色。
  2. 重寫onTouch,在onTouch 里面判斷點擊點的顏色。
  3. 根據(jù)當(dāng)前選擇的顏色設(shè)置圖片的src.

獲取Bitmap

在 ColorPickerView 構(gòu)造函數(shù)中初始化 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

根據(jù)Touch 事件的左邊獲取 Bitmap 對應(yīng)點的顏色。

需要注意的是如果 View 的寬和高參數(shù)是 wrap_content, MotionEvent 的點擊的點一定在Bitmap 的坐標內(nèi)。但是如果不是wrap_content, 需要對坐標轉(zhuǎn)換,利用矩陣Matrix 對點擊點轉(zhuǎn)換。

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);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


分享名稱:ImageView實現(xiàn)AndroidcolorPikcer選擇器的示例代碼
分享地址:http://weahome.cn/article/jdppho.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部