skip to Main Content

I made a treasure map in photoshop and I made a transparent image with coloured hotspots to put over the treasure map so I can make it clickable.

When I click on the colored dots (that are invisible), Android detects the color clicked and does the appropriate methods, just as asked.

Now I have an imageview, that would be my player, and each day I want it to move to another colored hotspot on the map (each hotspot represents a day of the week).

I have this code, but the position is way off:

private void moveToColor(ImageView iv, int toColor) {

    Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.hotspots);   
    int width = bm.getWidth();
    int height = bm.getHeight();
    int[] pixels = new int[width * height];
    bm.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int ix = 0; ix < width; ++ix) {
        for (int iy = 0; iy < height; ++iy) {   
            if (toColor == bm.getPixel(ix, iy)) {
                iv.animate().translationX((float)ix);
                iv.animate().translationY((float)iy);
                return;
            }
        }
    }       
 }

sometimes it will move the imageview close to the toColor, and other times it is completely off or not even on the map.

Any pointers on how I could do this. I tried it with a buffer copypixelstobuffer, but I didn’t understand very well how that works. Because above is quite slow..

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    I actually found that if I change

    Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.hotspots);   
    int width = bm.getWidth();
    int height = bm.getHeight();
    

    to this:

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.hotspots);
    
    bm = Bitmap.createScaledBitmap(bm,size.x, size.y, true);
    int width = bm.getWidth();
    int height = bm.getHeight();
    

    where size.x and size.y come from

    display = getWindowManager().getDefaultDisplay();
    size = new Point();
    display.getSize(size);
    

    Then the position is not that far off. It is still not completely where I want it, the player is positioned on the bottom of the dot, but at least it is a step closer.


  2. Sure it is off, read the description of the translationX property carefully: http://developer.android.com/reference/android/view/View.html#attr_android:translationX

    “This value is added post-layout to the left property of the view”

    It’s relative to the left of the view, i.e. it’s displacement, not absolute position.
    Instead, use

    iv.animate().x((float)ix);
    

    and it’s twin brother .y()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search