我有pdf文件,我正在弯曲的画布上绘制路径,并且在滚动pdf页面时尝试显示前一个画布,为此我保存了pdf页码和路径。
我的代码:
这是onTouch:在ACTION_DOWN中,我每次都为path创建一个新的构造函数。调用它时,新路径正在删除旧路径并覆盖旧路径。
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
signaturePaint = new Paint(Paint.DITHER_FLAG);
signaturePaint.setAntiAlias(true);
signaturePaint.setDither(true);
signaturePaint.setColor(0xff0000cc);
signaturePaint.setStyle(Paint.Style.STROKE);
signaturePaint.setStrokeJoin(Paint.Join.ROUND);
signaturePaint.setStrokeCap(Paint.Cap.ROUND);
signaturePaint.setStrokeWidth(1);
**signaturePath = new Path();// When calling this my problem occure**
if (Constants.draw) {
Point p = getPagePositionOnScreen(currentPage);
int currentPageWidth = getCurrentPageWidth(currentPage);
int currentPageHeight = (int) getCurrentPageHeight(currentPage);
if (currentPageWidth < x || currentPageHeight < y) {
} else {
if (Constants.isSignatureSelected)
signaturePath.moveTo(x, y);
// pdfcanvas.moveTo(x, y);
}
firstX = x;
firstY = y;
}
break;
case MotionEvent.ACTION_MOVE:
if (Constants.draw) {
mPageNumber.add(currentPage);
// int[][] currentPagesSize = pagesProvider.getPageSizes();
float pageMarginX = getCurrentMarginX();
float pageMarginy = getCurrentMarginY();
// int pageWidth = currentPagesSize[0][0];
// int pageHeight = currentPagesSize[0][1];
int currentPageWidth = getCurrentPageWidth(currentPage);
int currentPageHeight = (int) getCurrentPageHeight(currentPage);
if (currentPageWidth < x || currentPageHeight < y) {
} else {
resetDirtyRect(x, y);
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
if (Constants.isSignatureSelected)
signaturePath.lineTo(historicalX, historicalY);
}
}
}
break;
case MotionEvent.ACTION_UP:
if (Constants.draw) {
firstX = x;
firstY = y;
if (Constants.isSignatureSelected)
saveCanvas();
}
break;
}
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
firstX = x;
firstY = y;
return true;
为了在数组列表中保存canvas:这是将我的画布路径保存在arraylist中
private void saveCanvas() {
SaveCanvasDrawing canvasDrawing = new SaveCanvasDrawing();
canvasDrawing.setPath(signaturePath);
canvasDrawing.setPdfPageNumber(currentPage);
canvasDrawing.setxAxis(firstX);
canvasDrawing.setyAxis(firstY);
mSavingCanvasDrawing.add(canvasDrawing);
mCanvasHashMap.put(currentPage, mSavingCanvasDrawing);
}
为了在画布上显示路径:在else块中的此方法中,我正在重置路径。当我调用此画布时,它并没有绘制我的路径。但是在调试时,我的路径不为null。但是,它没有在画布上显示路径。
private void redoCanvas() {
if (mCanvasHashMap.containsKey(currentPage)) {
mDrawingPath.clear();
mPageNumber.clear();
for (int j = 0; j < mSavingCanvasDrawing.size(); j++) {
int pageNumber = mSavingCanvasDrawing.get(j).getPdfPageNumber();
if (pageNumber == currentPage) {
float xAxis = mSavingCanvasDrawing.get(j).getxAxis();
float yAxis = mSavingCanvasDrawing.get(j).getyAxis();
Path path = mSavingCanvasDrawing.get(j).getPath();
mDrawingPath.add(path);
mPageNumber.add(pageNumber);
} else {
signaturePath.reset();
invalidate();
}
}
} else {
signaturePath.reset();
invalidate();
}
}
最后onDraw():在此方法中,我正在检查何时isSignatureSelected = false;。我正在调用保存的arraylist作为路径并绘制图形,这是我在redoCanvas()中重置路径时没有在画布上显示任何路径的问题。来自signaturePath。保护无效的onDraw(Canvas canvas){
if (Constants.isSignatureSelected) {
if (Constants.draw) {
canvas.drawPath(signaturePath, signaturePaint);
}
} else {
if (mPageNumber.contains(currentPage)) {
for (Path p : mDrawingPath) {
canvas.drawPath(p, signaturePaint);
}
}
}
}
是的,我有解决方案。在computeScroll()中,我创建Path的新构造函数,在onDraw()中,我检查路径的数组列表,并将路径添加到画布中
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句