Handler定時數行應用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/wall" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/wall" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/wall" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/wall" />

</LinearLayout>
package com.example.app22;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    Handler handler;
    Resources res;
    ImageView[] imageViews = new ImageView[4];
    int[] len = new int[4];
    int sw;// 螢幕(imageView)的寬度
    int iw;// 圖片(小綠人)的寬度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        res = this.getResources();
        for (int i = 0; i < imageViews.length; i++) {
            int id = res.getIdentifier("imageView" + (i + 1), "id",
                    this.getPackageName());
            imageViews[i] = (ImageView) this.findViewById(id);
        }
        handler = new Handler();
        handler.postDelayed(updateTask, 2000);

    }

    @Override//擷取畫面長度時要複寫的方法
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub

        if (hasFocus) {
            sw = imageViews[0].getWidth();
            iw = imageViews[0].getDrawable().getIntrinsicWidth();
            Log.e("MainActivity", "width=" + sw);
        }
        super.onWindowFocusChanged(hasFocus);
    }

    //擷取畫面長度第二種方法:
    /*
    OnPreDrawListener listener = new OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw() {
            // TODO Auto-generated method stub
            if (sw == -1)
            {
                sw = imageViews[0].getWidth();
                iw = imageViews[0].getDrawable().getIntrinsicWidth();
                Log.e("MainActivity", "width=" + sw);
            }
            return true;
        }
    };
    imageViews[0].getViewTreeObserver().addOnPreDrawListener(listener);
    imageViews[0].getViewTreeObserver().addOnWindowFocusChangeListener(listener);
    */


    Runnable updateTask = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.e("MainActivity", "執行了");

            if (race()) {
                handler.postDelayed(updateTask, 200);
            }
        }

        private boolean race() {
            Matrix m = new Matrix();    //也可以放到m.reset()這行,但會產生四個物件耗資源
            // TODO Auto-generated method stub
            for (int i = 0; i < imageViews.length; i++) {
                int n = (int) (Math.random() * 5) + 5;      // *5-->0~4 , +5-->5~9
                len[i] += n;        // 個別存放入四個跑的長度
                m.reset();          // 重製Matrix,不然2會累加到1,3會累加到2
                m.postTranslate(len[i], 0);     // 只要移動x軸
                imageViews[i].setImageMatrix(m);

                if (len[i] > sw - iw) {
                    String text = String.format("第%d隻贏了", i + 1);
                    Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT)
                            .show();
                    return false;
                }
            }
            return true;    // 代表要繼續移動
        }
    };

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        handler.removeCallbacks(updateTask);
        super.onDestroy();
    }

}