首页 > 解决方案 > 使用 Path 类时有什么方法可以获取线的端点?

问题描述

我正在构建一个应用程序,用户可以使用Path该类在自定义画布中用手指画线(多个片段有一个画布,这很有效)。

我还设法使用 SharedPreferences 来保存和加载绘制的线条,但是加载时线条从左上角开始(即 (0, 0)),并且形状已更改为开始时带有轻微曲线的线条(我说开始是因为我发现这条线从触摸开始的地方结束)。

起点被保留,Path但据我所见,没有保留终点。有什么办法可以获得端点吗?

我之前曾尝试将所需的变量传递给另一个 ArrayList,该 ArrayList 使用另一个带有端点的构造函数(使用用于手指停止触摸屏幕时的方法找到),但绘图不再像以前那样显示在画布上。

编辑

我已更改为查找多个点,因为我认为仅获取端点还不够,并更改了显示的代码以显示我对 getPosTan 的尝试,但testing由于某种原因中的元素为空,因此没有显示任何图纸,因此它不会不要进去。

更新

我发现 pathMeasure.getLength() 产生 0.0 所以它不会进入 while 并因此导致 null 元素但我不知道为什么它产生 0.0 因为 somePath 不是 null

PaintView.java

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;



public class PaintView extends View {

public static int BRUSH_SIZE = 10;
public static final int DEFAULT_COLOR = Color.WHITE;
public static int DEFAULT_BG_COLOR = Color.GRAY;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
private Path mPath;
private Paint mPaint;
private static ArrayList<FingerPath> paths = new ArrayList<>();
private int currentColor;
private int backgroundColor = DEFAULT_BG_COLOR;
private int strokeWidth;
private boolean emboss;
private boolean blur;
private MaskFilter mEmboss;
private MaskFilter mBlur;
private Bitmap mBitmap;
public Canvas mCanvas;
private Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);



public PaintView(Context context) {
    this(context, null);

}

public PaintView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);

    mPaint.setColor(DEFAULT_COLOR);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setXfermode(null);
    mPaint.setAlpha(0xff);

    mEmboss = new EmbossMaskFilter(new float[] {1, 1, 1}, 0.4f, 6, 3.5f);
    mBlur = new BlurMaskFilter(5, BlurMaskFilter.Blur.NORMAL);

}

public ArrayList getPaths() {
    return paths;

}

public ArrayList setPaths(ArrayList<FingerPath> list) {


    return this.paths = list;
}

public void init(DisplayMetrics metrics) {
    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

    mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);

    loadDrawing(mCanvas);

    currentColor = DEFAULT_COLOR;
    strokeWidth = BRUSH_SIZE;
}

public void normal() {
    emboss = false;
    blur = false;

}



public void clear() {
    backgroundColor = DEFAULT_BG_COLOR;
    paths.clear();
    normal();
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    mCanvas.drawColor(backgroundColor);

        for (FingerPath fp : paths) {

            mPaint.setColor(fp.color);
            mPaint.setStrokeWidth(fp.strokeWidth);
            mPaint.setMaskFilter(null);

            if (fp.emboss)
                mPaint.setMaskFilter(mEmboss);
            else if (fp.blur)
                mPaint.setMaskFilter(mBlur);

            mCanvas.drawPath(fp.path, mPaint);
        }




    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

    canvas.restore();
}

private void touchStart(float x, float y) {
    mPath = new Path();
    FingerPath fp = new FingerPath(currentColor, emboss, blur, strokeWidth, mPath, x, y);

    paths.add(fp);

    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touchMove(float x, float y) {
    float dx = Math.abs(x-mX);
    float dy = Math.abs(y-mY);

    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touchUp() {mPath.lineTo(mX, mY);}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN :
            touchStart(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE :
            touchMove(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP :
            touchUp();
            invalidate();
            break;
    }

    return true;
}

private FloatPoint[] getPoint(Path somePath, float x, float y){
    FloatPoint[] pointArray = new FloatPoint[20];
    PathMeasure pathMeasure = new PathMeasure(somePath, false);
    float length = pathMeasure.getLength();
    float distance = 0f;
    float speed = length / 20;
    int counter = 0;
    float[] aCoordinates = {x, y};

    while ((distance < length) && (counter < 20)) {
        pathMeasure.getPosTan(distance, aCoordinates, null);
        pointArray[counter] = new FloatPoint(aCoordinates[0], 
aCoordinates[1]);
        counter++;
        distance = distance + speed;
    }



    return pointArray;
}

public void loadDrawing(Canvas canvas) {
    if (mCanvas != null) {


        currentColor = DEFAULT_COLOR;
        strokeWidth = BRUSH_SIZE;
        if (! paths.isEmpty()) {
            canvas.save();
            mCanvas.drawColor(backgroundColor);




            for (FingerPath fp : paths) {
                mPaint.setColor(fp.color);
                mPaint.setStrokeWidth(fp.strokeWidth);
                mPaint.setMaskFilter(null);

                if (fp.emboss)
                    mPaint.setMaskFilter(mEmboss);
                else if (fp.blur)
                    mPaint.setMaskFilter(mBlur);

                  

                FloatPoint[] testing = getPoint(fp.path, fp.x, fp.y);

                    //need to figure out how to for loop testing
                float sectionTestX = 0.0f;
                float sectionTestY = 0.0f;

                for (FloatPoint testingPoint : testing) {
                    if (sectionTestX == 0.0f && sectionTestY == 0.0f) {
                        sectionTestX = testingPoint.getX();
                        sectionTestY = testingPoint.getY();
                        continue;
                    } else {

                        fp.path.quadTo(sectionTestX, sectionTestY, 
testingPoint.getX(), testingPoint.getY());

                        sectionTestX = testingPoint.getX();
                        sectionTestY = testingPoint.getY();
                    }

                }

                    /*xTest = fp.x;
                    yTest = fp.y;*/

            }




            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

            canvas.restore();
        }

    }
}




}

浮点数.java

public class FloatPoint {

static float x, y;

public FloatPoint(float x, float y) {
    this.x = x;
    this.y = y;
}

public static float getX() {
    return x;
}

public static float getY() {
    return y;
}
}

标签: javaandroidandroid-studio

解决方案


推荐阅读