0

I have a problem, I have tried resolve the problem but I haven't found a solution.

I have two columns of images. I want to join them through the midpoint of each image. The problem I have is that the attachment point moves down, like the image example of my problem

I have a "main" class and I have the internal class: public class DrawView extends LinearLayout with the atribute: private Paint paint = new Paint(); and I set the next values: paint.setColor(Color.BLACK); paint.setStrokeWidth(6);

I use the next code for draw the lines:

public void onDraw(Canvas canvas) {
    }

    @SuppressLint("UseValueOf")
    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (activateDraw) {
            for (int i = 0; i < 5; i++) {
                             //I not include the color selection.
                    x1= Image[i].x + Image[i].width;
                    y1=Image[i].y+ (new Double(Image[i].height / 2).intValue()));

                    x2=ImagePr[i].x;
                    y2=ImagePr[i].y + (new Double((ImagePr[i].height) / 2).intValue()));
                    canvas.drawLine(x1, y1, x2, y2, paint);
            }
            activateDraw = false;
        }
    }

To set the x and y values I use the method:

public void setData(ImageView img) {
    image = img;
    int[] values = new int[2];
    image.getLocationInWindow(values);
    x = values[0];
    y = values[1];
    width = image.getWidth();
    height = image.getHeight();
}

In the main class I have the atribute: Canvas auxCanvas = new Canvas(); and I execute the onDraw(auxCanvas) method when I want draw the lines. Why the lines don't draw joining the "midpoints"?

Anyone can help me?Thanks!!

@Shaunak Sorry, it was a fail. I've removed it and it doesn't affect, the problem continues. Thank you!

@anthropomo I tried your change but the problem continues.

I don't understand why in the emulator seems to work fine, but not on the device.

SOLUTION:

(I thought I had written the answer, sorry) The solution was very simple. The app is destinated to students that have 6-8 years, so I decided to hide the status bar and the above code works perfect without do changes! Hide the status bar: Hide Notification bar

How to hide the title bar for an Activity in XML with existing custom theme

If other people want to show the status bar, I suppose you need to subtract the status bar height.

Community
  • 1
  • 1
Adrian
  • 336
  • 6
  • 22

1 Answers1

1

reference: http://developer.android.com/reference/android/util/DisplayMetrics.html#density

does something like this work for you?:

float d = getResources().getDisplayMetrics().density;
canvas.drawLine(x1*d, y1*d, x2*d, y2*d, paint);

note: if the multiplication doesn't work try dividing by d... i can never remember what to do.

John Boker
  • 82,559
  • 17
  • 97
  • 130
  • Thanks for your comment =) I searched information about the density and it seems to be what I need. I've seen the next two posts: http://stackoverflow.com/questions/14391387/android-subpixel-rendering http://stackoverflow.com/questions/10282151/android-drawing-on-the-different-devices But I haven't found something useful, or I haven't able to resolve my problem. In these posts they convert units, but I don't need it because I get the pixels with the methods getWidth and getHeight. – Adrian Feb 03 '13 at 15:53