public void PaintButton(Canvas canvas,Paint m_Paint) // 绘制下方方向导航按键和升级位图
{
if(timeTask == 800 || timeTask == 500 || timeTask == 200)
{
if(log_X == 0)
{
canvas.drawBitmap(m_GameResources.Level,170,362, m_Paint);
log_X = 1;
}
}
}
------解决思路----------------------
if(log_X == 0)
{
canvas.drawBitmap(m_GameResources.Level,170,362, m_Paint);
//这样不等于只画一次了么
log_X = 1;
}
问题是你想用什么来控制,如果用变量,那就声明个变量(假设你没几十毫秒调用重绘方法),然后加加,大于多少的时候就不画了,例如:
private int count = 0;
public void PaintButton(Canvas canvas,Paint m_Paint) // 绘制下方方向导航按键和升级位图
{
if(timeTask == 800
------解决思路----------------------
timeTask == 500
------解决思路----------------------
timeTask == 200){
if(count < 100){
canvas.drawBitmap(m_GameResources.Level,170,362, m_Paint);
count ++;
}
}
}
------解决思路----------------------
用线程就行,然后时间自己控制,想多少就多少。
下面相当于一个自定义的View,绘制的时间可以用count控制,当然可以用其他方式
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
public abstract class MyView extends View {
protected final String TAG = "MyView";
private Thread spriteThread;
private boolean running = false;
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context) {
super(context);
}
private void init() {
running = false;
spriteThread = new Thread(run);
spriteThread.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
PaintButton(canvas, null);
}
private int count = 0;
public void PaintButton(Canvas canvas, Paint m_Paint) // 绘制下方方向导航按键和升级位图
{
if (count < 100) {
canvas.drawBitmap(m_GameResources.Level, 170, 362, m_Paint);
count++;// 相当于停止30毫秒
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
running = false;
}
private Runnable run = new Runnable() {
public void run() {
running = true;
while (running) {
mHandler.sendEmptyMessage(0);
try {
// 停止30毫秒
Thread.sleep(30);
} catch (InterruptedException e) {
break;
}
}
}
};
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
invalidate();
};
};
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (running) {
return;
}
init();
};
}
原理就是通过Thread+Handler来实现的,具体不明白可以网上查一下,资料和讲解都挺多的。