Java 当X、Y位于屏幕中心上方时,无法获得正确的颜色值
无论何时将值传递到位于屏幕中心或低于屏幕中心的x,y,我都会得到正确的像素颜色,但当传递位于屏幕中心以上的x,y的值时,我会得到错误的颜色值,下面是我正在使用的代码Java 当X、Y位于屏幕中心上方时,无法获得正确的颜色值,java,android,Java,Android,无论何时将值传递到位于屏幕中心或低于屏幕中心的x,y,我都会得到正确的像素颜色,但当传递位于屏幕中心以上的x,y的值时,我会得到错误的颜色值,下面是我正在使用的代码 private int getColor(int x, int y){ Drawable imgDrawable = ((ImageView) mImageView_picture).getDrawable(); Bitmap bitmap1 = ((BitmapDrawable) imgDrawable).getBi
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;
}
});