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

来源:互联网 发布:淘宝网店手机实名认证 编辑:程序博客网 时间:2024/05/20 20:56


创建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{@Overrideprotected 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(){@Overridepublic 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(){@Overridepublic 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();}@Overridepublic void onNothingSelected(AdapterView<?> parent){}});}/** * ImageAdapter 用于获取子图像 * ----- * 2015-8-13 上午11:34:51  * wangzhongyuan */class ImageAdapter extends BaseAdapter{private Context context;public int[] pics;// 子图像资源idint 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];}// 获取图片IDpublic long getItemId(int index){return index;}// 获取index对应的子Viewpublic 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;}}}





0 0
原创粉丝点击