ImageSwitcher和Gallery组件的应用

阶段一:进行主界面的布局(如下图)

                                    

具体代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#55000000"
        android:spacing="10dp" />
</RelativeLayout>

阶段二:在MainActivity中查找组件并进行相应的事件处理,具体代码如下:

package cn.bzu.imageswitchergallery;

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

public class MainActivity extends Activity implements ViewFactory,
		OnItemClickListener {
	private ImageSwitcher imageSwitcher;
	private Gallery gallery;
	// 存放大图的数组
	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.activity_main);
		// 得到组件
		imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);
		imageSwitcher.setFactory(this);
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		gallery = (Gallery) this.findViewById(R.id.gallery1);
		gallery.setAdapter(new ImageAdapter());
		//设置默认选择的图片
		gallery.setSelection(imagesSmall.length/2);
		//注册事件监听器
		gallery.setOnItemClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int postion,
			long id) {
		imageSwitcher.setImageResource(imagesLarge[postion]);

	}

	// 重写视图工厂中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置
	@Override
	public View makeView() {
		ImageView imageView = new ImageView(MainActivity.this);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return imageView;
	}

	/**
	 * 负责产生gallery中的图片
	 */
	private class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;;
		public ImageAdapter(){
			TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery);
		    mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
		    typedArray.recycle();
		}

		// 返回图片的个数,比如你想得到图片的个数
		@Override
		public int getCount() {
			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(MainActivity.this);
			// 设置imageView中的图像资源
			imageView.setImageResource(imagesSmall[position]);
			/*// 设置图像大小尺寸自适应
			imageView.setAdjustViewBounds(true);*/
			imageView.setBackgroundResource(mGalleryItemBackground);
			return imageView;
		}
	}
}

结果显示:

                          

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值