我在android中创建了一个绘图应用程序,并且想知道,在简明的RAM分配的同时,在画布上绘制的正确方法是什么。
我注意到当路径中的路径或点数变得太大时,onDraw(Canvas canvas)
的延迟变得明显。很明显有一个解决方案,但我正在寻找解决这个问题的标准方法。
我在网上找不到太多信息,但我目前正在尝试将当前canvas
保存到Bitmap
,然后将View
设为canvas
({ {1}})。
我目前正在尝试以下操作,但未将LinearLayout
设置为LinearLayout
:
Background
这是创建public Bitmap saveTempCanvas() {
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
this.setBackgroundColor(Color.WHITE);
Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
return bmp;
}
//. . .
//. . .
Drawable ob = new BitmapDrawable(getResources(), pv.saveTempCanvas());
LinearLayoutCanvas.setBackgroundDrawable(ob);
活动的标准方式吗?
为什么以下代码无法设置"Paint"
?
答案 0 :(得分:0)
如果你每次都在onDraw()中调用这个函数,我并不感到惊讶为什么它越来越迟钝。这是我的方法,它运作良好:
public class TouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
Bitmap bitMap;
Canvas myCanvas;
Canvas defaultCanvas;
public TouchEventView(Context context) {
this(context, null);
}
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
bitMap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.BEVEL);
}
@Override
protected void onDraw(Canvas canvas) {
myCanvas = new Canvas(bitMap);
defaultCanvas = canvas;
canvas.drawPath(path, paint);
myCanvas.drawColor(Color.WHITE);
myCanvas.setBitmap(bitMap);
myCanvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
performClick();
break;
default:
return false;
}
invalidate();
return true;
}
@Override
public boolean performClick() {
super.performClick();
return true;
}
}