0
我在屏幕上顯示圖像。圖像由灰色陰影組成。當我點擊圖片時,我想要獲取點擊的rgb代碼。這是可能的Android?我如何獲得點擊的顏色?
我在屏幕上顯示圖像。圖像由灰色陰影組成。當我點擊圖片時,我想要獲取點擊的rgb代碼。這是可能的Android?我如何獲得點擊的顏色?
在onTouch電話試試這個:
public boolean onTouch (View v, MotionEvent ev)
{
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN :
break;
case MotionEvent.ACTION_UP :
ImageView img = (ImageView) findViewById (YOUR_IMG_DRAWABLE);
img.setDrawingCacheEnabled(true);
Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
img.setDrawingCacheEnabled(false);
int pxl = imgbmp.getPixel(evX, evY);
int redComponent = Color.red(pxl);
int greenComponent = Color.green(pxl);
int blueComponent = Color.blue(pxl);
...USE COLOR HERE...
break;
}
}
希望幫助!
您可以使用下面的參考。您可以覆蓋觸摸偵聽器上的imageview並獲取imageview的像素和rgb值。
public class MainActivity extends Activity {
ImageView iv;
int redValue,blueValue,greenValue;
int pixel;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv= (ImageView) findViewById(R.id.imageView1);
BitmapDrawable bitmapDrawable = (BitmapDrawable)iv.getDrawable();
bitmap = bitmapDrawable.getBitmap();
iv.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// int color = bitmap.getPixel(event.getX(),event.getY());
break;
case MotionEvent.ACTION_UP:
pixel = bitmap.getPixel((int)event.getX(),(int)event.getY());
redValue = Color.red(pixel);
blueValue = Color.blue(pixel);
greenValue = Color.green(pixel);
System.out.println("...."+redValue+"..blue"+blueValue+"..."+greenValue+"color"+pixel);
Toast.makeText(MainActivity.this,""+pixel, 1000).show();
break;
}
return true;
}
});
}
}
我認爲這是相當完整的答案在http://stackoverflow.com/questions/7807360/how-to-get-pixel-colour-in-android。 –
當我把顏色,我設置一個標籤的圖像視圖。後來,我確實拿到了標籤()來獲取信息 – mromer