简体   繁体   English

在OnTouchListener的画布上绘图

[英]Drawing on a canvas in OnTouchListener

I try to draw a line with finger move on an ImageView with a bitmap inside using the setOnTouchListener() method. 我尝试使用setOnTouchListener()方法在具有位图的ImageView上用手指移动画一条线。

Here is the code. 这是代码。

@Override
public boolean onTouch(View view, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            downx = event.getX();
            downy = event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            upx = event.getX();
            upy = event.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            photoView.invalidate();
            break;
        case MotionEvent.ACTION_UP:
            canvas.drawBitmap(notChangedRotatedBitmap, new Matrix(), paint);
            photoView.invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            break;
        default:
            break;
    }
    return true;
}

The problem is that it lookes like the coordinates returned by the event object is multiplied by 2, because if I start my finger move near the left top corner it starts almost below my finger, but when I move my finger away from the top left corner the line begin to grow much faster then my move (near two times faster) and soon goes over the borders of the view. 问题是事件对象返回的坐标看起来像是2乘以,因为如果我开始将手指移到左上角附近,则它几乎会在手指的下方开始,但是当我将手指移离左上角时,线条开始比我移动快得多(快了将近两倍),并且很快越过了视图边界。 On the other hand, if I start my finger move near the right bottom corner it just don't show until I cross the center of the image moving towards the left top corner. 另一方面,如果我开始在右下角附近移动手指,则直到我越过图像的中心向左上角移动时才显示。

UPD> Besides, I use this attributes on Imageview: UPD>此外,我在Imageview上使用以下属性:

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"

It seems, that the bitmap you draw on is not the same size as the ImageView. 您所绘制的位图似乎与ImageView的大小不同。 So you paint on the bitmap using the event coordinates and when the bitmap is displayed, it's scaled to a different size due to the scaleType="fitCenter" attribute. 因此,您可以使用事件坐标scaleType="fitCenter"图上绘画,并且在显示位图时,由于scaleType="fitCenter"属性,它会缩放为其他大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM