在我的应用程序中,我使用的是 chrisbanes 的 PhotoView Library,我用它来使图像可缩放。然后我需要获取图像上触摸的像素的颜色。只要图像没有移动,它就可以工作,但是当它被放大时,返回的颜色与图像的颜色不匹配。什么是必要的,以便设备识别图像已移动并返回正确的颜色?
编码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.karte);
photoView.setScaleType(ImageView.ScaleType.FIT_XY);
photoView.setDrawingCacheEnabled(true);
photoView.buildDrawingCache(true);
}
photoView.setOnViewTapListener(new OnViewTapListener() {
@Override
public void onViewTap(View view, float x, float y) {
bitmap = photoView.getDrawingCache();
int pixel = bitmap.getPixel((int) x, (int) y);
String text = "x = " + x + ", y = " + y;
Log.d("Position", text);
int redValue = Color.red(pixel);
int greenValue = Color.green(pixel);
int blueValue = Color.blue(pixel);
String hex = String.format("%02x%02x%02x", redValue, greenValue, blueValue);
}
});