Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android 定時、刷新(一)

Android 定時、刷新(一)

編輯:初級開發

‍我們一共有2個類一個繼承了View用來畫圖 另外Activity類用來刷新我們的視圖 這2類分別是 StartView和 GameVIEw 
一、 StartView類: ‍package CityLife.VIEw;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.vIEw.Window;
import android.vIEw.WindowManager;
public class StartVIEw extends Activity
{
 private static final int REFRESH  = 0x000001;
 /* 聲明GameVIEw類對象 */
 private GameView mGameVIEw = null;
 Handler myHandler = new Handler() {   
         //接收到消息後處理   
         public void handleMessage(Message msg)
         {
             switch (msg.what){   
             case StartVIEw.REFRESH:  

              //刷新
              //注意這裡的刷新界面實際上是在UI 線程中執行的 不是另外開啟一個線程這裡要搞清楚   
                 mGameVIEw.invalidate();   
                 break;   
             }   
             super.handleMessage(msg);   
         }   
     };
    /** Called when the activity is first created. */
 @Override
    public void onCreate(Bundle savedInstanceState) 
    { 
     //設置全屏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.TYPE_STATUS_BAR,
                        WindowManager.LayoutParams.TYPE_STATUS_BAR);
        super.onCreate(savedInstanceState);
        setContentVIEw(R.layout.main);
        /* 實例化GameVIEw對象 */  
        this.mGameView = new GameVIEw(this);
        //設置顯示為我們自定義的View(GameVIEw)   
        setContentView(mGameVIEw);
        //開啟線程   
        new Thread(new GameThread()).start();
 }
 public class GameThread implements Runnable 
 {   
        public void run() 
        {   
            while (!Thread.currentThread().isInterrupted()) 
            {   
                Message message = new Message();   
                message.what = StartVIEw.REFRESH;   
                //發送消息   
                StartVIEw.this.myHandler.sendMessage(message);   
                try
                {   
                    Thread.sleep(100);   
                }
                catch (InterruptedException e)
                {   
                    Thread.currentThread().interrupt();   
                }   
            }   
        }
 }
}

二、 GameVIEw類: 

‍package CityLife.VIEw;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.view.VIEw;

public class GameView extends VIEw 
{
 int startDrawBgX, startDrawBgY;
 int startProgressX,startProgressY;
 int startRectX ,startRectY ;
 int rectW , rectH;
 Bitmap bmpStartBg;
 Bitmap bmpProgressBar;
 public GameVIEw(Context context) 
 {
  super(context);
  
   bmpStartBg = BitmapFactory.decodeResource(getResources(), R.drawable.startbg);
      startDrawBgY = (480 - bmpStartBg.getHeight()) / 2 - 40;
      startDrawBgX = (320 - bmpStartBg.getWidth()) / 2;
      //
      bmpProgressBar = BitmapFactory.decodeResource(getResources(), R.drawable.progressbg); 
      startProgressY = (480 - bmpProgressBar.getHeight()) / 2 - 15;
      startProgressX = (320 - bmpProgressBar.getWidth()) / 2;
      //
      startRectX = startProgressX;
      startRectY = startProgressY;
      rectW = startProgressX+bmpProgressBar.getWidth();
      rectH = startProgressY+bmpProgressBar.getHeight();  
  // TODO Auto-generated constructor stub
 }
 public void onDraw(Canvas canvas) 
 {  
  if(startRectX < startProgressX + bmpProgressBar.getWidth())
        {
         startRectX = startRectX + 2;
        }
  else
  {
   
  }
  //首先定義一個paint 
        Paint paint = new Paint(); 
        paint.setColor(UsedColor.startBgColor); 
        // 設置樣式-填充 
        paint.setStyle(Style.FILL); 
        // 繪制一個矩形 
        canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint); 
        /********繪圖**************/             
        //輸出背景圖片
        canvas.drawBitmap(bmpStartBg, startDrawBgX, startDrawBgY, paint);
        canvas.drawBitmap(bmpProgressBar, startProgressX, startProgressY, paint);
        paint.setColor(UsedColor.progressBgColor); 
        canvas.drawRect(new Rect(startRectX, startRectY, rectW, rectH), paint);
    }


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved