如何在Android上将行开始和结束点锚定到ImageView?

时间:2015-12-02 13:18:16

标签: android

我有两个ImageView s(1,2),当我从位置1移动Path时,我需要按行(ImageView类认为)将它们相互连接起来3行必须动态更改并锚定到ImageView s

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以这样做:

我使用扩展PaintView的自定义SurfaceView。在此课程中,您必须扩展touchMovetouchStarttouchUp等方法。您必须将一些初始变量作为起始点,当您调用方法touchUp时,它将连接到最后一个点,并且您必须具有包含“绘制”调用的Canvas类。在这种情况下,它以touchUp方式连接,您可以从第一个initXinitY点到X和Y坐标或路径数组中的最后一点绘制线条。

public class PaintView extends SurfaceView
{
    protected boolean changed = false;
    protected View layout;
    protected int progress;
    private PaintView pv;
    protected float stroke = 6.0F;
    private Paint bmpPaint;
    private Canvas canvas;
    private int colour;
    private Context context;
    private float mX;
    private float mY;
    private float initX;
    private float initY;
    private ArrayList<Float> xCoords;
    private ArrayList<Float> yCoords;

    private void touchMove(float paramFloat1, float paramFloat2)
    {
        float f1 = Math.abs(paramFloat1 - this.mX);
        float f2 = Math.abs(paramFloat2 - this.mY);
        if ((f1 >= 1.8F) || (f2 >= 1.8F))
        {
            this.path.quadTo(this.mX, this.mY, (paramFloat1 + this.mX) / 2.0F, (paramFloat2 + this.mY) / 2.0F);
            this.mX = paramFloat1;
            this.mY = paramFloat2;

            xCoords.add(paramFloat1);
            yCoords.add(paramFloat2);
        }
    }

    private void touchStart(float paramFloat1, float paramFloat2)
    {
        this.clear();
        this.path.moveTo(paramFloat1, paramFloat2);
        this.mX = paramFloat1;
        this.mY = paramFloat2;
        this.initX = paramFloat1;
        this.initY = paramFloat2;
        xCoords.add(paramFloat1);
        yCoords.add(paramFloat2);
    }

    private void touchUp()
    {
        this.path.lineTo(this.initX, this.initY);
        this.canvas.drawPath(this.path, this.paint);            
        this.clear();
    }

    public void clear()
    {
        this.path.reset();
        this.canvas.drawColor(Color.TRANSPARENT);
        if (this.bgImage != null) {
            this.canvas.drawBitmap(this.bgImage, 0.0F, 0.0F, null);
        }
        invalidate();
    }

    public ArrayList<Float> getXCoords(){
        return xCoords;
    }

    public ArrayList<Float> getYCoords(){
        return yCoords;
    }
    protected void onDraw(Canvas paramCanvas)
    {
        paramCanvas.drawColor(Color.TRANSPARENT);
        paramCanvas.drawBitmap(this.bgImage, 0.0F, 0.0F, null);
        paramCanvas.drawBitmap(this.bmp, 0.0F, 0.0F, null);
        paramCanvas.drawPath(this.path, this.paint);
    }
}

最终看起来像这样:

draw