android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。
想写一个复杂一点的游戏,是必须用到SurfaceView来开发的,不要老想着用Layout和view去实现,不要将某个游戏
中的对象做成一个组件来处理。应该尽量想着在Canvas(画布)中画出游戏戏中的背景、人物、动画等.
android 开发文档
引用
public void invalidate()
Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
public void postInvalidate ()
Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread.
示例:
gameview
- public class GameView extends View
- {
- int miCount = 0;
- int y = 0;
- public GameView(Context context)
- {
- super(context);
- }
- //
- //具体绘图内容我们紧接着就会讲
- public void onDraw(Canvas canvas)
- {
- if (miCount < 100)
- {
- miCount++;
- }
- else
- {
- miCount = 0;
- }
- //绘图
- Paint mPaint = new Paint();
- switch (miCount%4)
- {
- case 0:
- mPaint.setColor(Color.BLUE);
- break;
- case 1:
- mPaint.setColor(Color.GREEN);
- break;
- case 2:
- mPaint.setColor(Color.RED);
- break;
- case 3:
- mPaint.setColor(Color.YELLOW);
- break;
- default:
- mPaint.setColor(Color.WHITE);
- break;
- }
- //绘制矩形--后面我们将详细讲解
- canvas.drawRect((320-80)/2, y, (320-80)/2+80, y+40, mPaint);
- }
- }
第一个重绘方法
- public class Activity01 extends Activity
- {
- private static final int REFRESH = 0x000001;
- /* 声明GameView类对象 */
- private GameView mGameView = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- /* 实例化GameView对象 */
- this.mGameView = new GameView(this);
- // 设置显示为我们自定义的View(GameView)
- setContentView(mGameView);
- // 开启线程
- new Thread(new GameThread()).start();
- }
- Handler myHandler = new Handler()
- {
- //接收到消息后处理
- public void handleMessage(Message msg)
- {
- switch (msg.what)
- {
- case Activity01.REFRESH:
- mGameView.invalidate();
- break;
- }
- super.handleMessage(msg);
- }
- };
- class GameThread implements Runnable
- {
- public void run()
- {
- while (!Thread.currentThread().isInterrupted())
- {
- Message message = new Message();
- message.what = Activity01.REFRESH;
- //发送消息
- Activity01.this.myHandler.sendMessage(message);
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException e)
- {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
- //详细事件处理见第三章
- //当然这些事件也可以写在GameView中
- //触笔事件
- public boolean onTouchEvent(MotionEvent event)
- {
- return true;
- }
- //按键按下事件
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- return true;
- }
- //按键弹起事件
- public boolean onKeyUp(int keyCode, KeyEvent event)
- {
- switch (keyCode)
- {
- //上方向键
- case KeyEvent.KEYCODE_DPAD_UP:
- mGameView.y-=3;
- break;
- //下方向键
- case KeyEvent.KEYCODE_DPAD_DOWN:
- mGameView.y+=3;
- break;
- }
- return false;
- }
- public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
- {
- return true;
- }
- }
第二种方法
- class GameThread implements Runnable
- {
- public void run()
- {
- while (!Thread.currentThread().isInterrupted())
- {
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException e)
- {
- Thread.currentThread().interrupt();
- }
- //使用postInvalidate可以直接在线程中更新界面
- mGameView.postInvalidate();
- }
- }
- }
浙公网安备 33010602011771号