tags:

views:

946

answers:

1

Basic description of what I'm trying to do: Load up a background image, paint some text into a new bitmap layer, position that text over the background image where I want it to show up, then save the background image with the new layer on top of it where I had positioned it.

Problem: The problem I'm having is that when I paint the text over the background image it ends up in the wrong x/y position.

For example: 1. I load in a 640x480 image with the ScaleType set to Fit_Start (which shrinks it to 1/2 the size). I'm keeping track of the ratio and using it when painting the text to the final image.

  1. Then I create a blank overlay bitmap, paint some text onto it, trim the extra transparent space off, and place it into another imageview (ScaleType of Matrix)

  2. Then I position that new imageview using my mouse and the onTouchEvent method. I keep track of the X and Y:

    float X = event.getX();

    float Y = event.getY() - 50; // Title bar, status bar

    overlayView.layout((int)X, (int)Y, overlay.getWidth() + (int)X, overlay.getHeight() + (int)Y);

  3. Then I attempt to write the text to the final image (background image) using the X & Y except that I double the X & Y since I know that the image was shrunk in half to fit the screen.

  4. Final result is the text being written to the wrong location. It ends up being slightly too much X and not enough Y (like 25+ depending on the background image scale -- this varies).

Somewhere my math is wrong or I'm forgetting to add something into the formula for calculating the correct position on the background where the text should be painted to. I've been working on this for quite a few hours and haven't been able to pin-pointed the problem.

The last thing I tried was to take into account the title bar and status bar, which on the simulator takes up like 50 pixels. So, I'm subtracting that from Y, which is done when dragging. But that still doesn't correct Y being off different amounts depending on the background image dimensions.

Anyone have any ideas on what I'm doing wrong?

A: 

i don't think that is your problem yet, but i think you need to account for device density. Cause title bar + status bar not gonna be 50 px it's 50 dip

Alex Volovoy
Also, you can't rely on the status bar being exactly 50dp on all devices. I'm sure it's not as much as that on the emulator anyway(?)
Christopher
good point. I'd start calculation from your first layout size. However it doesn't explain x difference. Do you have any padding on your views ? As size: the title bar along is 26 dip + notification bar.
Alex Volovoy
Thanks for the info on the title bar and notification bar. I actually ended up solving my problem, although I'm not 100% sure at this point what I did since I've made so many changes. Thanks for the help!
Greg