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

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

Android colorPikcer 选择器

环形的ColorPicker,主要思路是:

Color 选在放在ImageView 的background上面,根据点击的位置判断选择的颜色。 重写onTouch,在onTouch 里面判断点击点的颜色。 根据当前选择的颜色设置图片的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); } }

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

时间: 2024-08-04 10:25:56

ImageView 实现Android colorPikcer 选择器的示例代码的相关文章

android添加新的示例代码到SDK的samples中

1.拷贝示例代码目录到development/samples目录下 2.修改development/build/sdk.atree,按照文件中其他示例代码添加的格式在后面添加一行即可

Android解析json数据示例代码(三种方式)

Json数据 复制代码 代码如下: [{"code":"110000","sheng":"11","di":"00","xian":"00","name":"北京市","level":1},{"code":"659004","sheng&q

Android时间选择器、日期选择器实现代码_Android

本文为大家分享了两款选择器,一款可以针对时间进行选择.一款可以针对日期进行选择,供大家参考,具体内容如下 一.时间选择器1.1.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.andr

Android时间选择器、日期选择器实现代码

本文为大家分享了两款选择器,一款可以针对时间进行选择.一款可以针对日期进行选择,供大家参考,具体内容如下 一.时间选择器 1.1.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and

Action Bar示例代码 (一)

 今天一起来看下Android Action Bar的示例代码,我们通过活动栏做一个简单选项菜单.下面这个例子将演示ActionBar.NAVIGATION_MODE_STANDARD.ActionBar.NAVIGATION_MODE_TABS和                     : ActionBar.NAVIGATION_MODE_STANDARD等模式的效果.最后Android123仍然提示大家Action Bar是Android 3.0 honeycomb才开始有的特性,老版本的

Android中imageView图片放大缩小及旋转功能示例代码

一.简介 二.方法 1)设置图片放大缩小效果 第一步:将<ImageView>标签中的android:scaleType设置为"fitCenter" android:scaleType="fitCenter" 第二步:获取屏幕的宽度 DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); dm.widthPixels 第三

Android仿微信发送语音消息的功能及示例代码

微信的发送语音是有一个向上取消的,我们使用onTouchListener来监听手势,然后做出相应的操作就行了. 直接上代码: //语音操作对象 private MediaPlayer mPlayer = null; private MediaRecorder mRecorder = null; //语音文件保存路径 private String FileName = null; FileName = Environment.getExternalStorageDirectory().getAbs

Android LibGDX游戏引擎开发教程(三) 示例代码详细讲解

承接了上一篇文章中关于环境搭建的简单示例,这一篇我会详细讲解FirstGame和HelloGameActivity类中 的代码. 一.ApplicationListener接口详解 1.简单代码示例,FirstGame.java: package com.example.hellolibgdx; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.gra

Android 文件选择器详解及实例代码_Android

     本文给大家讲解下Android文件选择器的使用.实际上就是获取用户在SD卡中选择的文件或文件夹的路径,这很像C#中的OpenFileDialog控件.        此实例的实现过程很简单,这样可以让大家快速的熟悉Android文件选择器,提高开发效率.        网上曾经见到过一个关于文件选择器的实例,很多人都看过,本实例是根据它修改而成的,但更容易理解,效率也更高,另外,本实例有自己的特点:        1.监听了用户按下Back键的事件,使其返回上一层目录.