更新界面 void android.view.View.invalidate()

来源:互联网 发布:金蝶软件系统参数 编辑:程序博客网 时间:2024/05/02 05:07
/*Invalidate the whole view. If the view is visible, onDraw(android.graphics.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(). *//*Activity01.java*/import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.KeyEvent;import android.view.MotionEvent;public class Activity01 extends Activity{private static final intREFRESH= 0x000001;/* 声明GameView类对象 */private GameViewmGameView= null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/* 实例化GameView对象 */this.mGameView = new GameView(this);// 设置显示为我们自定义的View(GameView)setContentView(mGameView);// 开启线程new Thread(new GameThread()).start();}HandlermyHandler= 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();}}}}/** * 当然可以将GameThread类这样写 * 同样可以更新界面,并且不在需要 * Handler在接受消息class GameThread implements Runnable{public void run(){while (!Thread.currentThread().isInterrupted()){try{Thread.sleep(100);}catch (InterruptedException e){Thread.currentThread().interrupt();}//使用postInvalidate可以直接在线程中更新界面mGameView.postInvalidate();}}}*///详细事件处理见第三章//当然这些事件也可以写在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;}}
//GameView.java
import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.View;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); }}


                                             
0 0
原创粉丝点击