问题描述
- 一个关于android surfaceView的问题,求大神回答
-
surfaceView据说双缓存,两画布换来换去,但是我post后画布有时候还有有时候就没了
surfaceCreated方法里,最后的
canvas=sfh.lockCanvas();
sfh.unlockCanvasAndPost(canvas);
多加几个少加几个出现不同的情况,这个是怎么回事,大神求帮忙TT下面是代码 clock.java
package com.app.jtj.clock; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.text.format.Time; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * Created by Administrator on 2015/11/18. */ public class Clock extends SurfaceView implements SurfaceHolder.Callback,Runnable{ private int w,h=0; private int mCP=0; private int xxxR=0; private int xxR=0; private int xR=0; private int R=0; private int mR=0; private float du=0; private Time t=new Time(); private Time tP=new Time(); private SurfaceHolder sfh; private Paint paint; private Canvas canvas; private boolean flag=true; private Thread th; public Clock(Context context) { super(context); sfh = this.getHolder(); sfh.addCallback(this); } private void init() { mCP= (int) (w * 0.5); xxxR = (int) ( w * 0.93 * 0.5); xxR = (int) ( w * 0.90 * 0.5); xR = (int) ( w * 0.85 * 0.5); R = (int) ( w * 0.80 * 0.5); mR = (int) ( w * 0.75 * 0.5); } private void initView() { //canvas.drawColor(Color.WHITE); //画圆 paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setARGB(255, 150, 150, 150); canvas.drawCircle(mCP, mCP, xR, paint); paint.setStrokeWidth(1); paint.setARGB(255, 150, 150, 150); canvas.drawCircle(mCP, mCP, R, paint); //刻度 paint.setStrokeWidth(10); for (int i=0;i<12;i++){ canvas.drawLine(mCP,mCP-xR,mCP,mCP-xxR,paint); canvas.rotate(30,mCP,mCP); } paint.setStrokeWidth(10); paint.setARGB(255, 20, 20, 20); for (int i=0;i<4;i++){ canvas.drawLine(mCP,mCP-xR,mCP,mCP-xxxR,paint); canvas.rotate(90,mCP,mCP); } //日期时间 tP.setToNow(); t.setToNow(); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setTextSize((float) (w * 0.05)); paint.setStrokeWidth(1); int month=tP.month+1; String str1=tP.year + "."+month+"."+tP.monthDay; float numb1=paint.measureText(str1); canvas.drawText(str1, mCP-numb1/2, (float) (w*0.30), paint); str1=getWeek(tP); numb1=paint.measureText(str1); canvas.drawText(str1, mCP-numb1/2, (float) (w*0.70), paint); } private String getWeek(Time t) { String str2=""; switch (t.weekDay){ case 0:str2="星期日";break; case 1:str2="星期一";break; case 2:str2="星期二";break; case 3:str2="星期三";break; case 4:str2="星期四";break; case 5:str2="星期五";break; case 6:str2="星期六";break; } return str2; } @Override public void run() { } @Override public void surfaceCreated(SurfaceHolder holder) { w=this.getWidth(); h = this.getHeight(); flag=true; paint =new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); for (int i=0;i<2;){ canvas=sfh.lockCanvas(new Rect(0,0,w,h)); if (canvas!=null){ i++; canvas.drawColor(Color.WHITE); sfh.unlockCanvasAndPost(canvas); } } for (int i=0;i<2;) { canvas = sfh.lockCanvas(); if (canvas!=null) { init(); Log.i("ttt", "1"); initView(); i++; sfh.unlockCanvasAndPost(canvas); } } canvas=sfh.lockCanvas(); sfh.unlockCanvasAndPost(canvas); canvas=sfh.lockCanvas(); sfh.unlockCanvasAndPost(canvas); canvas=sfh.lockCanvas(); sfh.unlockCanvasAndPost(canvas); canvas=sfh.lockCanvas(); sfh.unlockCanvasAndPost(canvas); //th = new Thread(this); //th.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder){ flag=false; } }
activity里就加了
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Clock(this));
解决方案
无报错,log.i,"ttt"有打印,也就是方法有执行
时间: 2025-01-26 13:03:00