Android 开发弄一个倒计时的界面?
展开全部
用SurfaceView 做的一个倒计时的界面:
public class CountDownView extends SurfaceView implements Callback, Runnable{
//
private SurfaceHolder sfh;
//画布
private Canvas canvas;
//线程是否开起的标识
private boolean flag;
//背景画笔
Paint backgroundPaintpaint;
//绘制数字的画笔
Paint paintText;
//声明一条线程
private Thread th;
//二级计数器
long timeBox;
//倒计时数字
long time;
public void myDraw()
{
canvas = sfh.lockCanvas();
try {
//二级计数器自加
timeBox++;
if(canvas != null)
{
//绘制背景--全黑
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backgroundPaintpaint);
if(timeBox%8 == 0)
{
//50*8 毫秒绘制一个数字
if(time > 0)
{
time--;
}else{time = 9;}
}
canvas.drawText(String.valueOf(time), this.getWidth()/2, this.getHeight()/2+50, paintText);
}
} catch (Exception e) {
}finally
{
sfh.unlockCanvasAndPost(canvas);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
public CountDownView(Context context)
{
super(context);
sfh = this.getHolder();
sfh.addCallback(this);
canvas = new Canvas();
time = 9;
//二级计数器
timeBox = 0;
//设置背景常亮
this.setKeepScreenOn(true);
//
}
@Override
public void run()
{
while(flag)
{
long start = System.currentTimeMillis();
myDraw();
long end = System.currentTimeMillis();
try {
if(end - start < 50)
{
Thread.sleep(50 -(end-start));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//实例背景画笔
backgroundPaintpaint = new Paint();
//设置背景画笔为黑色
backgroundPaintpaint.setColor(Color.BLACK);
//实例数字画笔
paintText = new Paint();
//设置风格
paintText.setStyle(Paint.Style.STROKE);
//设置居中
paintText.setTextAlign(Paint.Align.CENTER);
//设置着色
paintText.setColor(Color.BLUE);
//设置大小
paintText.setTextSize((float) (38*7.5));
//标识线程开起
flag = true;
//实例线程
th = new Thread(this);
//开起线程
th.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//关闭线程
flag = false;
}
}
程序入口:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new CountDownView(this));
}
}
public class CountDownView extends SurfaceView implements Callback, Runnable{
//
private SurfaceHolder sfh;
//画布
private Canvas canvas;
//线程是否开起的标识
private boolean flag;
//背景画笔
Paint backgroundPaintpaint;
//绘制数字的画笔
Paint paintText;
//声明一条线程
private Thread th;
//二级计数器
long timeBox;
//倒计时数字
long time;
public void myDraw()
{
canvas = sfh.lockCanvas();
try {
//二级计数器自加
timeBox++;
if(canvas != null)
{
//绘制背景--全黑
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backgroundPaintpaint);
if(timeBox%8 == 0)
{
//50*8 毫秒绘制一个数字
if(time > 0)
{
time--;
}else{time = 9;}
}
canvas.drawText(String.valueOf(time), this.getWidth()/2, this.getHeight()/2+50, paintText);
}
} catch (Exception e) {
}finally
{
sfh.unlockCanvasAndPost(canvas);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
public CountDownView(Context context)
{
super(context);
sfh = this.getHolder();
sfh.addCallback(this);
canvas = new Canvas();
time = 9;
//二级计数器
timeBox = 0;
//设置背景常亮
this.setKeepScreenOn(true);
//
}
@Override
public void run()
{
while(flag)
{
long start = System.currentTimeMillis();
myDraw();
long end = System.currentTimeMillis();
try {
if(end - start < 50)
{
Thread.sleep(50 -(end-start));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//实例背景画笔
backgroundPaintpaint = new Paint();
//设置背景画笔为黑色
backgroundPaintpaint.setColor(Color.BLACK);
//实例数字画笔
paintText = new Paint();
//设置风格
paintText.setStyle(Paint.Style.STROKE);
//设置居中
paintText.setTextAlign(Paint.Align.CENTER);
//设置着色
paintText.setColor(Color.BLUE);
//设置大小
paintText.setTextSize((float) (38*7.5));
//标识线程开起
flag = true;
//实例线程
th = new Thread(this);
//开起线程
th.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//关闭线程
flag = false;
}
}
程序入口:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new CountDownView(this));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询