Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android學習---Gallery畫廊視圖

android學習---Gallery畫廊視圖

編輯:關於Android編程

Gallery與Spinner有共同父類:AbsPinner,說明Gallery與Spinner都是一個列表框。它們之間的區別在於Spinner顯示的是一個垂直的列表選擇框,而Gallery顯示的是一個水平的列表選擇框。Spinner的作用是供用戶選擇,而Gallery則允許用戶通過拖動查看上一個,下一個。

Gallery用法與Spinner的用法形似,只要為它提供一個內容Adapter就可以了。Adapter的getView方法返回View作為Gallery列表的列表項。如果程序需要監控Gallery選擇項的改變,可以添加OnItemSelectedListener監聽即可。


Gallery 的xml屬性

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPs/Cw+bNqLn90ru49rvDtcbGrMD919PAtMrsz6RHYWxsZXJ5PC9wPgo8cD48YnI+CjwvcD4KPHA+o6gxo6lhY3Rpdml0eV9tYWluLnhtbCAgICAgsry+1tK7uPZJbWFnZVN3aXRjaGVyILrNR2FsbGVyeSAgICAgICAgIEltYWdlU3dpdGNoZXLTw9Paz9TKvkdhbGxlcnnRodbQtcTNvMasPGJyPgo8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">

(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
				
			}
		});
		
	}

}

運行效果如下:


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