Android控件之Gallery探究

Gallery組件主要用於橫向顯示圖像列表,不過按常規做法。Gallery組件只能有限地顯示指定的圖像。也就是說,如果爲Gallery組件指定了10張圖像,那麼當Gallery組件顯示到第10張時,就不會再繼續顯示了。這雖然在大多數時候沒有什麼關係,但在某些情況下,我們希望圖像顯示到最後一張時再重第1張開始顯示,也就是循環顯示。要實現這種風格的Gallery組件,就需要對GalleryAdapter對象進行一番改進。

以下通過Gallery模擬循環顯示圖像,在單擊某一個Gallery組件中的圖像時在下方顯示一個放大的圖像(使用ImageSwitcher組件)。

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">     <!-- android:unselectedAlpha: 設置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。 -->     <Gallery android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:spacing="10dip"         android:unselectedAlpha="1.2"         android:id="@+id/gallery"         android:layout_marginTop="30dp"/>     <ImageSwitcher android:id="@+id/imageSwitcher"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_marginTop="30dp" /> </LinearLayout>

GalleryActivity類

package com.ljq.ga; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.LinearLayout.LayoutParams; import android.widget.ViewSwitcher.ViewFactory; public class GalleryActivity extends Activity implements ViewFactory {     private Gallery gallery = null;     private ImageSwitcher imageSwitcher=null;     int[] imageIDs={             R.drawable.p1,R.drawable.p2,R.drawable.p3,             R.drawable.p4,R.drawable.p5,R.drawable.p6,             R.drawable.p7,R.drawable.p8 };               @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);         // 設置ImageSwitcher組件的工廠對象         imageSwitcher.setFactory(this);         // 設置ImageSwitcher組件顯示圖像的動畫效果         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                 android.R.anim.fade_in));               imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                 android.R.anim.fade_out));         gallery = (Gallery) findViewById(R.id.gallery);         ImageViewAdapter adapter=new ImageViewAdapter(GalleryActivity.this, imageIDs);         gallery.setAdapter(adapter);         gallery.setOnItemSelectedListener(new OnItemSelectedListener(){             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {                 //選中Gallery中某個圖像時,在ImageSwitcher組件中放大顯示該圖像                 imageSwitcher.setImageResource(imageIDs[position%imageIDs.length]);             }             public void onNothingSelected(AdapterView<?> arg0) {                              }                      });                  gallery.setOnItemClickListener(new OnItemClickListener(){             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                 Log.i("ljq", "parent="+parent.getClass().getName()); //Gallery                 Log.i("ljq", "view="+view.getClass().getName()); //ImageView                 Log.i("ljq", "position=" + position); //1                 Log.i("ljq", "id=" + id);//1                 Gallery gl=(Gallery)parent;                 ImageView iv=(ImageView)view;             }         });                       }     // ImageSwitcher組件需要這個方法來創建一個View對象(一般爲ImageView對象)     // 來顯示圖像     public View makeView() {         ImageView imageView = new ImageView(this);         imageView.setBackgroundColor(0xFF000000);         imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);         imageView.setLayoutParams(new ImageSwitcher.LayoutParams(                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));         return imageView;     } }

ImageViewAdapter自定義適配器

package com.ljq.ga; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; public class ImageViewAdapter extends BaseAdapter{     private int[] imageIDs=null;     private Context context=null;          public ImageViewAdapter(Context context, int[] imageIDs) {         this.context=context;         this.imageIDs=imageIDs;     }          //用於返回圖像總數,要注意的是,這個總數不能大於圖像的實際數(可以小於圖像的實際數),否則會拋出越界異常。     public int getCount() {         //優化一         //return imageIDs.length;         return Integer.MAX_VALUE;     }     public Object getItem(int position) {         return imageIDs[position];     }     public long getItemId(int position) {         return position;     }     //ScaleType的用法     //CENTER/center  按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示     //CENTER_CROP/centerCrop  按比例擴大圖片的size居中顯示,使得圖片長 (寬)等於或大於View的長(寬)     //CENTER_INSIDE/centerInside  將圖片的內容完整居中顯示,通過按比例縮小 或原來的size使得圖片長/寬等於或小於View的長/寬     //FIT_CENTER/fitCenter  把圖片按比例擴大/縮小到View的寬度,居中顯示     //FIT_END/fitEnd   把 圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置     //FIT_START/fitStart  把 圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置     //FIT_XY/fitXY  把圖片 不按比例 擴大/縮小到View的大小顯示     //MATRIX/matrix 用矩陣來繪製     public View getView(int position, View convertView, ViewGroup parent) {         ImageView iv = new ImageView(context);         //優化二,通過取餘來循環取得imageIDs數組中的圖像資源ID,取餘可以大大較少資源的浪費         iv.setImageResource(imageIDs[position%imageIDs.length]);         iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);         iv.setLayoutParams(new LinearLayout.LayoutParams(77,77));//把圖片縮小原來的60%         return iv;     } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章