1

I am new to Android and I am having a problem. I am using a map, and on the map there are different colors. I am trying to get the colors to determine where I clicked on the map but getPixel() returns 0 every time. Can you please help me?

public class ImageZoomActivity extends Activity {

    private ImageViewTouch mImageView;
    public Bitmap map;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        requestWindowFeature( Window.FEATURE_NO_TITLE );
        setContentView( R.layout.imagezoom );
        getWindow().addFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN );

        bitmap_init();
        selectImage();
        click_map();
    }

    @Override
    public void onContentChanged()
    {
        super.onContentChanged();
        mImageView = (ImageViewTouch)findViewById( R.id.imageView1 );
        click_map();
    }

    public void bitmap_init() {
        map = BitmapFactory.decodeResource(getResources(), R.drawable.map);
    }

    public void selectImage() {
        mImageView.setImageBitmapReset(map, true);
        return;
    }

    public void click_map()  {

        //final ImageView image_map = new ImageView(this);
        mImageView.setImageBitmap(map);

        mImageView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

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

                Bitmap bitmap = ((BitmapDrawable)mImageView.getDrawable()).getBitmap();
                int pixel = bitmap.getPixel(x_coordinate,y_coordinate);

                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    Log.d("Log", "Color: " + bitmap.getPixel(x_coordinate, y_coordinate) + " x: " + event.getX() + " y: " + event.getY());
                }

                /*if (event.getAction() == MotionEvent.ACTION_UP)
                {

                }*/

                return true;
            }
        });
    }
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Filip Marusca
  • 176
  • 1
  • 11
  • 1
    Possible duplicate: http://stackoverflow.com/questions/9245071/bitmap-getpixel-always-returning-black?rq=1 – jonstaff Jan 30 '14 at 20:08
  • You already have a Bitmap map, why don't you - in onTouch() - do int pixel = map.getPixel(x_coordinate, y_coordinate); ? Just make sure these coordinates are not bigger than the sizes of map... – Melquiades Jan 30 '14 at 20:15
  • sorry. i used to do that but i tried something and forgot to change that. i just did but it is just the same thing. i just get 0. – Filip Marusca Jan 30 '14 at 20:38
  • i don't think it's a duplicate because there he is creating a black bitmap. here i am using a png picture – Filip Marusca Jan 30 '14 at 20:45
  • Are you getting different coordinates? Please post your layout, ImageViewTouch class and the image you're using – Melquiades Jan 30 '14 at 20:48
  • what do you mean with different coordinates? if you mean in different tests, then no, i always get 0. i will try to add those but it's hard because it says that the text is mostly code and i have to add more details but what i said i believe covers pretty much everything. – Filip Marusca Jan 30 '14 at 21:27
  • I mean when you click on different places, do x/y_coordinate change? Can you include the picture and code for ImageViewTouch, so that I run the project? – Melquiades Jan 31 '14 at 14:13
  • you can get it from here. i did't modify it. i didn't post it here because imageviewtouch extend another class. the only thing that i modified on that project is that it doesn't get a random image from gallery, i give my specific picture. https://github.com/sephiroth74/ImageViewZoom – Filip Marusca Feb 01 '14 at 15:44
  • and this is the picture: http://imgur.com/j4EFKb8 – Filip Marusca Feb 01 '14 at 15:45

0 Answers0