用户界面View之Gallery



生活就像海洋,只有意志坚强的人,才能到达彼岸。 —— 马克思


本讲内容:Gallery 画廊 和 ImageSwitcher 图片选择器


一、Gallery 画廊4.0后已经过期了,Google建议使用HorizontalScrollView和ViewPager代替

二、 ImageSwitcher 和ImageView 类似,都可以用于显示图片,区别在于ImageSwitcher的效果更炫,它可以指定图片切换时的动画效果。可以理解成ImageView的选择器,它需要设置ViewFactory视图工厂接口,通常让ViewFactory的makeView()方法返回ImageView。


注意:

一、使之循环播放 

1、在ImageAdapter中的getCount() 方法中,修改返回值为无穷大 return Integer.MAX_VALUE 

2、设置imageview.setImageResource(imgs[position % imgs.length]); 取余

3、GalleryActivity中,使gallery显示图片的位置从中间开始显示gallery.setSelection(res.length*100);。由于起始位置如果是0,则向右滑动左侧将无法循环(此时左侧将为-1,超出了imgs[]数组的下边界),因此开始应设置起始位置为imgs.length的整数倍


二、事件

1、点击事件OnItemClickListener,是需要用手点击才触发,滑动时不触发

2、选中事件OnItemSelectedListener,是当图片滑到屏幕正中,则视为自动选中,在滑动的过程中会触发


示例一:


下面是res/layout/activity_main.xml 布局文件:

<LinearLayout 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"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/id_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageSwitcher
        android:id="@+id/id_imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


下面是ImageAdapter.java文件:

public class ImageAdapter extends BaseAdapter {
	private Context context;
	private int[] res;
	
	public ImageAdapter(Context context,int[] res) {
		this.context=context;
		this.res=res;
	}

	/**
	 * 返回适配器中数据的数量  
	 */
	public int getCount() {
		//返回一个很大的的数,使之可以循环显示
		return Integer.MAX_VALUE;
	}

	/**
	 * 获取数据中与指定索引对应的数据项 
	 */
	public Object getItem(int position) {
		return res[position];
	}

	/**
	 *  获取指定行对应的ID 
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 获取每一个Item的显示内容 
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView image=new ImageView(context);
		//使可以循环显示 position%res.length
		image.setBackgroundResource(res[position%res.length]);
		image.setLayoutParams(new Gallery.LayoutParams(200, 150));
		image.setScaleType(ScaleType.FIT_XY);
		return image;
	}
}


下面是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{
	private Gallery gallery;
	private ImageSwitcher is;
	private ImageAdapter adapter;
	
	private int[] res = { R.drawable.item1, R.drawable.item2,R.drawable.item3, R.drawable.item4, 
						  R.drawable.item5, R.drawable.item6,R.drawable.item7, R.drawable.item8,
						  R.drawable.item9,R.drawable.item10, R.drawable.item11, R.drawable.item12};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {
		gallery=(Gallery) findViewById(R.id.id_gallery);
		is=(ImageSwitcher) findViewById(R.id.id_imageSwitcher);
		adapter=new ImageAdapter(this, res);
		gallery.setAdapter(adapter);
		gallery.setSelection(res.length*100);
		gallery.setOnItemSelectedListener(this);
		
		//设置动画
		is.setFactory(this);
		is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
	}

	/**
	 * 当图片滑到屏幕正中,则视为自动选中
	 */
	public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
		is.setBackgroundResource(res[position%res.length]);
	}

	public void onNothingSelected(AdapterView<?> parent) {
		
	}

	/**
	 * 设置视图
	 */
	public View makeView() {
		ImageView image=new ImageView(this);
		image.setScaleType(ScaleType.FIT_CENTER);
		return image;
	}
}




Take your time and enjoy it 


Take your time and enjoy it 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值