1

ボタンの背景としてPNG画像(背景が透明な画像)を設定しました。ボタンをタッチすると、ボタンのタッチ位置のX座標とY座標が表示されますが、タッチ位置のピクセルカラーを知りたいです。ボタン。

実は、タッチした位置がボタンの透明な部分なのか、ボタンの色のついた部分なのか知りたいです。この目的のために私が開発したコードを確認できます。この点で私を助けてください。よろしくお願いします。

 button.setOnTouchListener(new OnTouchListener() 
        {
            public boolean onTouch(View v, MotionEvent event) 
            {

                int x_coordinate  = (int)event.getX(); 
                int y_coordinate = (int)event.getY(); 

            //int color = Bitmap.getPixel(x,y);

            if (event.getAction() == MotionEvent.ACTION_DOWN) 
                {
                }

            if (event.getAction() == MotionEvent.ACTION_UP) 
                {
                }

                return true;
            }
        });
4

3 に答える 3

3

これを試して

Drawable drawable = button.getBackground();
Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
int color = bmp.getPixel(x_coordinate, y_coordinate);
于 2013-03-21T10:19:45.443 に答える
2

まず、次のようにBitmapFactoryを使用してビットマップを取得できます。
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

これでビットマップができました。このビットマップでは、関数getPixel(int x, int y)を呼び出してこのピクセルの色を取得できます。
そうすれば、その色からアルファを取得できると思います。

詳細については、次のリンクを参照してください。

于 2013-03-21T10:21:04.710 に答える
2

これを試して

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
    int color=bitmap.getPixel(x_coordinate, y_coordinate);

ifステートメントでこの色を使用して、必要な操作を実行します

于 2013-03-21T10:27:54.600 に答える