Mapping A "Touch Region" to a Bitmap
<p>I am trying to gain some more familiarity with the Android <code>SurfaceView</code> class, and in doing so am attempting to create a simple application that allows a user to move a <code>Bitmap</code> around the screen.  The troublesome part of this implementation is that I am also including the functionality that the user may drag the image <strong>again</strong> after it has been placed.  In order to do this, I am mapping the bitmap to a simple set of coordinates that define the <code>Bitmap</code>'s current location.  The region I am mapping the image to, however, does not match up with the image.</p>

<h1>The Problem</h1>

<p>After placing an image on the <code>SurfaceView</code> using <code>canvas.drawBitmap()</code>, and recording the coordinates of the placed image, the mapping system that I have set up misinterprets the <code>Bitmap</code>'s coordinates somehow and does not display correctly.  As you can see in this image, I have simply used <code>canvas.drawLine()</code> to draw lines representing the space of my touch region, and the image is always off and to the right:</p>

<p><img src="http://i.stack.imgur.com/0sdVO.png" alt="The .png and the texture region are always off!"></p>

<h1>The Code</h1>

<p>Here, I shall provide the relevant code excerpts to help answer my question.</p>

<h2>CustomSurface.java</h2>

<p>This method encapsulates the drawing of the objects onto the canvas.  The comments clarify each element:  </p>

<pre><code>public void onDraw(Canvas c){

    //Simple black paint
    Paint paint = new Paint();

    //Draw a white background
    c.drawColor(Color.WHITE);

    //Draw the bitmap at the coordinates
    c.drawBitmap(g.getResource(), g.getCenterX(), g.getCenterY(), null);

    //Draws the actual surface that is receiving touch input
    c.drawLine(g.left, g.top, g.right, g.top, paint);
    c.drawLine(g.right, g.top, g.right, g.bottom, paint);
    c.drawLine(g.right, g.bottom, g.left, g.bottom, paint);
    c.drawLine(g.left, g.bottom, g.left, g.top, paint);
}
</code></pre>

<p>This method encapsulates how I capture touch events:</p>

<pre><code>public boolean onTouchEvent(MotionEvent e){

    switch(e.getAction()){

        case MotionEvent.ACTION_DOWN:{

            if(g.contains((int) e.getX(), (int) e.getY()))
                item_selected = true;
            break;
        }
        case MotionEvent.ACTION_MOVE:{

            if(item_selected)      
                g.move((int) e.getX(), (int) e.getY());

            break;
        }
        case MotionEvent.ACTION_UP:{

            item_selected = false;
            break;
        }
        default:{

            //Do nothing
            break;
        }
    }

    return true;
}
</code></pre>

<h2>Graphic.java</h2>

<p>This method is used to construct the Graphic:</p>

<pre><code>//Initializes the graphic assuming the coordinate is in the upper left corner
public Graphic(Bitmap image, int start_x, int start_y){

    resource = image;
    left = start_x;
    top = start_y;
    right = start_x + image.getWidth();
    bottom = start_y + image.getHeight();
}
</code></pre>

<p>This method detects if a user is clicking inside the image:</p>

<pre><code>public boolean contains(int x, int y){

    if(x &gt;= left &amp;&amp; x &lt;= right){

        if(y &gt;= top &amp;&amp; y &lt;= bottom){

            return true;
        }
    }

    return false;
}
</code></pre>

<p>This method is used to move the graphic:</p>

<pre><code>public void move(int x, int y){

    left = x;
    top = y;
    right = x + resource.getWidth();
    bottom = y + resource.getHeight();
}
</code></pre>

<p>I also have 2 methods that determine the center of the region (used for redrawing):</p>

<pre><code>public int getCenterX(){

    return (right - left) / 2 + left;
}

public int getCenterY(){

    return (bottom - top) / 2 + top;
}
</code></pre>

<p>Any help would be greatly appreciated, I feel as though many other StackOverflow users could really benefit from a solution to this issue.</p>
