Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實現圖片瀏覽器範例

編輯:關於 Android 程式設計

本文提供一個基礎的 Android 圖片瀏覽器程式碼,參考 Google 原版加以改寫,涵蓋了主要的實作流程與方法,若要更完善仍需自行補充。程式碼中附有大量註解,能幫助新手快速理解,並使用了部分圖像資源。

主要功能程式碼如下:

 package com.android.coding; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.Gallery.LayoutParams; import android.widget.ViewSwitcher.ViewFactory; import android.widget.ImageSwitcher; import android.widget.ImageView; public class ViewPicturesActivity extends Activity { ImageSwitcher imageSwitcher; //聲明ImageSwitcher對象,圖片顯示區域 Gallery gallery; //聲明Gallery對象,圖片列表索引 int imagePosition; //標記圖片數組下標,用於循環顯示 //聲明圖片整型數組 private int[] images = { R.drawable.image1,R.drawable.image2, R.drawable.image3,R.drawable.image4, R.drawable.image5,R.drawable.image6, R.drawable.image7,R.drawable.image8, R.drawable.image9,R.drawable.image10, R.drawable.image11,R.drawable.image12, R.drawable.image13,R.drawable.image14, R.drawable.image15,R.drawable.image16, R.drawable.image17}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過控件的ID獲得imageSwitcher的對象 imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher); //設置自定義的圖片顯示工廠類 imageSwitcher.setFactory(new MyViewFactory(this)); //通過控件的ID獲得gallery的對象 gallery = (Gallery) findViewById(R.id.gallery); //設置自定義的圖片適配器 gallery.setAdapter(new ImageAdapter(this)); //實現被選中的事件監聽器 gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //通過求余數,循環顯示圖片 imageSwitcher.setImageResource(images[position%images.length]); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } //自定義圖片適配器,繼承BaseAdapter class ImageAdapter extends BaseAdapter{ private Context context; //定義上下文 //參數為上下文的構造方法 public ImageAdapter(Context context) { this.context = context; } //得到圖片的大小 @Override public int getCount() { //設置為整型的最大數 return Integer.MAX_VALUE; } //得到指定圖片的對象 @Override public Object getItem(int position) { return null; } //得到指定圖片的對象的ID @Override public long getItemId(int position) { return 0; } //顯示圖標列表 @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(context); //創建ImageView對象 iv.setImageResource(images[position%images.length]); //設置循環顯示圖片 iv.setAdjustViewBounds(true); //圖片自動調整顯示 //設置圖片的寬和高 iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); return iv; //返回ImageView對象 } } //自定義圖片顯示工廠類,繼承ViewFactory class MyViewFactory implements ViewFactory{ private Context context; //定義上下文 //參數為上下文的構造方法 public MyViewFactory(Context context) { this.context = context; } //顯示圖標區域 @Override public View makeView() { ImageView iv = new ImageView(context); //創建ImageView對象 iv.setScaleType(ImageView.ScaleType.FIT_CENTER); //圖片自動居中顯示 //設置圖片的寬和高 iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); return iv; //返回ImageView對象 } } } 

本文所述僅為主要功能程式碼部分,讀者可進一步加以完善。從這個圖片檢視器出發,還能延伸出許多實用的 Android 圖像操作功能,這些都是 Android 開發新手應當掌握的技巧。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved