Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android開發 簡單相冊查看器

Android開發 簡單相冊查看器

編輯:關於android開發

  1、創建工程:HandlerTest
  2、准備程序中需要的圖片資源,將圖片資源存放在res/drawable-mdpi目錄下
  3、編寫布局文件main.xml:代碼如下:
      <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
  <TextView 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello"
      />
      <ImageView
       android:id="@+id/myImageView"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:src="@drawable/a"
       android:layout_gravity="center"
      />
  </LinearLayout>
  4、開發換圖的線程。在目錄下新建一個MyThread.java文件。代碼如下:
     package com.handlerTest02;

  public class MyThread extends Thread{
      HandlerTest02 handlerTestActivity;
      int what=1;
      public MyThread( HandlerTest02 handlerTestActivity){
          this.handlerTestActivity=handlerTestActivity;
      }
      @Override
      public void run() {
          while(true){
              //發送消息
              handlerTestActivity.myHandler.sendEmptyMessage((what++)%7);
              try{
                  Thread.sleep(2000); //休眠兩秒鐘
              }catch(Exception e){
                  e.printStackTrace(); //打印異常消息
              }
          }
      }

  }
  5、開發主邏輯代碼。代碼如下:
   package com.handlerTest02;

  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.widget.ImageView;

  public class HandlerTest02 extends Activity {
      /** Called when the activity is first created. */
      ImageView myImageView;
      Handler myHandler=new Handler(){
          public void handleMessage(Message msg){
              switch(msg.what){
              case 0:
                   myImageView.setImageResource(R.drawable.a);
                   break;
              case 1:
                  myImageView.setImageResource(R.drawable.b);
                  break;
              case 2:
                  myImageView.setImageResource(R.drawable.c);
                  break;
              case 3:
                  myImageView.setImageResource(R.drawable.d);
                  break;
              case 4:
                  myImageView.setImageResource(R.drawable.e);
                  break;
              case 5:
                  myImageView.setImageResource(R.drawable.f);
                  break;
              case 6:
                  myImageView.setImageResource(R.drawable.g);
                  break;
              }   
              super.handleMessage(msg);
          }
      };
     
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          myImageView=(ImageView)findViewById(R.id.myImageView);
          MyThread myThread=new MyThread(this);
          myThread.start();
      }
  }

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