android - 绘制到 SurfaceView 时出现应用程序错误

标签 android xml drawing surfaceview

我正在进行一个简单的编码尝试,试图在我的 main.xml 布局上创建的 SurfaceView 上进行绘制。我可以更改背景颜色并正常显示图标,但是当我尝试绘制时出现错误。我是新手,很明显我遗漏了一些东西,请提供帮助提示,谢谢!

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

        <SurfaceView android:id="@+id/Paper"
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent">
        </SurfaceView>

</LinearLayout>

并在此处编码;

package com.example.SurfaceViewTest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private Paint paint;
    Bitmap mDrawing;

    boolean mRun;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSurfaceView = (SurfaceView) this.findViewById(R.id.Paper);

        mSurfaceHolder = mSurfaceView.getHolder();

        mSurfaceHolder.addCallback(this);

        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mSurfaceView.setBackgroundColor(Color.rgb(0, 255, 0));
        mRun=true;
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    Thread thread = new Thread(){

        public void doDraw(Canvas c){
            mDrawing = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565);

            c.setBitmap(mDrawing);

            paint = new Paint();
            paint.setColor(Color.rgb(255, 255,255));

            c.drawLine(1,1,200,300, paint);
        }

        public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        doDraw(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) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    };
}

更新:

好的,谢谢!

package com.example.SurfaceViewTest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    Bitmap mDrawing;
    Canvas tempCanvas = new Canvas();
    Paint paint;

    boolean mRun;

    int intCanvasWidth, intCanvasHeight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSurfaceView = (SurfaceView) this.findViewById(R.id.Paper);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        intCanvasWidth = width;
        intCanvasHeight = height;
        mDrawing = Bitmap.createBitmap(intCanvasWidth, intCanvasHeight,
                Bitmap.Config.RGB_565);
        paint = new Paint();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (thread.getState() == Thread.State.TERMINATED) {
            thread = new Thread();
        }
        mRun = true;
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        mRun = false;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

    Thread thread = new Thread() {
        public void doDraw(Canvas c) {
            tempCanvas.setBitmap(mDrawing);

            paint.setColor(Color.rgb(255, 255,255));

            tempCanvas.drawLine(1,1,200,300, paint);

            c.drawBitmap(mDrawing, 0, 0, null);
        }

        @Override
        public void run() {
            Canvas c;
            while (mRun) {
                c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        doDraw(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) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    };
}

最佳答案

这是我的 SurfaceView 的工作原理。我认为您的问题是在 surfaceCreated() 中执行位图。

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

Thread thread = new Thread(){
...
public void doDraw(Canvas c){
//draw onto the canvas here
}

public void run() {
    while (mRun) {
        Canvas c = null;
        try {
        c = mSurfaceHolder.lockCanvas(null);
        synchronized (mSurfaceHolder) {
            doDraw(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) {
            mSurfaceHolder.unlockCanvasAndPost(c);
        }
        }
     }
   }
};

关于android - 绘制到 SurfaceView 时出现应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4626698/

相关文章:

Android SIP 聊天消息

java - Box2d 不会使用 LibsGDX 和 Gradle 进行加载

java - XMLStreamReader 和 XMLEventReader 有什么区别?

java - 布局 XML 中添加的 EditText 与代码中添加的 EditText 之间存在奇怪的差异

java - 从同一个类中绘制多个字符串

android - 在撰写 TextField 中格式化数字

带有电话号码黑名单的 Android 4.0 Telephony API

java - 根据 XSD 验证完整的 XML

ios - 在 Swift 中绘制多边形

iphone - 在 Quartz (iPhone SDK) 中绘制(打印 - 不是图表)方程