Android 触摸事件时从图像视图中选择颜色

Android 触摸事件时从图像视图中选择颜色,android,Android,我试图从用户触摸图像的图像中获取颜色。我能够得到x,y坐标,并且可以使用矩阵计算像素,但是我的问题是它没有给我正确的颜色 private void getColor(MotionEvent event, Button capture) { float HeightRatio = (float) image.getHeight() / (float) imageView.getHeight(); float WidthRatio = (float) image.getWidth()

我试图从用户触摸图像的图像中获取颜色。我能够得到x,y坐标,并且可以使用矩阵计算像素,但是我的问题是它没有给我正确的颜色

private void getColor(MotionEvent event, Button capture) {
    float HeightRatio = (float) image.getHeight() / (float) imageView.getHeight();
    float WidthRatio = (float) image.getWidth() / (float) imageView.getWidth();
    Matrix inverse = new Matrix();
    imageView.getImageMatrix().invert(inverse);
    float[] touchPoint = new float[]{event.getX(), event.getY()};

    i2.setX(event.getX());
    i2.setY(event.getY());
    inverse.mapPoints(touchPoint);
    int x = Integer.valueOf((int) touchPoint[0]);
    int y = Integer.valueOf((int) touchPoint[1]);
    x = (int) (x * WidthRatio);
    y = (int) (y * HeightRatio);
    if (x < 0) {
        x = 0;
    } else if (x > image.getWidth() - 1) {
        x = image.getWidth() - 1;
    }

    if (y < 0) {
        y = 0;
    } else if (y > image.getHeight() - 1) {
        y = image.getHeight() - 1;
    }
    i2.setBackgroundColor(image.getPixel(x, y));
    i2.setVisibility(View.VISIBLE);
    capture.setBackgroundColor(image.getPixel(x, y));
}
private void getColor(运动事件,按钮捕获){
float-HeightRatio=(float)image.getHeight()/(float)imageView.getHeight();
float WidthRatio=(float)image.getWidth()/(float)imageView.getWidth();
矩阵逆=新矩阵();
imageView.getImageMatrix().invert(逆);
float[]touchPoint=newfloat[]{event.getX(),event.getY()};
i2.setX(event.getX());
i2.setY(event.getY());
反向映射点(接触点);
int x=整数。valueOf((int)接触点[0]);
int y=整数。value of((int)接触点[1]);
x=(int)(x*宽度比);
y=(整数)(y*高度比);
if(x<0){
x=0;
}else if(x>image.getWidth()-1){
x=image.getWidth()-1;
}
if(y<0){
y=0;
}else if(y>image.getHeight()-1){
y=image.getHeight()-1;
}
i2.setBackgroundColor(image.getPixel(x,y));
i2.设置可见性(View.VISIBLE);
setBackgroundColor(image.getPixel(x,y));
}
这是我用来获得触摸坐标颜色的方法

谢谢, Vipin

试试这个:

final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event){
    int x = (int)event.getX();
    int y = (int)event.getY();
    int pixel = bitmap.getPixel(x,y);

    //then do what you want with the pixel data, e.g
    int redValue = Color.red(pixel);
    int blueValue = Color.blue(pixel);
    int greenValue = Color.green(pixel);        
    return false;
        }
   });
试试这个:

final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event){
    int x = (int)event.getX();
    int y = (int)event.getY();
    int pixel = bitmap.getPixel(x,y);

    //then do what you want with the pixel data, e.g
    int redValue = Color.red(pixel);
    int blueValue = Color.blue(pixel);
    int greenValue = Color.green(pixel);        
    return false;
        }
   });
为什么这么复杂?

你试过这个吗

private void getColor(MotionEvent event, Button capture) {
float[] touchPoint = new float[]{event.getX(), event.getY()};
float x = touchPoint[0];
float y = touchPoint[1];
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

capture.setBackgroundColor(image.getPixel(x, y));
}
为什么这么复杂?

你试过这个吗

private void getColor(MotionEvent event, Button capture) {
float[] touchPoint = new float[]{event.getX(), event.getY()};
float x = touchPoint[0];
float y = touchPoint[1];
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

capture.setBackgroundColor(image.getPixel(x, y));
}

您好,谢谢您的回复,我已经尝试了上面的代码,但是仍然没有在接触点获得正确的颜色。您好,谢谢您的回复,我已经尝试了上面的代码,但是仍然没有在接触点获得正确的颜色。