1

基本的には以下の2つの障害があります。

  1. カーソルがクリックされた座標の画像の背景色を取得したい。コードは初めて色と座標を取得しています。しかし、もう一度クリックするとエラーが発生します。
  2. RGB整数値ではなく16進数で画像の色を取得し、その特定の色が見つかった特定の領域でその特定の色を変更したいと思います。

コードは次のとおりです。

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();
    }
}
4

2 に答える 2

3

2 番目の質問から始めましょう: Color(として表されるInteger) を対応する 16 進数値に変換するには、16 進数の色表現が実際には次のようになることを知っておく必要があります。

#RGB, #RRGGBB, #RRRGGGBBB, #RRRRGGGGBBBB

したがって、必要な色の基本色コンポーネントを取得し、それらを個別に 16 進文字列に変換してから、それらをまとめることができます。

/**
 * 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 の出力を提供する必要があります。エラーに関する知識がなければ、エラーを解決することはできません。

また、この形式ではスタック オーバーフローのガイドラインに適合しないため、実際には質問を 2 つに分割することを検討する必要があります。

編集:最初の質問について:分析している画像が画面よりも小さく、引き伸ばされているため、画面をタップして座標を取得するxy、画像へのミラーリングが失敗する可能性があります。そのサイズに: x または y の値は、それぞれビットマップの幅、高さよりも大きい場合があります。

ブロックを使用して例外をログに記録しない限りtry-catch、問題が実際にどこにあるのかを知ることはできません。

于 2012-05-16T09:11:43.043 に答える
-1

セレクターを使ってみてはどうですか?

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>
于 2012-05-16T08:09:04.803 に答える