我在绘画应用上遇到了一些问题。我正在扩展View类并高估一些主要方法。以下是一些:
变量:
private ArrayList<Path> aldrawPath;
private ArrayList<Paint> aldrawPaint;
-onDraw:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
for(int i = 0; i < aldrawPath.size(); ++i ){
canvas.drawPath(aldrawPath.get(i),aldrawPaint.get(i));
}
}
-backup(按下撤销按钮时)
public void Backup(){
drawRectangle();
if ( aldrawPath.size()-1 > 0){
aldrawPath.remove(aldrawPath.size()-1);
aldrawPaint.remove(aldrawPaint.size()-1);
}
invalidate();
}
-drawRectangle
Draw a white rectangle where the user will paint
drawCanvas.drawRect(left,top,right,bottom,myPaint);
-OnTouchEvent
1.Action down --> path.moveTo
2.Action move --> path.lineTo
3.Action up --> path.drawpath + path and paint added to their arrays + path.reset
调整大小
*everything as a normal resize +
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
问题:
当我单击撤消按钮时,理论上添加的最后一个绘制路径将被删除,并且必须在画布上绘制paths.size() - 1。
它的作用:它只是绘制白色矩形。从来没有路径。
如果您需要更多代码或提出任何问题,请随时进行。
提前致谢
解决方案:
问题出现在ontouchevent上。而不是使用path.reset(),你应该使用: path = new Path();