ImageView 实现Android colorPikcer 选择器的示例代码
作者:赤兔欢 发布时间:2023-03-12 03:21:37
标签:Android,选择器
本文介绍了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


猜你喜欢
- @NonNull导致无法序列化的问题以上这个代码在接参的时候报了一个缺少无参构造函数无法序列化的错误将.class反编译可以看到编译后的源码
- SpringCloudStream配置以下配置摘自《SpringCloud微服务实战》,配置主要包括两大部分:Stream配置(基础配置、通
- 实现效果:奔溃的线程侠:(单线程)主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。绘制图片import jav
- springboot pom文件project报错问题如下 解决方案情况1:maven版本要和课程保持一致,网盘直接下载。情况2:
- MybatisMyBatis ,是国内最火的持久层框架采用了ORM思想解决了实体类和数据库表映射的问题。对JDBC进行了封装,屏蔽了JDBC
- 常量:其值不变即为常量。语法:数据类型 常量名 = 值;doubl
- @Cacheable在同一类中方法调用无效上述图片中,同一个类中genLiveBullets()方法调用同类中的queryLiveByRoo
- Java Config 下的Spring Test方式用了三种方式:1.纯手动取bean:package com.wang.test;imp
- 对于int,double等的tostring:C 货币 2.5.ToString("C
- 前言在讲这两种方式之前,我们先来说明一下什么是java中的jar文件jar (Java Archive File),翻译过来就是java的档
- 1.servlet:定义:接口2.配置servlet:public class HelloServlet extends HttpServl
- 一、下载客户端代码package javadownload; import java.io.ByteArrayOutputStream; i
- 本文实例讲述了Android编程设计模式之Builder模式。分享给大家供大家参考,具体如下:一、介绍Builder模式是一步一步创建一个复
- 分页插件  MP中自带了分页插件的功能,只需要在配置类中进行简单的配置即可使用分页的相关功能。分页插件常
- private void button1_Click(object sender, EventArgs e) {
- Android中很多产品(比如360手机助手、网易菜单...)都采用侧滑菜单的展现形式,采用这种展现形式1、能把更多的展现内容都存放在菜单中
- 注意:要保证已经有Node类和单链表的初始化,这样才能调用反转方法并显示结果。方法如下://Node<T>指泛型结
- 使用Inten的putExtra传递第一个Activity中//创建意图对象 Intent intent = new Intent(this
- 介绍:上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置
- 在前面一篇Java Comparable和Comparator对比详解中,对于java中的排序方法进行比较和具体剖析,主要是针对 Compa