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();
}
}
}
}