android中獲取 bitmap 像素的顏色 之吸管取色功能

    本功能是參考android API colorPickerView修改,實現類似與PS中吸管取色功能。也就是可以對圖片的任意位置取該位置的RGB。本demo中,完成了色盤取色功能。當點擊色盤的某個位置,鬆手時,顯示當前的顏色。由於是demo,顯示的顏色用button的文字顏色的相應改變達到效果。把色盤圖片更換爲其他資源,則對你換的資源取色。具體要按需求改動。色盤取色可以用於繪圖時的顏色選擇,不用彈出對話框選擇有限的幾種顏色。總得來說還是吸管功能。

主要代碼如下:


package com.mrlin.mycolordisk;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.Button;

import com.mrlin.mycolordisk.ColorPickerView.OnColorChangedListener;

public class MainActivity extends Activity {

	private ColorPickerView colorDisk=null;
	private Button btnColor=null;
	public static boolean flagOfColorChange=false;
	private final static int COLOR_CHANGE=1;
	Handler mColorhandler=new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch(msg.what)
			{
			case COLOR_CHANGE:
				btnColor.setTextColor(ColorPickerView.ColorText);
				break;
				
				default:
					break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);  
		btnColor=(Button)findViewById(R.id.btnColor); 
		
		//用線程監聽 是否顏色已經改變
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true)
				{
					//當色盤顏色改變時,也就是鬆手時,把flagOfColorChange置爲true
					//然後handler 發送消息,button改變字體
					
					//此變量爲全局變量,破壞了封裝性。但是實現了功能,有更好的方式可以留言
					if(flagOfColorChange) 
					{
						
						System.out.println("color change!!!");
						flagOfColorChange=false;
						mColorhandler.sendEmptyMessage(COLOR_CHANGE);
					}
				}
				
			}
		}).start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
package com.mrlin.mycolordisk;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View; 

public class ColorPickerView extends View   {
	
    private Context mContext;
	private Paint mRightPaint;            //畫筆
	private int mHeight;                  //view高
	private int mWidth;                   //view寬
	private int[] mRightColors;
	private int LEFT_WIDTH;
	private Bitmap mLeftBitmap;
	private Bitmap mLeftBitmap2;
	private Paint mBitmapPaint;
	private PointF mLeftSelectPoint; 
	private OnColorChangedListener mChangedListener;
	private boolean mLeftMove = false;
	private float mLeftBitmapRadius;
	private Bitmap mGradualChangeBitmap;
	private Bitmap bitmapTemp;
	private int mCallBackColor = Integer.MAX_VALUE;
	int newWidgth;
	int newHeigh;
    public static String hexColor="";
	public static int ColorText=0;
	
	public ColorPickerView(Context context) {
		this(context, null);
	}
	
	public ColorPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	public void setOnColorChangedListenner(OnColorChangedListener listener) {
		
		mChangedListener = listener;
		mChangedListener.onColorChanged(ColorText);
	}
	
	//初始化資源與畫筆
	private void init() {
		bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.piccolor); 
		mRightPaint = new Paint(); 
		mRightPaint.setStyle(Paint.Style.FILL);
		mRightPaint.setStrokeWidth(1);
		mRightColors = new int[3];
		mRightColors[0] = Color.WHITE;
		mRightColors[2] = Color.BLACK;
		mBitmapPaint = new Paint();
		
		mLeftBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.reading__color_view__button);
		mLeftBitmap2 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.reading__color_view__button_press);
		mLeftBitmapRadius = mLeftBitmap.getWidth() / 2;
		mLeftSelectPoint = new PointF(0, 0); 
		 newWidgth=BitmapFactory.decodeResource(getResources(), R.drawable.piccolor).getWidth();
		 newHeigh=BitmapFactory.decodeResource(getResources(), R.drawable.piccolor).getHeight(); 
	}
	
	//important patient please!!!
	@Override
	protected void onDraw(Canvas canvas) { 
		canvas.drawBitmap(getGradual() , null , new 
				Rect(0, 0, LEFT_WIDTH , mHeight ), mBitmapPaint);
		// 右邊

		// 兩個圖標
		if (mLeftMove) {
			canvas.drawBitmap(mLeftBitmap, mLeftSelectPoint.x - mLeftBitmapRadius,
					mLeftSelectPoint.y - mLeftBitmapRadius, mBitmapPaint);
		} else {
			try {
				
				canvas.drawBitmap(mLeftBitmap2, mLeftSelectPoint.x - mLeftBitmapRadius, 
						mLeftSelectPoint.y - mLeftBitmapRadius, mBitmapPaint);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (widthMode == MeasureSpec.EXACTLY) {
			mWidth = width;
		} else {
			mWidth = newHeigh;
		}
		if (heightMode == MeasureSpec.EXACTLY) {
			mHeight = height;
		} else {
			mHeight = newHeigh;
		}
		LEFT_WIDTH = mWidth;
		setMeasuredDimension(mWidth, mHeight);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_MOVE:
				mLeftMove = true;
				proofLeft(x, y);
			    invalidate();
			break;
		case MotionEvent.ACTION_UP:
			//取色
			ColorText=getLeftColor(x, y); 
			mLeftMove = false;
			invalidate();
			//鬆手後,此變量置真,更新button字體顏色
			MainActivity.flagOfColorChange=true;
		}
		return true;
	}
	
	@Override
	protected void onDetachedFromWindow() {
		if (mGradualChangeBitmap != null && mGradualChangeBitmap.isRecycled() == false) {
			mGradualChangeBitmap.recycle();
		}
		if (mLeftBitmap != null && mLeftBitmap.isRecycled() == false) {
			mLeftBitmap.recycle();
		}
		if (mLeftBitmap2 != null && mLeftBitmap2.isRecycled() == false) {
			mLeftBitmap2.recycle();
		}
		super.onDetachedFromWindow();
	}
	
	private Bitmap getGradual() {
		if (mGradualChangeBitmap == null) {
			Paint leftPaint = new Paint();
			leftPaint.setStrokeWidth(1); 
			mGradualChangeBitmap = Bitmap.createBitmap(LEFT_WIDTH, mHeight, Config.RGB_565);
			mGradualChangeBitmap.eraseColor(Color.WHITE);
			Canvas canvas = new Canvas(mGradualChangeBitmap); 
		     canvas.drawBitmap( bitmapTemp, null , new Rect(0, 0, LEFT_WIDTH , mHeight ), mBitmapPaint);
		}
		return mGradualChangeBitmap;
	}
	// 校正xy
	private void proofLeft(float x, float y) {
		if (x < 0) {
			mLeftSelectPoint.x = 0;
		} else if (x > (LEFT_WIDTH)) {
			mLeftSelectPoint.x = LEFT_WIDTH;
		} else {
			mLeftSelectPoint.x = x;
		}
		if (y < 0) {
			mLeftSelectPoint.y = 0;
		} else if (y > (mHeight - 0)) {
			mLeftSelectPoint.y = mHeight - 0;
		} else {
			mLeftSelectPoint.y = y;
		}
	}
	
	private int getLeftColor(float x, float y) {
		Bitmap temp = getGradual();
		// 爲了防止越界
		int intX = (int) x;
		int intY = (int) y;
		if(intX<0)intX=0;
		if(intY<0)intY=0;
		if (intX >= temp.getWidth()) {
			intX = temp.getWidth() - 1;
		}
		if (intY >= temp.getHeight()) {
			intY = temp.getHeight() - 1;
		}
		
		 
		System.out.println("leftColor"+temp.getPixel(intX, intY));
        return temp.getPixel(intX, intY);
	}
	
    

	// ### 內部類 ###
	public interface OnColorChangedListener {
		void onColorChanged(int color);
	}


	
}


<?xml version="1.0" encoding="utf-8"?>

     

運行效果如下:





源代碼地址是:http://download.csdn.net/detail/linhuorongandroid/7119175  也可入q羣383012091 下載源碼與討論。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章