Что на самом деле я хочу, всякий раз, когда я рисую или пишу на 1-м холсте, в то же время это будет отображаться на 2-м холсте с точками, а не растровым изображением. другой работает на том же экране.
Я закончил с точками сохранения и путями на первом холсте, но когда я показываю этот путь на другом холсте, он показывает мне только линию, а не правильный холст.
Позвольте мне показать вам изображение, как оно выглядит. Мой первый холст
Мой первый холст:
public class CanvasView extends View {
private Path mDrawPath;
private Paint mBackgroundPaint;
private Paint mDrawPaint;
private Canvas mDrawCanvas;
private Bitmap mCanvasBitmap, mBitmap;
public static String XVALUE = "";
public static String YVALUE = "";
private static final float TOUCH_TOLERANCE = 5;
public static ArrayList<String> mXPaths = new ArrayList<>();
public static ArrayList<String> mYPaths = new ArrayList<>();
private ArrayList<String> mUnXPaths = new ArrayList<>();
private ArrayList<String> mUnYPaths = new ArrayList<>();
private ArrayList<Path> mPaths = new ArrayList<>();
public static ArrayList<Path> mPaths1 = new ArrayList<>();
private ArrayList<Paint> mPaints = new ArrayList<>();
private ArrayList<Path> mUndonePaths = new ArrayList<>();
private ArrayList<Paint> mUndonePaints = new ArrayList<>();
public static ArrayList<Point> mPoints = new ArrayList<>();
private float mX, mY;
private int mBackgroundColor = 0xFFFFFFFF;
private int mPaintColor = 0xFF660000;
private int mStrokeWidth = 4;
public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
mDrawPath = new Path();
mBackgroundPaint = new Paint();
initPaint();
}
private void initPaint() {
mDrawPaint = new Paint();
mDrawPaint.setColor(mPaintColor);
mDrawPaint.setAntiAlias(true);
mDrawPaint.setStrokeWidth(mStrokeWidth);
mDrawPaint.setStyle(Paint.Style.STROKE);
mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
mDrawPaint.setDither(true);
}
private void drawBackground(Canvas canvas) {
mBackgroundPaint.setColor(mBackgroundColor);
mBackgroundPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), mBackgroundPaint);
}
private void drawPaths(Canvas canvas) {
int i = 0;
for (Path p : mPaths) {
canvas.drawPath(p, mPaints.get(i));
i++;
}
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Rect bitmapRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, source, bitmapRect, new Paint());
drawPaths(canvas);
canvas.drawPath(mDrawPath, mDrawPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mCanvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mDrawCanvas = new Canvas(mCanvasBitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDrawPath.moveTo(touchX, touchY);
mX = touchX;
mY = touchY;
Log.e("Location", String.valueOf("x : "+ event.getX()+ "y : " +event.getY()));
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(touchX - mX);
float dy = Math.abs(touchY - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mDrawPath.quadTo(mX, mY, (touchX + mX) / 2, (touchY + mY) / 2);
mX = touchX;
mY = touchY;
Point point = new Point();
point.x = (int) event.getX();
point.y = (int)event.getY();
mPoints.add(point);
mPaths1.add(mDrawPath);
Log.d("Ponts Values", mPoints.toString());
}
break;
case MotionEvent.ACTION_UP:
mDrawPath.lineTo(mX, mY);
mXPaths.add(String.valueOf(touchX));
mYPaths.add(String.valueOf(touchY));
mPaths.add(mDrawPath);
mPaints.add(mDrawPaint);
XVALUE = mXPaths.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
YVALUE = mYPaths.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
mDrawPath = new Path();
initPaint();
break;
default:
return false;
}
invalidate();
return true;
}
public void clearCanvas() {
mXPaths.clear();
mYPaths.clear();
mPaths.clear();
mPaints.clear();
mUndonePaths.clear();
mUndonePaints.clear();
XVALUE = "";
YVALUE = "";
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
mDrawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
invalidate();
}
public void setPaintColor(int color) {
mPaintColor = color;
mDrawPaint.setColor(mPaintColor);
}
public void setPaintStrokeWidth(int strokeWidth) {
mStrokeWidth = strokeWidth;
mDrawPaint.setStrokeWidth(mStrokeWidth);
}
public void setBackgroundColor(int color) {
mBackgroundColor = color;
mBackgroundPaint.setColor(mBackgroundColor);
invalidate();
}
public Bitmap getBitmap() {
drawBackground(mDrawCanvas);
drawPaths(mDrawCanvas);
this.setDrawingCacheEnabled(false);
this.setDrawingCacheEnabled(true);
return Bitmap.createBitmap(this.getDrawingCache());
}
public void undo() {
if (mPaths.size() > 0) {
mUnXPaths.add(mXPaths.remove(mPaths.size() - 1));
mUnYPaths.add(mYPaths.remove(mPaths.size() - 1));
mUndonePaths.add(mPaths.remove(mPaths.size() - 1));
mUndonePaints.add(mPaints.remove(mPaints.size() - 1));
Log.d("X Cordinates", mXPaths.toString());
Log.d("Y Cordinates", mYPaths.toString());
XVALUE = mXPaths.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
YVALUE = mYPaths.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
invalidate();
}
}
public void redo() {
if (mUndonePaths.size() > 0) {
mXPaths.add(mUnXPaths.remove(mUnXPaths.size() - 1));
mYPaths.add(mUnYPaths.remove(mUnYPaths.size() - 1));
mPaths.add(mUndonePaths.remove(mUndonePaths.size() - 1));
mPaints.add(mUndonePaints.remove(mUndonePaints.size() - 1));
Log.d("X Cordinates", mXPaths.toString());
Log.d("Y Cordinates", mYPaths.toString());
XVALUE = mXPaths.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
YVALUE = mYPaths.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
invalidate();
}
}
}
Код моего второго холста:
public class CanvasView_New extends View {
private Path mDrawPath;
private Paint mBackgroundPaint;
private Paint mDrawPaint;
private Canvas mDrawCanvas;
private Bitmap mCanvasBitmap, mBitmap;
public static String XVALUE = "";
public static String YVALUE = "";
private static final float TOUCH_TOLERANCE = 5;
public static ArrayList<String> mXPathsNew = new ArrayList<>();
public static ArrayList<String> mYPathsNew = new ArrayList<>();
private ArrayList<String> mUnXPathsNew = new ArrayList<>();
private ArrayList<String> mUnYPathsNew = new ArrayList<>();
private ArrayList<Path> mPathsNew = new ArrayList<>();
private ArrayList<Paint> mPaintsNew = new ArrayList<>();
private ArrayList<Path> mUndonePathsNew = new ArrayList<>();
private ArrayList<Paint> mUndonePaintsNew = new ArrayList<>();
private float mX, mY;
private int mBackgroundColor = 0xFFFFFFFF;
private int mPaintColor = 0xFF660000;
private int mStrokeWidth = 4;
private ArrayDeque<Point> mPoints1 = new ArrayDeque<>();
public CanvasView_New(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
mDrawPath = new Path();
mBackgroundPaint = new Paint();
initPaint();
}
private void initPaint() {
mDrawPaint = new Paint();
mDrawPaint.setColor(mPaintColor);
mDrawPaint.setAntiAlias(true);
mDrawPaint.setStrokeWidth(mStrokeWidth);
mDrawPaint.setStyle(Paint.Style.STROKE);
mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
mDrawPaint.setDither(true);
}
private void drawBackground(Canvas canvas) {
mBackgroundPaint.setColor(mBackgroundColor);
mBackgroundPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), mBackgroundPaint);
}
private void drawPaths(Canvas canvas) {
int i = 0;
for (Path p : mPathsNew) {
canvas.drawPath(p, mPaintsNew.get(i));
i++;
}
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Rect bitmapRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, source, bitmapRect, new Paint());
canvas.drawPath(mDrawPath, mDrawPaint);
for(int i = 0; i< mPoints.size(); i++) {
mDrawPath.moveTo(mPoints.get(i).x, mPoints.get(i).x);
mDrawPath.quadTo(mPoints.get(i).x, mPoints.get(i).y, (mPoints.get(i).x + mPoints.get(i).x) / 2, (mPoints.get(i).y + mPoints.get(i).y) / 2);
mPathsNew.add(mDrawPath);
mPaintsNew.add(mDrawPaint);
}
drawPaths(canvas);
canvas.drawPath(mDrawPath, mDrawPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mCanvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mDrawCanvas = new Canvas(mCanvasBitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDrawPath.moveTo(touchX, touchY);
mX = touchX;
mY = touchY;
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(touchX - mX);
float dy = Math.abs(touchY - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mDrawPath.quadTo(mX, mY, (touchX + mX) / 2, (touchY + mY) / 2);
mX = touchX;
mY = touchY;
}
break;
case MotionEvent.ACTION_UP:
mDrawPath.lineTo(mX, mY);
Log.d("Path Values", touchX + " " + touchY);
mXPathsNew.add(String.valueOf(touchX));
mYPathsNew.add(String.valueOf(touchY));
mPathsNew.add(mDrawPath);
mPaintsNew.add(mDrawPaint);
Log.d("X Cordinates", mXPathsNew.toString());
Log.d("Y Cordinates", mYPathsNew.toString());
XVALUE = mXPathsNew.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
YVALUE = mYPathsNew.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
mDrawPath = new Path();
initPaint();
break;
default:
return false;
}
invalidate();
return true;
}
public void clearCanvas() {
mXPathsNew.clear();
mYPathsNew.clear();
mPathsNew.clear();
mPaintsNew.clear();
mUndonePathsNew.clear();
mUndonePaintsNew.clear();
XVALUE = "";
YVALUE = "";
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
mDrawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
invalidate();
}
public void setPaintColor(int color) {
mPaintColor = color;
mDrawPaint.setColor(mPaintColor);
}
public void setPaintStrokeWidth(int strokeWidth) {
mStrokeWidth = strokeWidth;
mDrawPaint.setStrokeWidth(mStrokeWidth);
}
public void setBackgroundColor(int color) {
mBackgroundColor = color;
mBackgroundPaint.setColor(mBackgroundColor);
invalidate();
}
public Bitmap getBitmap() {
drawBackground(mDrawCanvas);
drawPaths(mDrawCanvas);
this.setDrawingCacheEnabled(false);
this.setDrawingCacheEnabled(true);
return Bitmap.createBitmap(this.getDrawingCache());
}
public void undo() {
if (mPathsNew.size() > 0) {
mUnXPathsNew.add(mXPathsNew.remove(mPathsNew.size() - 1));
mUnYPathsNew.add(mYPathsNew.remove(mPathsNew.size() - 1));
mUndonePathsNew.add(mPathsNew.remove(mPathsNew.size() - 1));
mUndonePaintsNew.add(mPaintsNew.remove(mPaintsNew.size() - 1));
Log.d("X Cordinates", mXPathsNew.toString());
Log.d("Y Cordinates", mYPathsNew.toString());
XVALUE = mXPathsNew.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
YVALUE = mYPathsNew.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
invalidate();
}
}
public void redo() {
if (mUndonePathsNew.size() > 0) {
mXPathsNew.add(mUnXPathsNew.remove(mUnXPathsNew.size() - 1));
mYPathsNew.add(mUnYPathsNew.remove(mUnYPathsNew.size() - 1));
mPathsNew.add(mUndonePathsNew.remove(mUndonePathsNew.size() - 1));
mPaintsNew.add(mUndonePaintsNew.remove(mUndonePaintsNew.size() - 1));
Log.d("X Cordinates", mXPathsNew.toString());
Log.d("Y Cordinates", mYPathsNew.toString());
XVALUE = mXPathsNew.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
YVALUE = mYPathsNew.toString().replaceAll("\\[|\\]", "").replaceAll(", ",",");
System.out.println("Cordinates X: "+XVALUE);
System.out.println("Cordinates Y:"+YVALUE);
invalidate();
}
}
}