Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:59:54+00:00 2026-06-14T07:59:54+00:00

I am trying to gain some more familiarity with the Android SurfaceView class, and

  • 0

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

The Problem

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

The .png and the texture region are always off!

The Code

Here, I shall provide the relevant code excerpts to help answer my question.

CustomSurface.java

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

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);
}

This method encapsulates how I capture touch events:

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;
}

Graphic.java

This method is used to construct the Graphic:

//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();
}

This method detects if a user is clicking inside the image:

public boolean contains(int x, int y){

    if(x >= left && x <= right){

        if(y >= top && y <= bottom){

            return true;
        }
    }

    return false;
}

This method is used to move the graphic:

public void move(int x, int y){

    left = x;
    top = y;
    right = x + resource.getWidth();
    bottom = y + resource.getHeight();
}

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

public int getCenterX(){

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

public int getCenterY(){

    return (bottom - top) / 2 + top;
}

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:59:55+00:00Added an answer on June 14, 2026 at 7:59 am

    This issue ended up being much simpler than I had thought, and after some tweaking I realized that this was an issue of image width compensation.

    This line in the above code is where the error stems from:

    c.drawBitmap(g.getResource(), g.getCenterX(), g.getCenterY(), null);
    

    As you can tell, I manipulated the coordinates from within the Graphic class to produce the center of the bitmap, and then called canvas.drawBitmap() assuming that it would draw from the center outward.

    Obviously, this would not work because the canvas always drops from the top left of an image downwards and to the right, so the solution was simple.

    The Solution

    Create the touch region with regards to the touch location, but draw it relative to a distance equal to the image width subtracted from the center location in the x and y directions. I basically changed the architecture of the Graphic class to implement a getDrawX() and getDrawY() method that would return the modified x and y coordinates of where it should be drawn in order to have the center_x and center_y values (determined in the constructor) actually appear to be at the center of the region.

    It all comes down to the fact that in an attempt to compensate for the way the canvas draws bitmaps, I unfortunately incorporated some bad behaviors and in the end had to handle the offset in a completely different way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute some queries using YQL. To gain some efficiency, I
I was trying to compile some code that uses Boost (1.49), with Clang(& libc++)
I'm trying to refactor some code here that was done previously by other guys,
I'm investigating since some days box-shadow and text-shadow. I'm trying to gain the following
I have a simple Rails application with which I am trying to use the
I am trying gain more understanding of Linq queries and Entity Framework (4.1). Please
So I am trying to make a JSON object that should hold some information
I am new to android world and just started with some examples to gain
I'm trying to gain some memory saving in a C++ program and I want
I am a newbie in assembly and still trying to gain some basic foundation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.