天天看點

android中畫廊視圖Gallery和ImageSwitcher元件的使用

随時随地閱讀更多技術實戰幹貨,擷取項目源碼、學習資料,請關注源代碼社群公衆号(ydmsq666)、Q技術交流群(183198395)。

android中畫廊視圖Gallery和ImageSwitcher元件的使用
android中畫廊視圖Gallery和ImageSwitcher元件的使用
android中畫廊視圖Gallery和ImageSwitcher元件的使用

Gallery能夠水準方向顯示其内容,一般用來浏覽圖檔,被選中的選項位于中間,并且可以響應事件顯示資訊,下面結合ImageSwitcher元件來實作一個通過縮略圖來浏覽照片的程式,代碼如下:

Activity:

package com.lovo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class TestGalleryActivity extends Activity {
	// 聲明ImageSwitcher
	private ImageSwitcher mSwitcher;
	// 圖檔數組ID
	private int[] m = { R.drawable.image1, R.drawable.image2,
			R.drawable.image3, R.drawable.image4, R.drawable.image5,
			R.drawable.image6, R.drawable.image7 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 設定視窗特征無标題
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 獲得ImageSwitcher對象
		mSwitcher = (ImageSwitcher) findViewById(R.id.swither);
		// 為ImageSwitcher設定工廠
		mSwitcher.setFactory(new ViewFactory() {
			@Override
			public View makeView() {
				// 建立imageView對象
				ImageView imageView = new ImageView(TestGalleryActivity.this);
				// 設定背景顔色
				imageView.setBackgroundColor(0xFF000000);
				// 設定精度類型
				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				// 設定布局參數
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				return imageView;

			}
		});
		// 設定出現和離開的動畫效果
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
		// 獲得Gallery對象
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// 設定圖檔資源
				mSwitcher.setImageResource(m[arg2]);
			}
		});
		// 為Gallery添加擴充卡
		gallery.setAdapter(new ImageAdapter(this));

	}

	class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return m.length;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// 執行個體化ImageView對象
			ImageView imageView = new ImageView(mContext);
			// 設定圖檔資源
			imageView.setImageResource(m[arg0]);
			// 設定邊界對齊
			imageView.setAdjustViewBounds(true);
			// 設定布局參數
			imageView.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			// 設定背景資源
			imageView.setBackgroundResource(R.drawable.about_background_land);
			return imageView;
		}
	}
}
           

布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageSwitcher
        android:id="@+id/swither"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#55000000"
        android:gravity="center_vertical"
        android:spacing="16dp" >
    </Gallery>

</RelativeLayout>
           

 附上圖檔效果:

android中畫廊視圖Gallery和ImageSwitcher元件的使用