ImageSwitcher_Gallery的使用

1.页面效果

2.页面布局的设计main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ImageSwitcher android:layout_alignParentLeft="true"
		android:id="@+id/imageSwitcher1" android:layout_height="fill_parent"
		android:layout_width="fill_parent" android:layout_alignParentTop="true"></ImageSwitcher>
	<Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="60dip"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"  android:gravity="center" android:spacing="10dip"/>
</RelativeLayout>

3.功能的实现

package cn.edu.bzu.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
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.ViewSwitcher.ViewFactory;

public class ImageSwitcherGalleryActivity extends Activity {
	ImageSwitcher imageSwitcher = null;
	Gallery gallery = null;
	// 存放大图的数组
	private int[] imagesLarge = { R.drawable.sample_0, R.drawable.sample_1,
			R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
			R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };
	// 存放小图的数组
	private int[] imagesSmall = { R.drawable.sample_thumb_0,
			R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
			R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
			R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,
			R.drawable.sample_thumb_7 };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		// 得到组件
		imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);
		gallery = (Gallery) this.findViewById(R.id.gallery);

		imageSwitcher.setFactory(new MyViewFactory());
		// imageSwitcher.setImageResource(imagesLarge[imagePostion]);
		gallery.setAdapter(new ImageAdapter(this));
		gallery.setSelection(imagesSmall.length/2);
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

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

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

	}

	private class MyViewFactory implements ViewFactory {

		@Override
		public View makeView() {
			ImageView imageView = new ImageView(
					ImageSwitcherGalleryActivity.this);
			imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			return imageView;
		}

	}

	private class ImageAdapter extends BaseAdapter {
		private Context context;
		int mGalleryItemBackground;
		public ImageAdapter(Context context) {
			this.context = context;
			TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
			mGalleryItemBackground = a.getResourceId(
					R.styleable.HelloGallery_android_galleryItemBackground, 0);
			a.recycle();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return imagesSmall.length;
		}

		@Override
		public Object getItem(int position) {
			return imagesSmall[position];
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(
					ImageSwitcherGalleryActivity.this);
			imageView.setImageResource(imagesSmall[position]);
			// imageView.setAdjustViewBounds(true);
			imageView.setBackgroundResource(mGalleryItemBackground);
			return imageView;
		}

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值