我的屏幕上有一个图像视图。图像由灰色阴影组成。当我点击图片时,我想获得我点击的rgb代码。它可以在Android上使用吗?
答案 0 :(得分:5)
在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;
}
}
希望有所帮助!
答案 1 :(得分:1)
您可以使用以下内容作为参考。您可以在触摸侦听器上覆盖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;
}
});
}
}