2 magicuser0 magicuser0 于 2016.01.14 16:28 提问

安卓 surfaceview 为什么最初几个循环会延迟

如果我把线程的sleep时间改成1000,就会出现卡屏的现象。大概要卡10秒左右
package com.example.myadd;

import java.util.Random;

import com.example.myob.Soldier;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class MyView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder Myholder = null;
myThread mt;

public MyView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    Myholder = this.getHolder();
    Myholder.addCallback(this);
    mt = new myThread(Myholder);
}

// @Override
// protected void onDraw(Canvas canvas) {
// // TODO Auto-generated method stub
// Soldier soldier = new Soldier();
// soldier.setLeave(2);
// soldier.setLocal(290, 290);
// soldier.doDraw(canvas);
// System.out.println(soldier.getLocal_x());
// super.onDraw(canvas);
// //soldier.run();
// //System.out.println(soldier.getLocal_x());
// //super.onDraw(canvas);
// }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mt.start();

}

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

}

class myThread extends Thread {

    private SurfaceHolder holder;
    Soldier soldier = new Soldier();
    Canvas canvas = null;

    public myThread(SurfaceHolder holder) {
        // TODO Auto-generated constructor stub
        this.holder = holder;
    }

    /*
     * public void run() { int count = 0; while (true) { Canvas c = null;
     * try { synchronized (holder) { c = holder.lockCanvas(null);//
     * 锁定画布,一般在锁定后就可以通过其返回的画布对象Canvas,在其上面画图等操作了。
     * c.drawColor(Color.BLACK);// 设置画布背景颜色 Paint p = new Paint(); // 创建画笔
     * p.setColor(Color.WHITE); Rect r = new Rect(100, 50, 300, 250);
     * c.drawRect(r, p); c.drawText("这是第" + (count++) + "秒", 100, 310, p);
     * Thread.sleep(1000);// 睡眠时间为1秒
     * 
     * // TODO Auto-generated method stub soldier.setLocal(290, 290);
     * soldier.setLeave(2); float i,j,k,l ; i = j = k = l = 300; while
     * (true) { try { canvas = null; synchronized (holder) { canvas =
     * holder.lockCanvas(); System.out.println("32");
     * 
     * //soldier.doDraw(canvas);
     * 
     * //soldier.move(); Paint p = new Paint(); p.setColor(Color.RED);
     * canvas.drawCircle(i+=10, j+=10, 15, p); } } catch (Exception e) { //
     * TODO: handle exception e.printStackTrace(); } finally { if (c !=
     * null) { holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。
     * 
     * } } } }
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        soldier.setLocal(290, 290);
        soldier.setLeave(2);
        float i, j, k, l;
        i = j = k = l = 300;
        while (true) {
            try {
                canvas = null;
                synchronized (holder) {
                    canvas = holder.lockCanvas();
                    System.out.println("32");

                    // soldier.doDraw(canvas);

                    // soldier.move();
                    Paint p = new Paint();
                    p.setColor(Color.RED);
                    canvas.drawCircle(i += 10, j += 10, 15, p);
                    Thread.sleep(10);
                }

            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                //
                holder.unlockCanvasAndPost(canvas);
            }
        }

    }
}

}

1个回答

devmiao
devmiao   Ds   Rxr 2016.01.19 04:41
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!