Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 表面视图拉伸故障_Android_Canvas_Surfaceview_Ondraw_Surfaceholder - Fatal编程技术网

Android 表面视图拉伸故障

Android 表面视图拉伸故障,android,canvas,surfaceview,ondraw,surfaceholder,Android,Canvas,Surfaceview,Ondraw,Surfaceholder,我正在尝试一个具有SurfaceView的示例。我从SurfaceView继承了我的类,并按如下方式使用: public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback { Context context; MySurfaceViewThread mThread; SurfaceHolder holder; Paint paint; int x =

我正在尝试一个具有
SurfaceView
的示例。我从
SurfaceView
继承了我的类,并按如下方式使用:

public class MySurfaceView extends SurfaceView  implements  SurfaceHolder.Callback {

    Context context;
    MySurfaceViewThread mThread;
    SurfaceHolder holder;
    Paint paint;

    int x = 20, y = 20, r = 10;

    public void init() {
        holder = getHolder();
        holder.addCallback(this);
        mThread = new MySurfaceViewThread(getHolder(), this);

        paint = new Paint();
        paint.setStyle(Style.STROKE);
        paint.setStrokeCap(Cap.ROUND);
        paint.setStrokeWidth(1);
        paint.setColor(Color.rgb(255, 255, 255));
    }

    public MySurfaceView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        init();

    }

    public MySurfaceView(Context context, AttributeSet attr) {
        super(context,attr);
        this.context = context;
        init();
    }

    public MySurfaceView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        this.context = context;
        init();
    }

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


    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mThread.isRunning = false;
        while (true) {
            try {
                mThread.join();
                break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void onDraw(Canvas canvas) {
//      super.onDraw(canvas);
//      canvas.drawColor(0, Mode.CLEAR);
        x += 2;
        y += 2;
        r += 3;
        canvas.drawColor(Color.rgb(x%255, y%255, (x+y)%255));
        canvas.drawCircle(x, y, r, paint);
        canvas.drawText("x:"+x, 100, 100, paint);
        Log.d("onDraw","onDraw()"+"x:"+x + ",y:"+y+",r:"+r);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.d("surfaceCreated", "surfaceCreated()");
        mThread.isRunning = true;
        mThread.start();
    }

}
我正在从
线程更新我的
画布
,该线程是:

public class MySurfaceViewThread extends Thread {

    SurfaceHolder holder;
    MySurfaceView surfaceView;
    boolean isRunning = false;

    public MySurfaceViewThread(SurfaceHolder holder, MySurfaceView surfaceView) {
        Log.d("thread","thread constructor");
        this.holder = holder;
        this.surfaceView = surfaceView;
    }

    @Override
    public void run() {
        Log.d("run","run()");
        while(isRunning) {

            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();

                synchronized(holder) {
                    surfaceView.onDraw(canvas);
                }
                sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                holder.unlockCanvasAndPost(canvas);
                Log.d("canvas status", "canvas unlocaked...");
            }
        }
    }

}

当应用程序启动并调用其
onDraw()
时,它也会绘制圆圈和文本,但在进一步调用
onDraw()
时,它不会绘制任何内容。表示第一次更新后屏幕上没有任何更改。知道我哪里出错了吗?我是android新手,学习速度也很慢。

我得到了答案。这很奇怪。我不知道为什么会这样。 我对我的活动中的以下行进行了注释,它将运行

surfaceView.setBackgroundColor(Color.rgb(0, 255, 0));
活动代码为:

public class SurfaceViewTutorialActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MySurfaceView surfaceView = new MySurfaceView(this);
//        surfaceView.setBackgroundColor(Color.rgb(0, 255, 0));
        surfaceView.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        setContentView(surfaceView);
    }
}
如果有人知道这一点,请引导我到正确的方向