Image mapping in java

Put your problem here if it does not fit any of the other categories.

Image mapping in java

Postby nin2246 » Thu Jan 26, 2012 6:48 pm

I don't know what it would be called in Java, I know in HTML Image Maps are used. Is that possible in Java? Basicly what I am asking is when the user clicks on the background somewhere, the animal, or unit will move there. I seen it done before like on wallpapers with animals and tactical games. But what would the source code look like? A link or tutorial would be better.
Posts: 42
Joined: Wed Jan 18, 2012 8:08 pm


Re: Image mapping in java

Postby Phyll » Thu Jan 26, 2012 9:46 pm

Hi nin2246,

Here's some code that lets you move a bitmap around with your finger:

Code: Select all
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class HeadFloat extends Activity {

   public Bitmap head = Bitmap.createBitmap(251,283, Bitmap.Config.ARGB_8888);
   public Canvas headCanvas = new Canvas(head);
   private SampleView sView;
    private static int displayWidth = 100;    //movement area
    private static int displayHeight = 100;
    Matrix mtx = new Matrix();
    public void onCreate(Bundle icicle) {
        head = BitmapFactory.decodeResource(getResources(),R.drawable.small_head);
        sView = new SampleView(this);
        double x1 = 10;
        double x2 = 12;
        double y1 = 10;
        double y2 = 12;

        double dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1- y2, 2));
        Log.e("DOUBLE","Math "+dist);


    private class SampleView extends View {
        private Rect displayRect = null; //rect we display to
        private int scrollRectX = 0; //current left location of scroll rect
        private int scrollRectY = 0; //current top location of scroll rect
        private float scrollByX = 0; //scroll by amounts
        private float scrollByY = 0;
        private float startX = 0;   //track x from one ACTION_MOVE to the next
        private float startY = 0;   //track y from one ACTION_MOVE to the next
        private int state = 0;
        public SampleView(Context context) {
            displayRect = new Rect(0, 0, displayWidth, displayHeight);
    public boolean onTouchEvent(MotionEvent event) {
        float x;
        float y;
            switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:  //Initial down event location.
                            startX = event.getRawX();
                            startY = event.getRawY()-50;
                            Log.e("TOUCHED",startY+" "+(scrollRectY+displayHeight));
                            if (((startX>scrollRectX)&(startX<(scrollRectX+displayWidth)))&
                               isInImage()) state = 1;
                            Log.e("TOUCHED","State "+state);
                    case MotionEvent.ACTION_MOVE:
                            x = event.getRawX();
                            y = event.getRawY()-50;                           
                            scrollByX = x - startX;
                            scrollByY = y - startY;
                            startX = x;
                            startY = y;
                            if (state != 0) invalidate(); //move it
                    case MotionEvent.ACTION_UP:
                            x = event.getRawX();
                            y = event.getRawY()-50;
                            scrollByX = x - startX;
                            scrollByY = y - startY;
                            startX = x;
                            startY = y;
                            state = 0;
            return true;

    private boolean isInImage() {
       Bitmap tmp = Bitmap.createScaledBitmap(head, displayWidth, displayHeight, false);
        int x = (int) ((scrollRectX+displayWidth)-startX);
       int y = (int) ((scrollRectY+displayHeight)-startY);
       int c = 0;
       try {
           c = tmp.getPixel(x, y);
            } catch (IllegalArgumentException e) {
                Log.e("ALPHA","Illegal Point"); } 
       if (Color.alpha(c)>128) return true;
       return false;

    protected void onDraw(Canvas canvas) {

       Log.e("TOUCHED",startX+" "+startY);
       scrollRectX = scrollRectX+(int)scrollByX; 
       scrollRectY = scrollRectY+(int)scrollByY;
        Paint paint = new Paint();
        if (state == 1) paint.setAlpha(128);
        //To rotate it as you go
        //float dx = 100.0f;
       //float dy = 500.0f;       
       //angle = angle + 90;
       //if (angle>270) angle = 0;
        //mtx.setTranslate(dx, dy);
        //Bitmap bMapRotate = Bitmap.createBitmap(head, 0, 0, head.getWidth(), head.getHeight(), mtx, true);
        //canvas.drawBitmap(bMapRotate, null, displayRect, paint);
        canvas.drawBitmap(head, null, displayRect, paint);
    }//sampleview class

    //TODO: Fill In Methods Etc.

No special layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

You just need to give it a bitmap. Shouldn't take too much to make it move the bitmap to your finger.

Master Developer
Master Developer
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 20 guests