android 的线程绘制View(简单列子)

package com.game.activity;


import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;
import android.graphics.Color;

import android.graphics.Paint;

import android.os.Bundle;

import android.util.DisplayMetrics;

import android.view.SurfaceHolder;
import android.view.SurfaceView;

import android.view.Window;
import android.view.WindowManager;


public class GameActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
  DisplayMetrics dm = new DisplayMetrics();
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  //获取屏幕信息
  getWindowManager().getDefaultDisplay().getMetrics(dm);

  int w = dm.widthPixels;

  int h = dm.heightPixels;
  final MyView myView = new MyView(this ,w ,h) ;
  setContentView(myView);

 }
 class MyView extends SurfaceView implements SurfaceHolder.Callback,Runnable{   
  SurfaceHolder holder=null;    
  Paint paint;
  int w = 0 ;
  int h = 0 ;
  int radius = 280 ;
  private float x,y;  
  private float s = 0.1f ;
  private boolean xis,yis = false ;
  private long time = 0 ;
  Bitmap bitmap ;
  public MyView(Context context , int w , int h) {   
   super(context);        
   // TODO Auto-generated constructor stub
   this.w = w ;
   this.h  = h ;
   holder=getHolder();       
   holder.addCallback(this);    
   paint=new Paint(Paint.ANTI_ALIAS_FLAG); 
   paint.setColor(Color.LTGRAY); 
   paint.setTextSize(40) ;
   this.setFocusable(true);        
  }        

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

   Thread t=new Thread(this);        
   t.start();      
  }         
  @Override      
  public void surfaceDestroyed(SurfaceHolder holder) {  
   // TODO Auto-generated method stub      
   isRunning=false;      
  }    

  private void paint() {
   try{
    SimpleDateFormat format = new SimpleDateFormat("yy年MM月dd日") ;
    SimpleDateFormat formats = new SimpleDateFormat("hh时mm分ss秒") ;
    Date d = new Date(System.currentTimeMillis() );
    String date = format.format(d) ;
    String dates = formats.format(d) ;

    Canvas canvas=holder.lockCanvas();  
    //刷屏            
    canvas.drawColor(Color.BLACK);  
    canvas.drawText(date, x, y, paint) ;
    canvas.drawText(dates, x, y+40, paint) ;
    holder.unlockCanvasAndPost(canvas);
   }catch(Exception e){
    System.out.println("huizhicuowu");
   }
  }                 
  boolean isRunning=true;  
  @Override     
  public void run() {    
   // TODO Auto-generated method stub  
   long time = System.currentTimeMillis() ;
   while (isRunning) {
    move(System.currentTimeMillis() -time);        

   }      
  }             

  private void move(long time){
   if(time- this.time > 10){
    if(xis)
     x=  (float) (x+ (time- this.time)*s);
    else
     x =(float) (x - (time- this.time)*s);
    if(yis) 
     y= (float) (y+ (time- this.time)*s);
    else
     y= (float) (y- (time- this.time)*s);
    if(x>=w-radius)
     xis = false ;
    if(x<=0)
     xis = true ;
    if(y>=h-40)
     yis = false ;
    if(y<= 40)
     yis = true ;
    this.time = time ;

    paint();
   }
  }


 } 

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值