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