问题描述
我有一个活动,我的活动包含一个ImageView.I已经实现了OnTouchListener我ImageView.Here找到想要的颜色的像素在那里我在image.Please碰让我知道如何找到color.I有一个想法像素,
I have one activity, my activity contains one ImageView.I have implemented OnTouchListener for ImageView.Here I want find pixels color where i touch in image.Please let me know how to find pixels color.I have one ideas,
- >在那里的ImageView我摸到只要找到X和Y坐标,找到相同的X和Y坐标的像素,找到像素color.Please让我有没有可能
-->In ImageView where i touch just find X and Y coordinate and find pixels in same X and Y coordinate and find pixels color.Please let me is it possible.
请给予任何建议或文档做到这一点吧。
Please give any suggestion or document for to do this please.
与乌尔参考我写code位图,
with ur reference i wrote code for bitmap,
int mId=R.drawable.with_colors;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mId);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
在onTouch
int x=(int) event.getX();
int y=(int) event.getY();
int color=bitmap.getPixel(x, y);
Log.d("colors","---"+color);
但在这里登录打印颜色--- 0。
But here Log printing "Colors---0".
推荐答案
您可以使用的上的ImageView得到什么绘制,然后通过强制转换为<$得到位图的方法C $ C> BitmapDrawable 并调用的:
You can use the getDrawable()
method of the imageview to get what is drawn, and then get the bitmap from that by casting to BitmapDrawable
and calling getBitmap()
:
Drawable d = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
int color = bitmap.getPixel(x, y);
要获得坐标,分配:
To get the coordinates, assign an OnTouchListener
:
imageview.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_UP:
int x = (int) event.getX();
int y = (int) event.getY();
//TODO: get pixel at x, y
break;
default:
return false;
}
return true;
}
});
在code以上就可以基本上只是将第一张code它说:在x拿到像素,Y来获得像素的颜色。
In the code above you can basically just insert the first code where it says "get pixel at x, y" to get the pixel color.
这篇关于如何找到具体的像素颜色协调的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!