view动画实例

 
  
package game.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class GameTest extends Activity implements Runnable {
/** Called when the activity is first created. */
MyView myView;
Handler handler;
boolean bExit = false ;

@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
// 隐去电池等图标和一切修饰部分(状态栏部分)
this .getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 隐去标题栏(程序的名字)
this .requestWindowFeature(Window.FEATURE_NO_TITLE);
myView
= new MyView( this );
handler
= new Handler() {
@Override
public void handleMessage(Message msg) {
myView.invalidate();
}
};
setContentView(myView);
new Thread( this ).start();
}

@Override
public void run() {
while ( ! bExit) {
handler.sendEmptyMessage(
0 );
try {
Thread.sleep(
500 );
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

class MyView extends View {
private Paint paint;
private int left;
public MyView(Context context) {
super (context);
paint
= new Paint();
paint.setAntiAlias(
true ); // 设置画笔无锯齿(如果不设置可以看到效果很差)
this .setKeepScreenOn( true ); // 设置背景常亮
paint.setColor(Color.RED);
left
= 0 ;
}

@Override
public void draw(Canvas canvas) {
super .draw(canvas);
canvas.drawColor(Color.WHITE);
// 设置刷屏颜色
Rect rect = new Rect(left, 20 , left + 10 , 30 ); // 这里最后两个参数不是宽高、而是矩形右下角的坐标
canvas.drawRect(rect, paint);
left
+= 10 ;
if (left > 200 )
left
= 0 ;
}

}

转载于:https://www.cnblogs.com/pen-ink/archive/2011/02/18/1957782.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值