我在应用程序中保留图形的形状时遇到问题

rwqw0loc  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(156)

我正在构建一个包含多个片段的应用程序,其中一半片段有一个自定义视图(我们称之为canvas),用户可以通过在画布上触摸和拖动手指来绘制线条。
我已经设法使用sharedpreferences来保存(用户需要在下拉菜单中单击save)并在用户返回片段时加载行(在离开另一个片段或关闭应用程序之后)。
但是,在加载时,原始图形将更改为一组线(编号等于创建原始图形所需的数量),这些线从画布的左上角开始,如果这有意义的话,几乎与阳光一样。
保存前

保存并返回碎片后

我不知道为什么它是这样加载,而不是如何修复它,但我相信这与loaddrawing方法有关。我将展示下面的代码(画布片段的代码基本相同,所以我将展示一个片段的代码。
画图.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;
}

//I believe it is here that problem lies
public void loadDrawing(Canvas canvas) {
    if (mCanvas != null) {

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

            float xTest = 0.0f;
            float yTest = 0.0f;

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

                    //need to figure out how to retain shape upon loading

                if (xTest == 0.0f && yTest == 0.0f) {
                    xTest = fp.x;
                    yTest = fp.y;

                    fp.path.quadTo(xTest, yTest, (xTest + fp.x) / 2, (yTest + fp.y) / 2);
                } else {
                    fp.path.quadTo(xTest, yTest, (xTest + fp.x) / 2, (yTest + fp.y) / 2);

                    xTest = fp.x;
                    yTest = fp.y;
                }

            }

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

            canvas.restore();
        }

    }
}
}

手指路径.java

import android.graphics.Path;

public class FingerPath{

public int color;
public boolean emboss;
public boolean blur;
public int strokeWidth;
public Path path;
public float x;
public float y;

public FingerPath(int color, boolean emboss, boolean blur, int strokeWidth, Path path, float x, float 
y) {
    this.color = color;
    this.emboss = emboss;
    this.blur = blur;
    this.strokeWidth = strokeWidth;
    this.path = path;
    this.x = x;
    this.y = y;

}

}

linelhwfragment.java文件
包含画布的片段

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class LineLHwFragment extends Fragment {

private PaintView paintView;
public ArrayList<FingerPath> lineLoad;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 
Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_line_l_hw, container, false);

        return v;

}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    paintView = view.findViewById(R.id.lineLPaintView);
    loadData();
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    paintView.init(metrics);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.normal:
            paintView.normal();
            saveData();
            return true;

        case R.id.clear:
            paintView.clear();
            return true;
    }

    return super.onOptionsItemSelected(item);
}

private void saveData() {
    SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("line test 1", 
Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson(paintView.getPaths());
    editor.putString("line test", json);
    editor.apply();
    Toast.makeText(this.getActivity(), "Data saved", Toast.LENGTH_SHORT).show();
}

private void loadData() {
    SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("line test 1", 
Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("line test", null);
    Type type = new TypeToken<ArrayList<FingerPath>>() {}.getType();

    lineLoad = gson.fromJson(json, type);
    if (lineLoad != null) {
        paintView.setPaths(lineLoad);
        Toast.makeText(this.getActivity(), "Data loaded", Toast.LENGTH_SHORT).show();
    }
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题