0

I'm trying to make an application, which allows users draw on a bitmap by touching a screen. But my code has a bug: all the lines are displaced. For example, on the attached jpg I've tryed to outline a green square. But as a result, the red border is places almost under the green square.

enter image description here

That is an activity's onCreate, where some variables are inited:

@Override
protected void onCreate(Bundle savedInstanceState) {      
    imageView = (ImageView) findViewById(R.id.myImg);
    loadImage(); //this one places an images from the camera to the imageView
    c = new Canvas(mutableBitmap);
    imageView.draw(c);
}

Here I get user inputed coordinates and draw new line, when a user ups his/her finger:

@Override
public boolean onTouchEvent(MotionEvent event){
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        xStart = event.getX();
        yStart = event.getY();
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        xEnd = event.getX();
        yEnd = event.getY();
       drawMyLine(xStart, yStart, xEnd, yEnd);
    }
    return super.onTouchEvent(event);
}

Thant is a method for drawing a line:

private void drawMyLine(float x, float y, float xend, float yend) {
    Paint p = new Paint();
    p.setStrokeWidth(3.7f);
    p.setColor(Color.RED);
    c.drawLine(x, y, xend, yend, p);
    imageView.setImageBitmap(mutableBitmap);
}
Victoria Agafonova
  • 2,048
  • 10
  • 33
  • 50
  • Does your `imageView` have any layout settings such as a padding or margin? Where exactly is the image view located in your layout? You should probably be doing that in a custom view, that overrides the `onDraw` method. There is actually no need to use an `ImageView` for that. – tiguchi Apr 21 '15 at 17:12
  • Take a look at the FingerPaint demo from the Android SDK: http://stackoverflow.com/questions/24164334/where-to-find-android-fingerpaint-demo-android-studio-era – tiguchi Apr 21 '15 at 17:15
  • use a Matrix, get it by calling getImageMatrix() – pskink Apr 21 '15 at 17:18

0 Answers0