android ViewPager 替代 Gallery 实现手势切换

转载文章请注明出处:http://write.blog.csdn.net/postedit/13168283 

        在论坛博客上看到很多人实现3D的切换图片效果大多是用的Gallery,加入倒影,加入倾斜角,而且有很多都是实现的特别好的。但是自己测试时发现This class was deprecated in API level 16.

其实还有很多组件可以实现手势切换,或者可以完全自己编写来控制imageview的切换效果,但是当看到android api上推荐的viewpager后发现其的确是个好东西。对于实现图片的切换其实于Gallery类似,只需要给定一个adapter即可。

附上部分源码:(我是要写软件的欢迎动画,initwelcomeview()方法是启动欢迎动画,ininmianview()是启动软件的主界面)

private void initWelcomeView()
	{
		editor.putBoolean("isFirstUse", false);
		editor.commit();
		setContentView(R.layout.main_welcome);
		ViewPager viewPager = (ViewPager)findViewById(R.id.main_welcome_gallery);
		MyPagerAdapter adapter = new MyPagerAdapter(this,getResources());
		viewPager.setAdapter(adapter);
		Button start = (Button)findViewById(R.id.main_welcome_start);
		start.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ininMainView();
			}
			
		});
	}

adapter内容:

public class MyPagerAdapter extends PagerAdapter{
	
	Context context;
	Resources res;
	int[] id = {R.drawable.welcome1,R.drawable.welcome2,R.drawable.welcome3,R.drawable.welcome4};
	public MyPagerAdapter(Context cx,Resources r) {
		context = cx;
		res = r;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 4;
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		// TODO Auto-generated method stub
		return view == (View) obj;
	}

	@Override
	public Object instantiateItem (ViewGroup container, int position) {
		ImageView iv = new ImageView(context);
		try {
			Bitmap bm = BitmapFactory.decodeResource(res, id[position]);//载入bitmap
			iv.setImageBitmap(bm);
		} 
		catch (OutOfMemoryError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		((ViewPager)container).addView(iv, 0);
		return iv;
	}
	
	@Override
	public void destroyItem (ViewGroup container, int position, Object object) {
		container.removeView((View)object);
	}


}


但是仔细查看viewpager的api后可以发现,它的功能远要比代替gallery多的多,因为它提供了setOnPageChangeListener()方法

public void setOnPageChangeListener (ViewPager.OnPageChangeListener listener)

Set a listener that will be invoked whenever the page changes or is incrementally scrolled. See ViewPager.OnPageChangeListener.

Parameters
listener Listener to set

可以监听它的切换,如果在应用其他组件或者干脆闲的自己写出来的,就需要完全挤在adapter类中判断当前的组件是哪个来实现监听的效果,代码的条理会变得特别不 清晰。而viewpager提供好了listener,非常好的解决了这个问题。

打个比方,我现在要写个欢迎动画,当其翻到最后一页的时候让显示一个“开始使用”的button ,而在其他界面里该button处于invisible状态,如果没有监听就需要在adapter返回组件的时候来判断当前的position,那如果adapter的内容并不是activity类中的内部类,或者为了加快运行速度是写在其他thread中的,就需要再用handler或者广播来通信控制button的状态,代码量会提升很多,那如果要是想要管理那种软件底部带有tab的,就会变得更加条理不清晰。但是加入监听器后,就可以很好解决这个问题。

对于这个监听其实也没有什么内容只有三个方法,但是已经足够应付了:

public abstract void onPageScrollStateChanged (int state)

Called when the scroll state changes. Useful for discovering when the user begins dragging, when the pager is automatically settling to the current page, or when it is fully stopped/idle.

Parameters
state The new scroll state.
See Also
  • SCROLL_STATE_IDLE
  • SCROLL_STATE_DRAGGING
  • SCROLL_STATE_SETTLING

public abstract void onPageScrolled (int position, float positionOffset, int positionOffsetPixels)

This method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll.

Parameters
position Position index of the first page currently being displayed. Page position+1 will be visible if positionOffset is nonzero.
positionOffset Value from [0, 1) indicating the offset from the page at position.
positionOffsetPixels Value in pixels indicating the offset from position.

public abstract void onPageSelected (int position)

This method will be invoked when a new page becomes selected. Animation is not necessarily complete.

Parameters
position Position index of the new selected page.
转载文章清注明出处: http://blog.csdn.net/dangxw_/article/details/13168283

你可能感兴趣的:(android)