0

タッチされたピクセルの色を取得し、この色を背景色として FrameLayout に設定しようとしていますが、機能しません。なぜ?

これが私のコードです:

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            float xPos = event.getX();
            float yPos = event.getY();

            ImageView iview = (ImageView)v;
            Bitmap bitmap = ((BitmapDrawable)iview.getDrawable()).getBitmap();
            int color = bitmap.getPixel(Math.round(xPos), Math.round(yPos));

            myFrameLayout.setBackgroundColor(Integer.parseInt(Integer.toString(color), 16));

            return true;
        }
4

1 に答える 1

0

エラーは色の解析にあります。

int color を直接使用すると、必要なことが行われます。

    int color = bitmap.getPixel(Math.round(xPos), Math.round(yPos));
    myFrameLayout.setBackgroundColor(color);

ただし、本当にそれを解析して再構築したい場合は、 Color.argb(int, int, int, int) メソッドを使用してそうします。ここで、int パラメータは透明度、赤、緑、青のコンポーネントの 0x00-0xFF 値です。色。

    int color = bitmap.getPixel(Math.round(xPos), Math.round(yPos));
    myFrameLayout.setBackgroundColor(Color.argb(
         (color & (255 << 24)) >> 24, // take the top 8 bits
         (color & (255 << 16)) >> 16, // take the second 8 bits
         (color & (255 << 8 )) >> 8   // take the third 8 bits
          color & 255)         // take the last 8 bits
    );

お役に立てれば。

于 2012-11-02T00:33:58.917 に答える