我正在尝试找到一种方法来保存我的路径并在重新启动应用时再次加载它。
我可以通过从文件中检索来检索Path的值。但遗憾的是,该路径未显示在我的应用中。
我曾尝试使用invalidate()
再次刷新视图,但没有任何变化。
你能给我一些解决这个问题的建议吗?
非常感谢。
// used to determine whether user moved a finger enough to draw again
private static final float TOUCH_TOLERANCE = 10;
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private CustomPaint mPaint; // use to draw bitmap onto screen
private CustomPaint mPaintScreen;
// private CustomPath mPath; // used to draw lines onto bitmap
// private ArrayList<Path> paths = new ArrayList<Path>();
// private ArrayList<Path> r_paths = new ArrayList<Path>();
private ArrayList<CustomPath> undonePaths = new ArrayList<CustomPath>();
private int patternID;
private float[] intervals = new float[] { 50.0f, 20.0f };
private float phase = 0;
private DashPathEffect dashPathEffect;
private byte[] byteArray;
private ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
private Map<CustomPath, Integer> colorsMap = new HashMap<CustomPath, Integer>();
private Map<CustomPath, Float> widthsMap = new HashMap<CustomPath, Float>();
public static int selectedcolor;
public static float selectedwidth;
private CustomPath customPath;
private ArrayList<CustomPath> cPath = new ArrayList<CustomPath>();
// DoodleView constructor initializes the DoodleView
public DoodleView(Context context, AttributeSet attrs) {
super(context, attrs); // pass context to View's constructor
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
dashPathEffect = new DashPathEffect(intervals, phase);
// set the initial display settings for the painted line
mPaint = new CustomPaint();
mPaint.setAntiAlias(true); // smooth edges of drawn line
mPaint.setColor(Color.BLUE); // default color is black
mPaint.setStyle(Paint.Style.STROKE); // solid line
mPaint.setStrokeWidth(5); // set the default line width
mPaint.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
bitmapCanvas = new Canvas();
customPath = new CustomPath();
mPaintScreen = new CustomPaint();
selectedcolor = mPaint.getColor();
selectedwidth = mPaint.getStrokeWidth();
// paths.add(mPath);
} // end DoodleView constructor
// Method onSizeChanged creates BitMap and Canvas after app displays
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
Resources res = getResources();
bitmap = BitmapFactory.decodeResource(res, R.drawable.court);
bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(bitmap);
} // end method onSizeChanged
// clear the painting
public void clear() {
System.out.println(colorsMap);
System.out.println(widthsMap);
System.out.println(cPath);
cPath.clear();
invalidate();
} // end method clear
// set the painted line's color
public void setDrawingColor(int color) {
mPaint.setColor(color);
} // end method setDrawingColor
// return the painted line's color
public int getDrawingColor() {
return mPaint.getColor();
} // end method getDrawingColor
// set the painted line's width
public void setLineWidth(int width) {
mPaint.setStrokeWidth(width);
} // end method setLineWidth
// return the painted line's width
public int getLineWidth() {
return (int) mPaint.getStrokeWidth();
} // end method getLineWidth
// public void settingClear() {
// mPaint.setColor(Color.BLACK); // default color is black
// mPaint.setStrokeWidth(5); // set the default line width
// }
public byte[] getBitmap() {
Bitmap bmp = takeScreenshot();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
public void setPathPattern(int pattern) {
patternID = pattern;
if (patternID == 2)
mPaint.setPathEffect(dashPathEffect);
}
private float mX, mY;
private PointF startPoint;
private PointF endPoint;
// called each time this View is drawn
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, 0, 0, mPaintScreen);
for (int p = 0; p < cPath.size(); p++) {
mPaint.setColor(colorsMap.get(cPath.get(p)));
mPaint.setStrokeWidth(widthsMap.get(cPath.get(p)));
canvas.drawPath(cPath.get(p), mPaint);
}
mPaint.setColor(selectedcolor);
canvas.drawPath(customPath, mPaint);
System.out.print(cPath);
} // end method onDraw
private void touch_start(float x, float y) {
// undonePaths.clear();
customPath.reset();
// paths.add(mPath);
customPath.moveTo(x, y);
mX = x;
mY = y;
if (patternID == 1) {
startPoint = new PointF(x, y);
endPoint = new PointF();
}
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (patternID == 0 || patternID == 2) {
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
customPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
} else {
customPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
// mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
endPoint.x = x;
endPoint.y = y;
}
}
private void touch_up() {
if (patternID == 0) {
customPath.lineTo(mX, mY);
// // commit the path to our offscreen
// bitmapCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
cPath.add(customPath);
// r_paths.add(mPath);
colorsMap.put(customPath, selectedcolor);
widthsMap.put(customPath, selectedwidth);
customPath = new CustomPath();
customPath.reset();
invalidate();
} else if (patternID == 1) {
customPath.lineTo(mX, mY);
float deltaX = endPoint.x - startPoint.x;
float deltaY = endPoint.y - startPoint.y;
float frac = (float) 0.1;
float point_x_1 = startPoint.x
+ ((1 - frac) * deltaX + frac * deltaY);
float point_y_1 = startPoint.y
+ ((1 - frac) * deltaY - frac * deltaX);
float point_x_2 = endPoint.x;
float point_y_2 = endPoint.y;
float point_x_3 = startPoint.x
+ ((1 - frac) * deltaX - frac * deltaY);
float point_y_3 = startPoint.y
+ ((1 - frac) * deltaY + frac * deltaX);
customPath.moveTo(point_x_1, point_y_1);
customPath.lineTo(point_x_2, point_y_2);
customPath.lineTo(point_x_3, point_y_3);
customPath.lineTo(point_x_1, point_y_1);
customPath.lineTo(point_x_1, point_y_1);
bitmapCanvas.drawPath(customPath, mPaint);
cPath.add(customPath);
// r_paths.add(mPath);
customPath = new CustomPath();
endPoint.x = mX;
endPoint.y = mY;
} else if (patternID == 4) {
customPath.moveTo(mX, mY);
customPath.lineTo(mX + 15, mY + 15);
customPath.lineTo(mX - 15, mY - 15);
customPath.lineTo(mX + 15, mY - 15);
customPath.lineTo(mX - 15, mY + 15);
cPath.add(customPath);
// r_paths.add(mPath);
customPath = new CustomPath();
} else {
bitmapCanvas.drawCircle(mX, mY, 20, mPaint);
cPath.add(customPath);
// r_paths.add(mPath);
customPath = new CustomPath();
}
}
public void onClickUndo() {
if (cPath.size() > 0) {
undonePaths.add(cPath.remove(cPath.size() - 1));
invalidate();
} else {
}
// toast the user
}
public void onClickRedo() {
if (undonePaths.size() > 0) {
cPath.add(undonePaths.remove(undonePaths.size() - 1));
invalidate();
} else {
}
// toast the user
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
selectedcolor = mPaint.getColor();
selectedwidth = mPaint.getStrokeWidth();
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
// save the current image to the Gallery
public void saveImage() {
// use "Doodlz" followed by current time as the image file name
String fileName = "Doodlz" + System.currentTimeMillis();
// create a ContentValues and configure new image's data
ContentValues values = new ContentValues();
values.put(MediaColumns.TITLE, fileName);
values.put(MediaColumns.DATE_ADDED, System.currentTimeMillis());
values.put(MediaColumns.MIME_TYPE, "image/jpg");
// get a Uri for the location to save the file
Uri uri = getContext().getContentResolver().insert(
Images.Media.EXTERNAL_CONTENT_URI, values);
try {
// get an OutputStream to uri
OutputStream outStream = getContext().getContentResolver()
.openOutputStream(uri);
Bitmap bitmap = takeScreenshot();
// copy the bitmap to the OutputStream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
// flush and close the OutputStream
outStream.flush(); // empty the buffer
outStream.close(); // close the stream
View rootView = findViewById(R.id.doodleView).getRootView();
rootView.setDrawingCacheEnabled(false);
// display a message indicating that the image was saved
Toast message = Toast.makeText(getContext(),
R.string.doodlz_message_saved, Toast.LENGTH_SHORT);
message.setGravity(Gravity.CENTER, message.getXOffset() / 2,
message.getYOffset() / 2);
message.show(); // display the Toast
} // end try
catch (IOException ex) {
// display a message indicating that the image was saved
Toast message = Toast.makeText(getContext(),
R.string.doodlz_message_error_saving, Toast.LENGTH_SHORT);
message.setGravity(Gravity.CENTER, message.getXOffset() / 2,
message.getYOffset() / 2);
message.show(); // display the Toast
} // end catch
} // end method saveImage
public Bitmap takeScreenshot() {
View rootView = findViewById(R.id.doodleView).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void ClearCache() {
View rootView = findViewById(R.id.doodleView).getRootView();
rootView.destroyDrawingCache();
}
public void savePreviousPlan(File plan) {
try {
ObjectOutputStream save = new ObjectOutputStream(
new FileOutputStream(plan));
System.out.println(colorsMap);
save.writeObject(cPath);
save.writeObject(colorsMap);
save.writeObject(widthsMap);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void loadPreviousPlan(File plan) {
try {
Canvas canvas = new Canvas();
ObjectInputStream save = new ObjectInputStream(new FileInputStream(
plan));
ArrayList<CustomPath> loadpath = new ArrayList<CustomPath>();
Map<CustomPath, Integer> loadcolorsMap = new HashMap<CustomPath, Integer>();
Map<CustomPath, Float> loadwidthsMap = new HashMap<CustomPath, Float>();
// clear();
loadpath = (ArrayList<CustomPath>) save.readObject();
loadcolorsMap = (HashMap<CustomPath, Integer>) save.readObject();
loadwidthsMap = (HashMap<CustomPath, Float>) save.readObject();
System.out.println(loadpath);
for (int i = 0; i < loadpath.size(); i++) {
cPath.add(loadpath.get(i));
}
// colorsMap.clear();
// widthsMap.clear();
colorsMap.putAll(loadcolorsMap);
widthsMap.putAll(loadwidthsMap);
//postInvalidate();
// bitmapCanvas.drawBitmap(bitmap, 0, 0, mPaintScreen);
// for (CustomPath p : cPath) {
// mPaint.setColor(colorsMap.get(p));
// mPaint.setStrokeWidth(widthsMap.get(p));
//
// bitmapCanvas.drawPath(p, mPaint);
// System.out.println("test");
// }
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}