android Su易做图ceView使用例子
做游戏一般用Su易做图ceView
[java]
package com.himi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.Su易做图ceHolder.Callback;
import android.view.animation.Animation;
/**
* @author Himi
*/
public class MySu易做图ceView extends Su易做图ceView implements Callback, Runnable {// 备注1
private Su易做图ceHolder sfh;
private Thread th;
private Canvas canvas;
private Paint paint;
private int ScreenW, ScreenH;
public MySu易做图ceView(Context context) {
super(context);
th = new Thread(this);
sfh = this.getHolder();
sfh.addCallback(this); // 备注1
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
this.setKeepScreenOn(true);// 保持屏幕常亮
}
@Override
public void startAnimation(Animation animation) {
super.startAnimation(animation);
}
public void su易做图ceCreated(Su易做图ceHolder holder) {
ScreenW = this.getWidth();// 备注2
ScreenH = this.getHeight();
th.start();
}
private void draw() {
try {
canvas = sfh.lockCanvas(); // 得到一个canvas实例
canvas.drawColor(Color.WHITE);// 刷屏
canvas.drawText("Himi", 100, 100, paint);// 画文字文本
canvas.drawText("这就是简单的一个游戏框架", 100, 130, paint);
sfh.unlockCanvasAndPost(canvas); // 将画好的画布提交
} catch (Exception ex) {
} finally { // 备注3
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
}
public void run() {
while (true) {
draw();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
}
public void su易做图ceDestroyed(Su易做图ceHolder holder) {
// TODO Auto-generated method stub
}
}
补充:移动开发 , Android ,