Java 画布上的动态绘图花费的时间太长
我想画一个具有动画色彩属性的四边形网格,但最终得到的代码太慢,无法创建图像的基本结构,更不用说动画了。此外,图像不是在完成计算时绘制的,而是在按部分绘制的过程中绘制的。我怎样才能修好它?以下是onDraw(画布)方法:Java 画布上的动态绘图花费的时间太长,java,android,performance,android-canvas,Java,Android,Performance,Android Canvas,我想画一个具有动画色彩属性的四边形网格,但最终得到的代码太慢,无法创建图像的基本结构,更不用说动画了。此外,图像不是在完成计算时绘制的,而是在按部分绘制的过程中绘制的。我怎样才能修好它?以下是onDraw(画布)方法: protected void onDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); final int height = canvas.getHeight(); final int width =
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
final int height = canvas.getHeight();
final int width = canvas.getWidth();
Bitmap.Config conf = Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, height, conf);
new Thread(() -> {
int cells_amount = 0;
float cells_vertical = 0;
float cells_horizontal = 0;
cells_vertical = (float) height / CELL_SIZE;
int y = 0;
if (cells_vertical % 1 > 0) {
y = Math.round(CELL_SIZE * -(cells_vertical % 1));
cells_vertical = (int) (cells_vertical + 1);
}
cells_horizontal = (float) width / CELL_SIZE;
int x = 0;
if (cells_horizontal % 1 > 0) {
x = Math.round(CELL_SIZE * -(cells_horizontal % 1));
cells_horizontal = (int) (cells_horizontal + 1);
}
cells_amount = (int) (cells_horizontal * cells_vertical);
Canvas c = new Canvas(bitmap);
c.drawColor(Color.BLACK);
int x_preserved = x;
Paint textPaint = new Paint();
textPaint.setColor(Color.parseColor("#EEEEEE"));
for (int i = 0; i < cells_amount; i++) {
Rect rect = new Rect(x, y, x + CELL_SIZE, y + CELL_SIZE);
Paint framePaint = new Paint();
framePaint.setColor(Color.parseColor("#1F1F1F"));
framePaint.setStyle(Paint.Style.STROKE);
framePaint.setStrokeWidth(1);
c.drawRect(rect, framePaint);
if (random.nextBoolean()) {
String output = String.format(Locale.getDefault(), "%d", "+");
int cx = (int) (x + (CELL_SIZE / 2) - (textPaint.measureText(output) / 2));
int cy = (int) ((y + (CELL_SIZE / 2)) - ((textPaint.descent() + textPaint.ascent()) / 2));
c.drawText(output, cx, cy, textPaint);
}
if (i % cells_horizontal == 0 && i >= cells_horizontal) {
y += CELL_SIZE;
x = x_preserved;
} else {
x += CELL_SIZE;
}
}
}).start();
canvas.drawBitmap(bitmap, 0, 0, null);
}
受保护的void onDraw(画布){
画布。drawColor(颜色。黑色);
final int height=canvas.getHeight();
final int width=canvas.getWidth();
Bitmap.Config conf=Bitmap.Config.RGB_565;
位图Bitmap=Bitmap.createBitmap(宽度、高度、形态);
新线程(()->{
int cells_amount=0;
浮点数_垂直=0;
浮点数_水平=0;
单元格垂直=(浮动)高度/单元格大小;
int y=0;
如果(单元格垂直%1>0){
y=数学圆(单元格大小*-(单元格垂直%1));
单元格垂直=(int)(单元格垂直+1);
}
单元格水平=(浮动)宽度/单元格大小;
int x=0;
如果(单元格水平%1>0){
x=数学圆整(单元格大小*-(单元格水平%1));
单元格水平=(int)(单元格水平+1);
}
单元数量=(int)(单元水平*单元垂直);
画布c=新画布(位图);
c、 drawColor(颜色:黑色);
int x_=x;
绘制文本绘制=新绘制();
textPaint.setColor(Color.parseColor(“#EEEEEE”);
对于(int i=0;i=单元格水平){
y+=单元大小;
x=x_;
}否则{
x+=单元大小;
}
}
}).start();
drawBitmap(位图,0,0,null);
}
看看这个看看这个