如何在Android上的ImageView中绘制一条线?

时间:2011-05-30 16:56:37

标签: android image imageview draw swipe

我想知道如何在用户滑动手指时在ImageView上绘制一条线?

任何机构可以解释一下吗?或者也许任何链接开始这个。

4 个答案:

答案 0 :(得分:9)

您必须拥有自己的ImageView并覆盖onDraw函数。使用类似这样的东西

public class MyImageView extends ImageView{

    public MyImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        canvas.drawLine(0, 0, 20, 20, p);

    }

}

并在您的主类中创建对象MyImageView;当您触摸显示屏时,请拨打update();功能

答案 1 :(得分:5)

这是如何在另一张图片上绘制绿色矩形的完整示例:

package CustomWidgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

/**
 * Allows to draw rectangle on ImageView.
 *
 * @author Maciej Nux Jaros
 */
public class DrawImageView extends ImageView {
    private Paint currentPaint;
    public boolean drawRect = false;
    public float left;
    public float top;
    public float right;
    public float bottom;

    public DrawImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        currentPaint = new Paint();
        currentPaint.setDither(true);
        currentPaint.setColor(0xFF00CC00);  // alpha.r.g.b
        currentPaint.setStyle(Paint.Style.STROKE);
        currentPaint.setStrokeJoin(Paint.Join.ROUND);
        currentPaint.setStrokeCap(Paint.Cap.ROUND);
        currentPaint.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (drawRect)
        {
            canvas.drawRect(left, top, right, bottom, currentPaint);
        }
    }
}

当你定义了这个时,你可以用上面的View(widget)替换ImageView,例如:

<CustomWidgets.DrawImageView
    android:id="@+id/widgetMap"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/map_small"
/>

然后您可以在控制布局的活动的触摸事件中使用它:

    mapImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            DrawImageView drawView = (DrawImageView) v;

            // set start coords
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                drawView.left = event.getX();
                drawView.top = event.getY();
            // set end coords
            } else {
                drawView.right = event.getX();
                drawView.bottom = event.getY();
            }
            // draw
            drawView.invalidate();
            drawView.drawRect = true;

            return true;
        }
    });

当然,您可以制作一些getter和setter以及其他Java过度工程例程; - )。

答案 2 :(得分:0)

为了绘制用户实际绘制的线,您必须覆盖dispatchTouchEvent。从该事件中,您可以获取线的坐标并在onDraw中绘制它们,如乔治所示。

http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent

答案 3 :(得分:0)

看一下ApiDemos样本FingerPaint

通过使用此功能,您可以通过触摸屏幕在ImageView上绘制线条。