ImageSwitcher和Gallery的使用——Android学习笔记

package com.cellcom;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

//显示画像效果
public class ImageShowActivity extends Activity implements
		ViewSwitcher.ViewFactory {

	private ImageSwitcher mSwitcher;
	private Gallery gallery;
	private Context mContext;

	private Integer[] mImageIds = { 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,
			R.drawable.sample_8, R.drawable.sample_9, R.drawable.sample_10,
			R.drawable.sample_11 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_show);
		setTitle("浏览图片");
		mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
		gallery = (Gallery) findViewById(R.id.gallery);

		mSwitcher.setFactory(this);
		// 设置大图的效果 这里是渐变出现和渐变消失的效果
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		gallery.setAdapter(new ImageAdapter(this));
		gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

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

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

			}

		});
	}

	public class ImageAdapter extends BaseAdapter {

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

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

		@Override
		public Object getItem(int position) {

			return position;
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);
			i.setImageResource(mImageIds[position]);
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			i.setBackgroundResource(R.drawable.picture_frame);
			return i;
		}

	}

	@Override
	public View makeView() {
		ImageView i = new ImageView(this);
		// 设置整个activity的背景颜色
		i.setBackgroundColor(Color.YELLOW);
		// 设置大图的位置,
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		// 设置大图片的大小,宽度等
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return i;
	}
}

  

 

xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/widget34"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent" >
 6 
 7     <Gallery
 8         android:id="@+id/gallery"
 9         android:layout_width="fill_parent"
10         android:layout_height="110px"
11         android:layout_alignParentLeft="true"
12         android:layout_marginTop="10px" >
13     </Gallery>
14 
15     <ImageSwitcher
16         android:id="@+id/imageswitch"
17         android:layout_width="90px"
18         android:layout_height="90px"
19         android:layout_alignBottom="@+id/gallery"
20         android:layout_alignParentTop="true"
21         android:layout_centerHorizontal="true" >
22     </ImageSwitcher>
23 
24 </RelativeLayout>

posted on 2012-10-19 11:31  liyajun2012  阅读(247)  评论(0编辑  收藏  举报

导航