【问题标题】:Android SurfaceView shows black screenAndroid SurfaceView 显示黑屏
【发布时间】:2011-06-17 15:53:22
【问题描述】:

我正在尝试在表面视图上绘图,但出现黑屏。我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <com.example.surfaceview.MySurface android:id="@+id/padview" 
        android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

我的代码的重要部分:MySurface.java

公共类 MySurface 扩展 SurfaceView 实现 SurfaceHolder.Callback {

public DrawingThread thread;

public MySurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    SurfaceHolder surfaceholder = getHolder();
    surfaceholder.addCallback(this);

    thread = new DrawingThread(surfaceholder, context, new Handler() {
        @Override
        public void handleMessage(Message m) {
            // Do some handle thing
        }
    });

    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder tholder, int format, int width, int height) {
    thread.setSurfaceSize(width, height);
    thread.holder = tholder;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread.running = true;
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    thread.running = false;
    while(retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
        }
    }
}

public Thread getThread() {
    return thread;
}

public class DrawingThread extends Thread {
    private SurfaceHolder holder;
    private Context context;
    private Handler handle;
    private Paint background;
    private Rect blank;

    public boolean running;
    public boolean created;

    public int canvasheight;
    public int canvaswidth;

    public PadThread(SurfaceHolder tholder, Context tcontext, Handler thandler) {
        holder = tholder;
        context = tcontext;
        handle = thandler;

        // Temporary canvas dimentions
        canvaswidth = 1;
        canvasheight = 1;

        running = false;
        created = false;

        background = new Paint();
        background.setColor(R.color.white);
        blank = new Rect(0, 0, canvaswidth, canvasheight);
    }

    @Override
    public void run() {
        Log.d("SurfaceView Test", "Drawing thread run");
        while(running) {
            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();
                synchronized(holder) {
                    // update object states
                    // get user input gestures
                    drawing(canvas);
                }
            } finally {
                if(canvas != null) {
                    holder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    private void drawing(Canvas canvas) {
        // Clear screen
        canvas.drawRect(blank, background);

        // Draw Things
    }

    public void setSurfaceSize(int width, int height) {
        synchronized(holder) {
            canvaswidth = width;
            canvasheight = height;

            // New background rect
            blank.set(0, 0, canvaswidth, canvasheight);
        }
    }
}

}

代码基于 Google 的 Lunar Landar SurfaceView 示例http://developer.android.com/resources/samples/LunarLander/index.html

我知道所有代码都是通过日志记录的。

【问题讨论】:

  • 已修复,问题是我必须从提供的上下文而不是 R 加载颜色。所以我不得不使用 background.setColor(R.color.white) 而不是 background.setColor(context.getResources().getColor(R.color.white)

标签: android surfaceview


【解决方案1】:

改变绘图功能

background.setColor(Color.RED); canvas.drawRect(new Rect(10, 10, 100, 100), 背景);

(colors和rect有测试值)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多