gpt4 book ai didi

android - Android 5位图绘制麻烦

转载 作者:行者123 更新时间:2023-12-03 13:11:49 25 4
gpt4 key购买 nike

将具有背景渲染的 View 写成Bitmap。下面的代码很大,但是很简单。两个位图。一种在屏幕上绘制,第二种用于自定义绘制。绘制完成后,将切换位图。

在Android 4.x上,一切正常。在Android 5上,一切都很慢,有很大的滞后和日志消息:

Choreographer: Skipped 30 frames! The application may be doing too much work on its main thread



如果在 drawThread()程序中只是删除以下行: synchronized (this) {useBMP = n; }或通过 if(useBMP == 0) n = 0;删除位图切换,一切将顺利快速地进行!当然-带有人工制品。

我被困住了,我不明白为什么。对可能有帮助的任何想法和信息感兴趣。
public class TouchView extends ScrollView {
volatile int useBMP;
Bitmap cacheBMP[];
boolean startDraw, exitDraw;

public TouchView(Context context) {
super(context);

cacheBMP = new Bitmap[2];
}

protected void onSizeChanged (int w, int h, int oldw, int oldh) {
for( int i=0; i<2; i++ )
cacheBMP[i] = Bitmap.createBitmap(w*2, h*2, Bitmap.Config.ARGB_8888);

useBMP=0;
startDraw = true;
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

exitDraw = false;
new Thread(new Runnable() {
public void run() { drawThread(); }
}).start();
}

@Override
protected void onDetachedFromWindow() {
exitDraw = true;

super.onDetachedFromWindow();
}

void drawThread() {
int n;
while( !exitDraw ) {
if( startDraw ) {
startDraw = false;

if( useBMP==0 ) n=1;
else n=0;

drawBMP(n);

synchronized (this) { useBMP = n; }
postInvalidate();
}

try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

void drawBMP(int n) {
cacheBMP[n].eraseColor(Color.WHITE);
Canvas canvas = new Canvas(cacheBMP[n]);
.....
}

public void redraw() {
startDraw = true;
}

@Override
protected void onDraw(Canvas c) {
synchronized (this) {
c.drawBitmap(cacheBMP[useBMP],0,0,p);
}
}
}

最佳答案

不能从onDraw中删除已同步的吗?他将等待具有LOCK的任何线程

关于android - Android 5位图绘制麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com