天天看點

android學習---Gallery畫廊視圖

    Gallery與Spinner有共同父類:AbsPinner,說明Gallery與Spinner都是一個清單框。它們之間的差別在于Spinner顯示的是一個垂直的清單選擇框,而Gallery顯示的是一個水準的清單選擇框。Spinner的作用是供使用者選擇,而Gallery則允許使用者通過拖動檢視上一個,下一個。

    Gallery用法與Spinner的用法形似,隻要為它提供一個内容Adapter就可以了。Adapter的getView方法傳回View作為Gallery清單的清單項。如果程式需要監控Gallery選擇項的改變,可以添加OnItemSelectedListener監聽即可。

  Gallery 的xml屬性

android學習---Gallery畫廊視圖

下面通過一個幻燈片例子來熟悉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;

	@Override
	protected 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() {
			
			@Override
			public 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() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				//建立imageview
				ImageView 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;
			}
			
			@Override
			public long getItemId(int position) {
				return position;
			}
			
			@Override
			public Object getItem(int position) {
				return position;
			}
			
			@Override
			public int getCount() {
				return images.length;
			}
		};
		
		//給Gallery設定擴充卡
		gallery.setAdapter(adapter);
		//添加事件
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				imgSwt.setImageResource(images[position]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

}
           

運作效果如下:

android學習---Gallery畫廊視圖