如何解决Android SurfaceView未显示onDraw
| 为了简单起见,我在SurfaceView上绘制一个整数,每次绘制都增加1。 正如我在System.out上看到的那样,实际上发生了增加。 屏幕上的文本停留在\'0 \'上。 谁能告诉我我做错了什么? SurfaceViewTest.javapackage com.niek.surfaceviewtest;
import android.app.Activity;
import android.os.Bundle;
public class SurfaceViewTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
StatusView.java
package com.niek.surfaceviewtest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class StatusView extends SurfaceView implements SurfaceHolder.Callback {
private int tmp;
private DrawThread drawThread;
public StatusView(Context context,AttributeSet attrs) {
super(context,attrs);
getHolder().addCallback(this);
setFocusable(true);
drawThread = new DrawThread(getHolder());
}
@Override
public void onDraw(Canvas c) {
c.drawColor(Color.BLACK);
Paint p = new Paint();
p.setColor(Color.RED);
c.drawText(tmp + \"\",10,p);
tmp++;
System.out.println(tmp);
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawThread.setRunning(true);
drawThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to finish,or else
// it might touch the Surface after we return and explode
boolean retry = true;
drawThread.setRunning(false);
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
protected class DrawThread extends Thread {
private SurfaceHolder surfaceHolder;
private boolean isRunning;
public DrawThread(SurfaceHolder surfaceHolder) {
this.surfaceHolder = surfaceHolder;
isRunning = false;
}
public void setRunning(boolean run) {
isRunning = run;
}
public void run() {
Canvas c;
while (isRunning) {
c = null;
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above,we don\'t leave the Surface in an
// inconsistent state
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
main.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
android:background=\"#FFFFFF\">
<com.niek.surfaceviewtest.StatusView
android:id=\"@+id/statusview1\"
android:layout_width=\"fill_parent\"
android:layout_height=\"30dip\"
android:background=\"#000000\"
/>
</LinearLayout>
解决方法
我唯一的猜测是,由于视图没有失效,因此未在表面上进行绘画。您应该调用
invalidate()
进行绘制,然后让框架调用onDraw()
。也许这就是为什么增加“ 5”的原因,但是喷涂操作仅在第一次时到达表面。
可能值得尝试:也许让Canvas c
成为StatusView
的成员,然后替换
synchronized (surfaceHolder) {
onDraw(c);
}
与
synchronized (surfaceHolder) {
invalidate();
}
那样有用吗?
,当前解决方案不正确。您正在使用ѭ10以便从单独的线程更新内容,而不是使用invalidate()
方法,该方法将在系统刷新内容时运行onDraw()
方法。问题是您为ѭ7设置了背景,请尝试删除该行
android:background=\"#000000\"
显然,您需要控制该视图中显示的全部信息。
,看起来您正在使用setContentView(R.layout.main)显示main.xml;而不是创建表面并显示它。除非我在某处缺少代码,否则我认为情况并非如此。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。