Drawable不会在SurfaceView,Thread实现上绘制,而不会覆盖onDraw

时间:2012-10-18 23:29:38

标签: java android multithreading drawable surfaceview

我真的很难过为什么这不起作用。尝试使用布局中的SurfaceView和绘制线程绘制一个简单的PNG到屏幕,而不是覆盖onDraw函数。它实际上与LunarLander示例项目完全相同(并且我实际上挫败了整个代码集)。

我已经通过日志测试了它,我知道精灵正被“绘制”到画布上,但窗口中没有任何内容。

这是代码,希望它不会太冗长(不重要的位被剥离):

activity_pannenkoekenhuis.xml(layout /)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.pannenkoekenhuis.MainView
    android:id="@+id/pannenkoekenhuis_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Pannenkoekenhuis" >
</RelativeLayout>

</FrameLayout>

MainView(SurfaceView):

public class MainView extends SurfaceView implements SurfaceHolder.Callback {

class MainThread extends Thread {
    SurfaceHolder surfaceHolder;
    Context context;
    Handler handler;

    HandleResources hResources;
    HandleGame hGame;

    public MainThread(SurfaceHolder surfaceHolder, Context context,
            Handler handler) {
        this.surfaceHolder = surfaceHolder;
        this.context = context;
        this.handler = handler;
        hResources = new HandleResources(context);
        hGame = new HandleGame();

        init();
    }

    @Override
    public void run() {
        while (true) {
            Canvas canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                                            Drawable d = context.getResources().getDrawable(R.drawable.s_char);
                    d.draw(canvas);
                }
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

MainThread thread;

public MainView(Context context, AttributeSet attrs) {
    super(context, attrs);

    SurfaceHolder surfaceHolder = getHolder();
    getHolder().addCallback(this);

    thread = new MainThread(surfaceHolder, context, new Handler());

    setFocusable(true);
}
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

Gah,想通了。需要删除setBackgroundColor()由于某种原因,因为它可能是绘制图形。愚蠢的错误。

相关问题