每当将值传递到位于屏幕中心或低于屏幕中心的x,y时,我都会获得正确的像素颜色,但是当x,y的值传递到屏幕中心上方时,我会得到错误的颜色值,以下是我正在使用的代码
private int getColor(int x, int y){
Drawable imgDrawable = ((ImageView) mImageView_picture).getDrawable();
Bitmap bitmap1 = ((BitmapDrawable) imgDrawable).getBitmap();
Matrix inverse = new Matrix();
((ImageView) mImageView_picture).getImageMatrix().invert(inverse);
float[] touchPoint = new float[]{x, y};
inverse.mapPoints(touchPoint);
int xCoord = (int) touchPoint[0];
int yCoord = (int) touchPoint[1];
int touchedRGB = bitmap1.getPixel(xCoord,yCoord);
int redValue = Color.red(touchedRGB);
int greenValue = Color.green(touchedRGB);
int blueValue = Color.blue(touchedRGB);
int alphaValue = Color.alpha(touchedRGB);
int colorValue = Color.argb(alphaValue, redValue, greenValue, blueValue);
return colorValue;
}
最佳答案
您的X和Y坐标有可能是错误的。
尝试变得像这样。
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Matrix inverse = new Matrix();
imageView.getImageMatrix().invert(inverse);
float[] touchPoint = new float[] {motionEvent.getX(), motionEvent.getY()};
inverse.mapPoints(touchPoint);
int xCoord = Integer.valueOf((int)touchPoint[0]);
int yCoord = Integer.valueOf((int)touchPoint[1]);
Log.v("coordinate",xCoord +" - "+yCoord );
return true;
}
});