Android 如何获取和替换图像';onTouch事件中的背景色

Android 如何获取和替换图像';onTouch事件中的背景色,android,Android,基本上有以下两个障碍: 我想在单击光标的坐标处获取图像的背景色。代码第一次获取颜色和坐标。但当我再次单击时,会发生错误 我想用十六进制而不是RGB整数值获取图像的颜色,然后在找到颜色的特定区域用我的颜色更改该特定颜色 代码如下: private ImageView mImageView; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate

基本上有以下两个障碍:

  • 我想在单击光标的坐标处获取图像的背景色。代码第一次获取颜色和坐标。但当我再次单击时,会发生错误
  • 我想用十六进制而不是RGB整数值获取图像的颜色,然后在找到颜色的特定区域用我的颜色更改该特定颜色
  • 代码如下:

    private ImageView mImageView;
    private Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vp_view_img);
    
        if(getIntent().hasExtra("BitmapImage")) 
        {
            final Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("BitmapImage");
    
            mImageView = (ImageView) findViewById(R.id.canvas_image);
            mImageView.setImageBitmap(bitmap);
            mImageView.setOnTouchListener(new ImageView.OnTouchListener()
            {     
                @Override   
                public boolean onTouch(View v, MotionEvent event)
                {
                    int x = (int)event.getX();
                    int y = (int)event.getY();
    
                   /*Toast.makeText(getBaseContext(), "Touch coordinates : "
                                         + String.valueOf(event.getX()) + "x"
                                         + String.valueOf(event.getY()),
                                         Toast.LENGTH_SHORT).show();*/
                    ImageView imageView = (ImageView) v;
                    Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
                    int pixel = bitmap.getPixel(x,y);
                    int redValue = Color.red(pixel);
                    int blueValue = Color.blue(pixel);
                    int greenValue = Color.green(pixel);
    
                    Toast.makeText(getBaseContext(), "Color Code :"+
                        redValue+blueValue+greenValue,Toast.LENGTH_SHORT).show();
                    return true;    
                    }    
                });
        }
        else
        {
            Toast.makeText(getBaseContext(), 
                    "No Image Found", 
                    Toast.LENGTH_LONG).show();
        }
    }
    

    使用选择器怎么样

    在res/drawable文件夹中,生成如下代码所示的xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:color="@color/white"/> <!-- selected -->
        <item android:state_pressed="true" android:color="@color/white" />  <!-- pressed -->
        <item android:color="@color/black" />
    </selector>
    

    让我们从第二个问题开始:为了将
    颜色
    (表示为
    整数
    )转换为相应的十六进制值,您应该知道,十六进制颜色表示实际上是:

    #RGB, #RRGGBB, #RRRGGGBBB, #RRRRGGGGBBBB
    
    因此,您可以获取所需颜色的基本颜色组件,并将它们分别转换为十六进制字符串,然后将它们组合在一起:

    /**
     * Converts the passed integer (color) into it's hexadecimal representation  
     * @param pixel the Color to convert
     * @return a String: the <code>pixel</code> parameter in #RRGGBB format 
     */
    private String rgbToHex(int pixel)
    {
        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);
        return "#" + getBaseColorAsHex(redValue) + 
            getBaseColorAsHex(greenValue) + 
            getBaseColorAsHex(blueValue);
    }
    
    /**
     * Returns the hex representation of the <code>baseColor</code>
     * parameter, padding up to 2 characters if necessary.
     * @param baseColor the color whose hex representation is needed
     * @return the hex code for the passed parameter
     */
    private String getBaseColorAsHex(int baseColor)
    {
        final String hex = Integer.toHexString(baseColor);
        return (hex.length() == 1 ? '0' : "") + hex;
    }
    
    对于第一个问题,您应该提供您的logcat输出:在不知道错误的情况下,我们无法帮助解决它

    同样,你应该考虑把问题分成两个部分,因为在这个表单中它不适合堆栈溢出的指导方针! 编辑:关于提出的第一个问题:一个严重的问题可能是,您正在分析的图像比屏幕小,并且被拉伸,因此当您点击屏幕并检索

    x
    y
    坐标时,由于图像的大小,将它们镜像到图像上失败:x或y的值可能大于宽度,分别大于位图的高度


    除非您使用
    try catch
    块并记录异常,否则您将无法判断问题的真正原因。

    您会遇到什么错误?你能粘贴logcat输出吗?05-16 15:58:12.580:E/AndroidRuntime(1404):致命异常:main 05-16 15:58:12.580:E/AndroidRuntime(1404):java.lang.IllegalArgumentException:x必须