2

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 2

0

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()

12
  • I tried that but it does exactly the same. And overall it's really slow, I see a white screen for about 2-3 seconds before the treasure map with the player is loaded. I hope to find a more efficient way to do this!
    – Iris_vdz
    Apr 11, 2014 at 20:43
  • well, the slowness might be due to animation -- do you need to animate it? Apr 11, 2014 at 20:47
  • As for x() positioning -- there's no way it does exactly the same as translateX() :) But I'm guessing it might still be off because your image's pixel position is in image's coordinates, while x() is in screen pixel coords. Those two are not equal -- image may be scaled. Also, to avoid animation, use iv.setX() Apr 11, 2014 at 20:51
  • setX and setY do resolve the white screen issue.. I do have some trouble with the difference in pixel coordinates, I imagine it is something like that but I have no idea how to get the correct ones...
    – Iris_vdz
    Apr 11, 2014 at 20:52
  • Either don't scale the image when displaying in ImageView, or use ImageView.getScaleX/Y() to get the screen pixel / image pixel ratio and calculate accordingly. Also, I assume you do account for the image width? setX() positions the view by it's left edge. So you need to subtract the view's width if you want to put view's center on the spot instead. Apr 11, 2014 at 20:59
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.