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
0
投稿
猜你喜欢
- 按照官方文档进行的配置:快速开始|mybatis-plus引入依赖:<!-- 引入mybatisPlus --> &
- CAS原理在计算机科学中,比较和交换(Compare And Swap)是用于实现多线程同步的原子指令。 它将内存位置的内容与给定值进行比较
- 手机号登录在现在的项目中用的场景非常多,实现起来也不难,今天我们就一起来通过演示实现登录过程。 一、首先需要注册个第三方的账户,比
- 目录@ConfigurationProperties使用@ConfigurationProperties特点宽松绑定支持复杂属性类型激活@C
- 思路如下:给定一个含有n个元素的整型数组a,求a中所有元素的和。问题的难点在于如何使用递归上。如果使用递归,则需要考虑如何进行递归执行的开始
- 本文实例讲述了C#获取CPU编号的方法。分享给大家供大家参考。具体如下:/// <summary>/// Gets the cp
- JAVA基础八股文Switch能支持哪些类型?jdk5之前,switch能够作用在byte,short,char,int(实际上都是提升为i
- 缘起工作时使用java开发服务器后台,用Jersey写Restful接口,发现有一个Post方法始终获取不到参数,查了半天,发现时获取参数的
- 前言Intellij IDEA 2017.2.2版本针对Springboot设置了一些特性,本篇文章给大家简单介绍一下如何使用这些特性。Ru
- git仓库直达List<String> strings = Lists.newArrayList("name=kk&q
- 本文介绍了C# 用什么方法将BitConverter.ToString产生字符串再转换回去,分享给大家,具体如下:byte[]
- 前言Spring 框架作为一个管理 Bean 的 IoC 容器,那么 Bean 自然是 Spring 中的重要资源了,那 Bean 的作用域
- 前言Springboot应用在启动的时候分为两步:首先生成 SpringApplication 对象 ,运行 SpringApplicati
- @Transactional跟@DS动态数据源注解冲突背景前阵子写一个项目时,有个需求是要往3个库,3个表里插入数据,在同一个方法里,公司是
- 还记得警匪片上,匪徒们是怎么配合实施犯罪的吗?一个团伙在进行盗窃的时候,总有一两个人在门口把风——如果有什么风吹草动,则会立即通知里面的同伙
- 实例如下:package com.bwsk.modules.weixin.util;import java.util.Random;/**
- 网易Java程序员两轮面试题,请作答。part 1:网易JAVA程序员一面1.volatile有什么用?2.Minor GC和Full GC
- 本文实例为大家分享了java封装前端查询条件的具体代码,供大家参考,具体内容如下import hengyi.oa.mobile.except
- 一、基本介绍(Nexus(maven * ))1,如果没有搭建 * 会有什么问题?如果没有 * ,我们所需的所有构件都需要通过 Mave
- 最近安装了idea,觉得比eclipse好用很多,今天不知道为啥yml文件就不识别了,上面显示一个问号,我查了半天,解决办法就是安装一个插件