安卓自定义控件(循环Gallery)


创建Gallery控件,可实现所有显示图像的循环切换



源码下载http://download.csdn.net/detail/scimence/9027453


package com.sci.circulargallary;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_main);
		
		// 从图像资源创建循环Gallery,并添加至当前Activity中
		int[] pics = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 };
		setContentView(new CircularGallery(this, pics));
	}
}


/**
 * 2015-8-19下午3:32:21
 * wangzhongyuan
 */

package com.sci.circulargallary;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
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.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;


/**
 * CircularGallery 循环Gallery,包含n张子图像,显示为2n张子图,在【0.5n, 1.5n】循环切换
 * -----
 * 2015-8-19 下午3:32:21 
 * wangzhongyuan
 */
public class CircularGallery extends RelativeLayout
{
	private Context context;		// 控件所处的上下文环境
	private Gallery gallery;
	private ImageAdapter adapter;
	private int select;				// 记录当前选中的子图像索引
	
	private int[] pics;
	
	/**
	 * 获取当前选中的子控件id = [0, pics.length)
	 */
	public int selected()
	{
		return select;
	}
	
	/**
	 * 选中子控件变动时调用该函数,子类可重写该函数,执行子控件选项变动逻辑
	 */
	public void selecteChanged()
	{
		// if(selected() == 0) ...;
		// else if(selected() == 1) ...;
	}
	
	/**
	 * 创建循环Gallery
	 * pics = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5};
	 */
	public CircularGallery(Context context, int[] pics)
	{
		super(context);
		this.context = context;
		this.pics = pics;
		
		creatMainView();
	}
	
	// 创建CircularImageView的子控件,以代码布局的方式显示PicId对应的图像
	private void creatMainView()
	{
		int space = 5;									// 子图像之间的间隔
		int w2 = 160, h2 = 220;							// CircularGallery中展示的图像大小
		int w = w2 * 5 + space * 6, h = h2 + space * 2;	// CircularGallery整体大小
		
		// 控件主体部分
		RelativeLayout body = new RelativeLayout(context);	// 创建一个相对布局的视图
		body.setBackgroundColor(Color.GRAY);				// 为其设置背景色
		
		// 添加主体部分到主界面
		RelativeLayout.LayoutParams paramsBody = new RelativeLayout.LayoutParams(w, h);
		paramsBody.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		this.addView(body, paramsBody);
		
		// 添加Gallery到
		gallery = new Gallery(context);
		RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(w, h2);
		parms.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		body.addView(gallery, parms);
		
		adapter = new ImageAdapter(context, pics, w2, h2);
		gallery.setAdapter(adapter); 					// gallery添加ImageAdapter图片资源
		gallery.setGravity(Gravity.CENTER_HORIZONTAL); 	// 设置水平居中显示
		gallery.setSelection(pics.length);				// 设置起始图片显示位置(可以用来制作gallery循环显示效果)
		gallery.setUnselectedAlpha(0.3f); 				// 设置未选中图片的透明度
		gallery.setSpacing(5); 							// 设置图片之间的间距
		
		gallery.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
//				Toast.makeText(context, "图片 " + position, Toast.LENGTH_SHORT).show();
				
				//记录选择的图像索引
				select = position;
				if(select >= pics.length) select -= pics.length;	
				Toast.makeText(context, "图片 " + select, Toast.LENGTH_SHORT).show();
			}
		});
		
		gallery.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{
				int len = pics.length;
				
				// 使得所有的图像在[0.5, 1.5)*len之间,无限循环切换
				int first = len / 2, last = first + len;
				while (position < first)
					position += len;
				while (position >= last)
					position -= len;
				
				gallery.setSelection(position);
//				adapter.notifyDataSetChanged();
				
				//记录选择的图像索引
				select = position;
				if(select >= len) select -= len;	
				
				selecteChanged();
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> parent)
			{}
		});
	}
	
	/**
	 * ImageAdapter 用于获取子图像
	 * -----
	 * 2015-8-13 上午11:34:51 
	 * wangzhongyuan
	 */
	class ImageAdapter extends BaseAdapter
	{
		private Context context;
		public int[] pics;			// 子图像资源id
		int W, H;					// 子图像尺寸
				
		public ImageAdapter(Context context, int[] pics, int w, int h)
		{
			this.context = context;
			this.pics = pics;
			W = w;
			H = h;
		}
		
		// 子图像总数
		public int getCount()
		{
			return pics.length * 2;	// 在Gallery中设置2倍图片数组长度的图像,用于循环显示图像信息
		}
		
		// 获取图片位置
		public Object getItem(int index)
		{
			return pics[index];
		}
		
		// 获取图片ID
		public long getItemId(int index)
		{
			return index;
		}
		
		// 获取index对应的子View
		public View getView(int index, View convertView, ViewGroup parent)
		{
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(pics[index % pics.length]);
			
			imageView.setLayoutParams(new Gallery.LayoutParams(W, H));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			return imageView;
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值