0

Update: I found a working answer, Android: how to make a clickable map image with each country producing a different action?

I was trying to do some tests with having different effects on different parts of an image being clicked: http://postimg.org/image/pwrcc7pyh/

For example when I click on Saudi Arabia which has a hex of FFFF00, getPixel(x, y) prints out -256 (I don't know if that's correct). The problem is that Yemen also prints out -256 but has a hex of 9EFA69. Does anyone know why this happens?

 ImageButton ib = (ImageButton) findViewById(R.id.middle);
    final Bitmap theBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.middle_east);  
    ib.setImageBitmap(theBitmap);
    ib.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int eventPadTouch = event.getAction();

            switch (eventPadTouch) {

                case MotionEvent.ACTION_DOWN:
                    if (event.getX()>=0 & event.getY()>=0 & event.getX()<theBitmap.getWidth() & event.getY()<theBitmap.getHeight()) { // ** Makes sure that X and Y are not less than 0, and no more than the height and width of the image.    
                        int pixColor = theBitmap.getPixel((int)event.getX(),(int)event.getY());
                        String country = null;
                        Log.e("MainActivity", pixColor + "");
                        if (pixColor == getResources().getColor(R.color.yellow)) country = "Saudi Arabia";
                        else if (pixColor == getResources().getColor(R.color.light_green)) country = "Yemen";
                        else if (pixColor == getResources().getColor(R.color.dark_yellow)) country = "Oman";
                        else if (pixColor == getResources().getColor(R.color.banana_yellow)) country = "Syria";
                        else if (pixColor == getResources().getColor(R.color.golden_yellow)) country = "Iraq";

                        if (country != null)
                        {
                            Log.e("MainActivty", country + " " + pixColor);
                            Toast t = Toast.makeText(MainActivity.this, country, Toast.LENGTH_SHORT);
                            t.show();
                        }
                    }
                    return true;                
            }           
            return false;
        }
    });

Thanks all:)

Community
  • 1
  • 1
Evan
  • 287
  • 4
  • 14

2 Answers2

0

First the image can't be seen and Constant Value: -256 (0xffffff00) is Yellow https://developer.android.com/reference/android/graphics/Color.html use this code to convert to hex value

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

checkout this link here :

How to convert a color integer to a hex String in Android?

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
Mero
  • 622
  • 12
  • 24
  • o sorry oops I'll try and fix that now and I was assuming that was right but then light green can't be that – Evan Jul 30 '14 at 22:40
  • if it answer your question accept the answer please – Mero Jul 30 '14 at 22:59
  • it didn't there are still major problems, some countries show multiple hex values for the same country and others just show the wrong hex, I'd really like a working answer, thanks for the info though it helped see what exactly was going wrong – Evan Jul 30 '14 at 23:05
  • Found a working answer here: http://stackoverflow.com/questions/3961071/android-how-to-make-a-clickable-map-image-with-each-country-producing-a-differe thanks for the help :) – Evan Jul 30 '14 at 23:13
0

In your if statement you are using to ensure the return value iz higher than 0 you are using the bitwise '&' operator instead of the logical '&&' operator which would have the affect you want.

Let me know if this helps!

  • Thanks for the answer, I actually didn't see that :D, it didn't change the result which kind of confuses me but thanks for the fix – Evan Jul 31 '14 at 06:35