本文介绍了如何获取像素的颜色在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了PNG图像作为按钮的背景(有透明背景图片),当我触摸按钮,它表明我X坐标和按钮的的Y坐标触摸的位置,但我想知道感动像素颜色按钮的位置。

其实我想知道触摸的位置是否是按钮的透明区域或按钮的颜色区域。您可以检查我已经为此开发了我的code。请帮我在这方面;您的帮助将是亲切AP preciated。

  button.setOnTouchListener(新OnTouchListener()
        {
            公共布尔onTouch(视图V,MotionEvent事件)
            {                INT x_coordinate =(int)的event.getX();
                INT y_coordinate =(int)的event.getY();            // INT颜色= Bitmap.getPixel(X,Y);            如果(event.getAction()== MotionEvent.ACTION_DOWN)
                {
                }            如果(event.getAction()== MotionEvent.ACTION_UP)
                {
                }                返回true;
            }
        });


解决方案

首先,你可以使用的 BitmapFactory 的像这样的位图:结果
位图mBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.pic1);

现在你有位图。在该位图,你可以调用函数与getPixel(INT X,int y)对来得到这个像素的颜色。结果
我想,那么你可以从颜色的Alpha ..

更多信息请参见以下链接:




I have set a PNG image(image having transparent background) as button background, When I touch the button it shows me X-coordinate and Y-coordinate of the button’s touched position but, I want to know pixel color of the touched position of the button.

Actually I want to know whether the touched position is transparent area of the button or colored area of the button. You can check my code that I have developed for this purpose. Please help me in this respect; your help would be cordially appreciated.

 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;
            }
        });
解决方案

First, you can get the Bitmap with the BitmapFactory like this:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

Now you have the Bitmap. On this Bitmap, you can call the function getPixel(int x, int y) to get the Color of this Pixel.
I guess you then can get the alpha from that Color..

See the following links for further information:

这篇关于如何获取像素的颜色在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:15