首页 > 解决方案 > 如何从 Bundle 中获取特定数据?

问题描述

我正在构建一个具有多个片段的应用程序,其中大约一半的片段包含一个用户可以用手指画线的视图(我们称之为画布)。我要做的是保存用户绘制的线条,然后将片段留给另一个片段并在该片段的画布上绘制一些线条,然后可以返回到前一个片段(第二个的绘图将得到已保存)该片段的绘图仅显示在画布中。如果用户返回到第二个片段,则只会显示第二个片段的绘图。我正在使用 Parcelable 接口。

我遇到的问题是来自一个片段的绘图将显示在不同的片段中,并且任何额外的线条都将显示在原始绘图的第一个片段中,我不知道如何解决这个问题。

片段 1

import android.os.Bundle;
import android.util.DisplayMetrics;
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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class ChatFragment extends Fragment {

private PaintView paintView;


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

//I tried using this if statement to get the specific data
//Same for the other fragment
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("line test")) {
            paintView.setPaths(savedInstanceState.<FingerPath>getParcelableArrayList("chat test"));
        }
    }
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    paintView.init(metrics);
    setHasOptionsMenu(true);
        return v;

}

@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();
            return true;

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

    return super.onOptionsItemSelected(item);
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("chat test", paintView.getPaths());
}

}

片段 2

import android.os.Bundle;
import android.util.DisplayMetrics;
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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class LineLHwFragment extends Fragment {

private PaintView paintView;

@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);
        paintView = v.findViewById(R.id.lineLPaintView);

//here's where I try for fragment 2
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("line test")) {
                paintView.setPaths(savedInstanceState.<FingerPath>getParcelableArrayList("line 
test"));
            }
        }
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    paintView.init(metrics);
    setHasOptionsMenu(true);
        return v;

}

@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();
            return true;

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

    return super.onOptionsItemSelected(item);
}


@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("line test", paintView.getPaths());
}


}

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.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;
private 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 Path getMPath() {
    return mPath;
}

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

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

FingerPath.java,包含 Parcelable 接口

import android.graphics.Path;
import android.os.Parcel;
import android.os.Parcelable;


public class FingerPath implements Parcelable {

public int color;
public boolean emboss;
public boolean blur;
public int strokeWidth;
public Path path;

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

}



protected FingerPath(Parcel in) {
    color = in.readInt();
    emboss = in.readByte() != 0;
    blur = in.readByte() != 0;
    strokeWidth = in.readInt();

}

public static final Creator<FingerPath> CREATOR = new Creator<FingerPath>() {
    @Override
    public FingerPath createFromParcel(Parcel in) {
        return new FingerPath(in);
    }

    @Override
    public FingerPath[] newArray(int size) {
        return new FingerPath[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(color);
    parcel.writeByte((byte) (emboss ? 1 : 0));
    parcel.writeByte((byte) (blur ? 1 : 0));
    parcel.writeInt(strokeWidth);


}




}

标签: javaandroid

解决方案


推荐阅读