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

環(huán)形的ColorPicker,主要思路是:
- Color 選在放在ImageView 的background上面,根據(jù)點(diǎn)擊的位置判斷選擇的顏色。
- 重寫(xiě)onTouch,在onTouch 里面判斷點(diǎn)擊點(diǎn)的顏色。
- 根據(jù)當(dāng)前選擇的顏色設(shè)置圖片的src.
獲取Bitmap
在 ColorPickerView 構(gòu)造函數(shù)中初始化 Bitmap。因?yàn)間etBackground有多種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);
}
重寫(xiě)onTouch
根據(jù)Touch 事件的左邊獲取 Bitmap 對(duì)應(yīng)點(diǎn)的顏色。
需要注意的是如果 View 的寬和高參數(shù)是 wrap_content, MotionEvent 的點(diǎn)擊的點(diǎn)一定在Bitmap 的坐標(biāo)內(nèi)。但是如果不是wrap_content, 需要對(duì)坐標(biāo)轉(zhuǎn)換,利用矩陣Matrix 對(duì)點(diǎn)擊點(diǎn)轉(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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter開(kāi)發(fā)Mac桌面應(yīng)用實(shí)現(xiàn)自動(dòng)提取生成視頻字幕文件
這篇文章主要為大家介紹了Flutter開(kāi)發(fā)Mac桌面應(yīng)用實(shí)現(xiàn)自動(dòng)提取生成視頻字幕文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Flutter路由跳轉(zhuǎn)參數(shù)處理技巧詳解
這篇文章主要為大家介紹了Flutter路由跳轉(zhuǎn)參數(shù)處理技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android觸摸及手勢(shì)操作GestureDetector
這篇文章主要a為大家詳細(xì)介紹了Android觸摸及手勢(shì)操作GestureDetector的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
Android端“被擠下線(xiàn)”功能的單點(diǎn)登錄實(shí)現(xiàn)
本篇文章主要介紹了Android端“被擠下線(xiàn)”功能的單點(diǎn)登錄實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android通過(guò)ExifInterface判斷Camera圖片方向的方法
今天小編就為大家分享一篇關(guān)于Android通過(guò)ExifInterface判斷相機(jī)圖片朝向的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
詳解 android 光線(xiàn)傳感器 light sensor的使用
這篇文章主要介紹了詳解 android 光線(xiàn)傳感器 light sensor的使用的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android學(xué)習(xí)筆記——Menu介紹(二)
這次將繼續(xù)上一篇文章沒(méi)有講完的Menu的學(xué)習(xí),上下文菜單(Context menu)和彈出菜單(Popup menu)2014-10-10

