Gallery和imageSwitcher结合使用浏览图片(简单图片浏览器)

效果如下图,点击小图显示大图:

activity_image_switcher_and_gallery.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:id="@+id/imageswitch"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"></ImageSwitcher>

    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:layout_gravity="center"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

ImageSwitcherAndGalleryActivity.java中的代码:

package com.bzu.imageswitcher_gallery.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager.LayoutParams;

public class ImageSwitcherAndGalleryActivity extends Activity {
	// 存放大图片
	int[] bigImage = { 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 };
	// 存放小图片
	int[] smallImage = { 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);
		setContentView(R.layout.activity_image_switcher_and_gallery);
		final ImageSwitcher imageswitch = (ImageSwitcher) this
				.findViewById(R.id.imageswitch);
		Gallery gallery = (Gallery) this.findViewById(R.id.gallery);
		imageswitch.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				ImageView imageView = new ImageView(
						ImageSwitcherAndGalleryActivity.this);
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				return imageView;
			}
		});
		 //final ImageAdapter myImageAdapter=new ImageAdapter(this);
          gallery.setAdapter(new ImageAdapter(this));
          //imageswitch.setImageResource((int) myImageAdapter.getItemId(0));
          gallery.setSelection(smallImage.length/2);
          gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				imageswitch.setImageResource(bigImage[position]);
			}
		});
	}
	//定制适配器
	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() {
			return smallImage.length;
		}

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

		@Override
		public long getItemId(int position) {
			return position;
		}@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(
					ImageSwitcherAndGalleryActivity.this);
			imageView.setImageResource(smallImage[position]);
			imageView.setBackgroundResource(mGalleryItemBackground);
			return imageView;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_image_switcher_and_gallery,
				menu);
		return true;
	}

}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值