我想随机画N个三角形,帧率60fs,就重写了SurfaceView,但是屏幕会不停的闪,中途按返回建,还会出现force close,请各位高手帮忙看一下(不过我现在已经用新的方法实现了)
public class MyView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder holder;
public MyView(Context context) {
super(context);
holder = this.getHolder();//获取holder
holder.addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) {
new Thread(new MyThread()).start();
} public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
} class MyThread implements Runnable {
public void run() {
Paint mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);
Random random = new Random(System.currentTimeMillis());
Canvas canvas = null;
int rotate = 0;
while (rotate < 500) {
//Date curr = new Date();
rotate=rotate+1;
try{
canvas = holder.lockCanvas();//获取canvas
mPaint.setColor(new Color().rgb(random.nextInt(255), random.nextInt(255), random
.nextInt(255)));
mPaint.setStrokeWidth(2);/*设置paint的外框宽度*/
Path mPath = new Path();
mPath.moveTo(random.nextInt(450), random.nextInt(600));
mPath.lineTo(random.nextInt(300), random.nextInt(400));
mPath.lineTo(random.nextInt(200), random.nextInt(300));
mPath.close();
canvas.drawPath(mPath, mPaint);
Thread.sleep(Math.max(0, 17-(new Date().getTime()))); //休眠
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
holder.unlockCanvasAndPost(canvas); //解锁canvas,提交画好的图像
}
}
}
}
}
public class MyView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder holder;
public MyView(Context context) {
super(context);
holder = this.getHolder();//获取holder
holder.addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) {
new Thread(new MyThread()).start();
} public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
} class MyThread implements Runnable {
public void run() {
Paint mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);
Random random = new Random(System.currentTimeMillis());
Canvas canvas = null;
int rotate = 0;
while (rotate < 500) {
//Date curr = new Date();
rotate=rotate+1;
try{
canvas = holder.lockCanvas();//获取canvas
mPaint.setColor(new Color().rgb(random.nextInt(255), random.nextInt(255), random
.nextInt(255)));
mPaint.setStrokeWidth(2);/*设置paint的外框宽度*/
Path mPath = new Path();
mPath.moveTo(random.nextInt(450), random.nextInt(600));
mPath.lineTo(random.nextInt(300), random.nextInt(400));
mPath.lineTo(random.nextInt(200), random.nextInt(300));
mPath.close();
canvas.drawPath(mPath, mPaint);
Thread.sleep(Math.max(0, 17-(new Date().getTime()))); //休眠
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
holder.unlockCanvasAndPost(canvas); //解锁canvas,提交画好的图像
}
}
}
}
}
解决方案 »
- android中webservce获取soapObject数据的解析问题
- listview刷新问题。急=。=求助
- 你也许也遇到过这样的问题!进来讨论下吧
- 求教如何获得sd卡中指定文件夹的大小?
- SimpleAdapter 数据项能否使用逻辑控制TextView颜色
- so库如何调试
- 求教:android无法和手机正常连接(composite)
- 百度云推送在jdk1.5报错
- android 如何从服务器端的数据库中拿数据,在客户端显示类?
- WeakReference
- 菜鸟求助 EditText获取空值
- 导入现有Android工程时,出现Project has no default.properties file
应用上加缓冲就可以了。 //加缓冲
private Paint[] paints=new Paint[2];
private Path[] paths=new Path[2];
//这样更新canvas
paints[0]=paints[1];
paints[1]=mPaint;
paths[0]=paths[1];
paths[1]=mPath;
if(paints[0]!=null) //画的时候还有画一次的
canvas.drawPath(paths[0], paints[0]);
canvas.drawPath(mPath, mPaint);
怎么解决两个buffer画面不同的问题?
package cn.elf;import java.util.Random;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.view.SurfaceHolder;
import android.view.SurfaceView;public class MyView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder holder; public MyView(Context context) {
super(context);
holder = this.getHolder();//获取holder
holder.addCallback(this);
} public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
new Thread(new MyThread()).start();
} public void surfaceCreated(SurfaceHolder holder) { } public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub } class MyThread implements Runnable {
public void run() {
Paint mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);
Random random = new Random();
Canvas canvas = null;
Bitmap bak = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas bakCanvas = new Canvas(bak);
int rotate = 0; while (rotate < 100) {
long before = System.currentTimeMillis();
rotate = rotate + 1;
try {
canvas = holder.lockCanvas();//获取canvas
mPaint.setColor(Color.rgb(random.nextInt(255), random.nextInt(255),
random.nextInt(255)));
mPaint.setStrokeWidth(2);/*设置paint的外框宽度*/
Path mPath = new Path();
mPath.moveTo(random.nextInt(450), random.nextInt(600));
mPath.lineTo(random.nextInt(300), random.nextInt(400));
mPath.lineTo(random.nextInt(200), random.nextInt(300));
mPath.close();
bakCanvas.drawPath(mPath, mPaint);
canvas.drawBitmap(bak, 0, 0, null);
before = System.currentTimeMillis() - before;
if (before < 17) {
Thread.sleep(17 - before); //休眠
} } catch (InterruptedException e) {
e.printStackTrace();
} finally {
holder.unlockCanvasAndPost(canvas); //解锁canvas,提交画好的图像
}
}
}
}
}