標籤:android blog http java os io ar 2014
Gallery與Spinner有共同父類:AbsPinner,說明Gallery與Spinner都是一個列表框。它們之間的區別在於Spinner顯示的是一個垂直的列表選擇框,而Gallery顯示的是一個水平的列表選擇框。Spinner的作用是供使用者選擇,而Gallery則允許使用者通過拖動查看上一個,下一個。
Gallery用法與Spinner的用法形似,只要為它提供一個內容Adapter就可以了。Adapter的getView方法返回View作為Gallery列表的清單項目。如果程式需要監控Gallery選擇項的改變,可以添加OnItemSelectedListener監聽即可。
Gallery 的xml屬性
下面通過一個投影片例子來熟悉Gallery
(1)activity_main.xml 布局一個ImageSwitcher 和Gallery ImageSwitcher用於顯示Gallery選中的圖片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageSwitcher android:id="@+id/imgSwt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:unselectedAlpha="0.7" android:spacing="2pt"/></LinearLayout>
(2)MainActivity.java
package com.example.gallery;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.RadioGroup.LayoutParams;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {//定義組件private ImageSwitcher imgSwt = null;private Gallery gallery = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//擷取布局組件imgSwt = (ImageSwitcher) findViewById(R.id.imgSwt);gallery = (Gallery) findViewById(R.id.gallery);//圖片final int images[] = new int[]{R.drawable.name01,R.drawable.name02,R.drawable.name03,R.drawable.name04,R.drawable.name05,R.drawable.name06,R.drawable.name07,R.drawable.name08,R.drawable.name09,R.drawable.name10,R.drawable.name11,R.drawable.name12};//設定圖片轉場效果imgSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));imgSwt.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//設定ViewFactory對象imgSwt.setFactory(new ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(MainActivity.this);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(350,350));return imageView;}});//建立BaseAdapter對象,負責提供Gallery顯示所有映像BaseAdapter adapter = new BaseAdapter() {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//建立imageviewImageView imageView = new ImageView(MainActivity.this);imageView.setImageResource(images[position]);imageView.setScaleType(ImageView.ScaleType.FIT_XY);imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));return imageView;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic int getCount() {return images.length;}};//給Gallery設定適配器gallery.setAdapter(adapter);//添加事件gallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {imgSwt.setImageResource(images[position]);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {// TODO Auto-generated method stub}});}}
運行效果如下: