ImageView 实现Android colorPikcer 选择器的示例代码
作者:赤兔欢 时间:2023-03-12 03:21:37
本文介绍了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);
}
}
来源:http://www.jianshu.com/p/b6f558509daa?utm_source=tuicool&utm_medium=referral
标签:Android,选择器
0
投稿
猜你喜欢
C# Double转化为String时的保留位数及格式方式
2021-09-08 13:12:00
Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)的过程解析
2021-10-03 11:25:56
SpringBoot+Elasticsearch实现数据搜索的方法详解
2023-04-17 02:45:46
IISExpress 配置允许外部访问详细介绍
2023-08-02 02:18:10
c#动态加载卸载DLL的方法
2023-06-08 08:18:08
Java读取txt文件中的数据赋给String变量方法
2022-08-04 22:32:19
关于ObjectUtils.isEmpty() 和 null 的区别
2022-05-07 17:10:56
解决unity rotate旋转物体 限制物体旋转角度的大坑
2022-01-14 01:41:49
c++中数字与字符串之间的转换方法(推荐)
2023-02-22 17:58:09
java map转Multipart/form-data类型body实例
2023-04-19 13:16:18
深入探讨JAVA中的异常与错误处理
2023-06-11 00:30:24
java base64编码、解码的三种方式总结
2022-12-29 22:20:51
Unity动画混合树实例详解
2022-06-17 14:41:27
C#使用Aforge调用摄像头拍照的方法
2022-02-10 02:34:36
.net的序列化与反序列化实例
2022-12-29 00:50:50
动态webservice调用接口并读取解析返回结果
2021-10-19 07:05:45
Java深入探究Object类的方法
2022-11-24 06:48:53
C++与namespace有关的两个编译错误的讲解
2021-12-09 11:37:35
Spring boot的上传图片功能实例详解
2022-10-09 09:52:00
Eclipse自定义启动画面和图标的方法介绍
2022-05-14 09:27:13