уже несколько раз задавал вопрос, возможно неправильно, но решения не нашел. Очередная попытка)
Я создаю рисовалку. Мне необходима возможность рисовать произвольные линии, типа карандаш, и различные геом. фигуры при движении пальца(фигуры меняют свой размер в зависимости от отдаления от первоначального касания). С карандашем проблем нет, а вот при отрисовки геом. фигур возникает проблема, для анимации изменения размера я закрашиваю весь Canvas белым цветом, чтоб скрыть предыдущий размер фигуры, проблема в том что закрашивается все отрисованное до этого
Вот код:
public class Pencil {
private float x;
private float y;
private Path path;
private static Pencil pencil;
private Pencil(float x, float y){
this.x = x;
this.y = y;
path = new Path();
path.moveTo(x, y);
}
public static Pencil createPencil(float x, float y){
if(pencil == null) return new Pencil(x,y);
return pencil;
}
public void draw(Canvas canvas, Paint paint){
canvas.drawPath(path, paint);
}
public void update(float x, float y){
this.x = x;
this.y = y;
path.lineTo(x,y);
}
}
public class Circle {
private float x;
private float y;
private float radius;
private static Circle circle;
private Circle(float x,float y, float radius){
this.x = x;
this.y = y;
this.radius = radius;
}
public static Circle createCircle(float x, float y, float radius){
if(circle == null) return new Circle(x,y,radius);
return circle;
}
public void draw(Canvas canvas, Paint paint){
canvas.drawColor(Color.WHITE);
canvas.drawCircle(x,y,radius,paint);
}
public void update(float startX,float startY,float finishX,float finishY){
//AB = √(x2 - x1)2 + (y2 - y1)2 - расчет длины отрезка между точками
double xInPow = Math.pow( (finishX - startX),2);
double yInPow = Math.pow( (finishY - startY),2);
radius = (float) (Math.sqrt(xInPow + yInPow))/2;
}
}
public class ViewCanvas extends View {
private Bitmap mainBitmap;
private Bitmap saveBitmap;
private Canvas mainCanvas;
private Paint paint;
public ViewCanvas(Context context) {
super(context);
}
public ViewCanvas(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ViewCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mainBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
saveBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mainCanvas = new Canvas(mainBitmap);
createDefaultPaint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(saveBitmap,0,0,paint);
canvas.drawBitmap(mainBitmap,0,0,paint);
invalidate();
}
public void saveBitmap(){
saveBitmap = mainBitmap.copy(Bitmap.Config.ARGB_8888,true);
}
private void createDefaultPaint(){
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5.0f);
paint.setColor(Color.BLACK);
}
public Canvas getCanvas() {
return mainCanvas;
}
public Paint getPaint() {
return paint;
}
public void setBrashStyle(Paint.Style style){
paint.setStyle(style);
}
public void setBrushColor(int color){
paint.setColor(color);
}
public void setBrashWidth(float width){
paint.setStrokeWidth(width);
}
}
Часть класса в котором происходит вся работа
float startX = 0;
float startY = 0;
private boolean viewOnTouchListener(View view, MotionEvent event){
float eventX = event.getX();
float eventY = event.getY();
float radius = 0;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
startX = event.getX();
startY = event.getY();
if (currentTool == pencil) new Pencil(eventX,eventY);
if (currentTool == circle) new Circle(eventX,eventY,radius);
break;
}
case MotionEvent.ACTION_MOVE:{
drawCurrentTool();
updateCurrentTool(eventX,eventY,startX,startY,event.getX(),event.getY());
break;
}
case MotionEvent.ACTION_UP:{
binding.paintView.saveBitmap();
break;
}
}
return true;
}
private void drawCurrentTool(){
if (currentTool == pencil) pencil.draw(binding.paintView.getCanvas(),
binding.paintView.getPaint());
if (currentTool == circle) circle.draw(binding.paintView.getCanvas(),binding.paintView.getPaint());
}
private void updateCurrentTool(float x, float y,float startX,float startY,float finishX,float finishY){
if (currentTool == pencil) pencil.update(x,y);
if (currentTool == circle) circle.update(startX,startY,x,y);
}
Главная проблема как перерисовывать фигуру на отрисованном Bitmape ?