首页 > 代码库 > Android ViewPager 动画效果

Android ViewPager 动画效果

找到个不错的开源项目:https://github.com/jfeinstein10/JazzyViewPager

Android ViewPager 动画效果

 

适配器:(这个是viewpager的每一项都添加了onclick事件的)

package com.example.jaazy;

import java.util.ArrayList;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
/**
 * Create by LanYan on 2014.04.24
 * This is a adapter
 */
public class ViewPagerAdapterOnClick extends PagerAdapter {
	private ArrayList<View> views;
	private ViewPageOnClick callback;
	private JazzyViewPager pager;

	public ViewPagerAdapterOnClick(JazzyViewPager pager,ArrayList<View> views,ViewPageOnClick callback) {
		this.setViews(views);
		this.callback=callback;
		this.pager=pager;
	}

	public void setViews(ArrayList<View> views) {
		if (views != null)
			this.views = views;
		else
			this.views = new ArrayList<View>();
	}

	public void changeItem(ArrayList<View> views) {
		this.setViews(views);
		notifyDataSetChanged();
	}


	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return POSITION_NONE;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return views.size();
	}

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

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		View v = views.get(position);
		container.removeView(v);
	}
	public void removeUpdate(ViewGroup container){
		container.removeAllViews();
	}
	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		View v = views.get(position);
		v.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				callback.callbackOnClickItem(position);
			}
		});
		container.addView(v);
		pager.setObjectForPosition(v, position);
		return v;
	}
	public interface ViewPageOnClick{
		void callbackOnClickItem(int position);
	}
}
activity:

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.jaazy.JazzyViewPager.TransitionEffect;
import com.example.jaazy.ViewPagerAdapterOnClick.ViewPageOnClick;

public class MainActivity extends Activity implements ViewPageOnClick{

	private JazzyViewPager pager;
	private ViewPagerAdapterOnClick adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setupJazziness(TransitionEffect.Stack);

	}

	private void setupJazziness(TransitionEffect stack) {
		// TODO Auto-generated method stub
		pager=(JazzyViewPager)findViewById(R.id.jazzy_pager);
		adapter=new ViewPagerAdapterOnClick(pager, getViews(),this);
		pager.setAdapter(adapter);
		
		pager.setPageMargin(5);
		//压栈方式
		pager.setTransitionEffect(TransitionEffect.Stack);
		//立体内部进出
		//pager.setTransitionEffect(TransitionEffect.CubeIn);
		//立体凸出
		//pager.setTransitionEffect(TransitionEffect.CubeOut);
		//左右挤压
		//pager.setTransitionEffect(TransitionEffect.Accordion);
		//水平翻页
		//pager.setTransitionEffect(TransitionEffect.FlipHorizontal);
		//垂直翻页
		//pager.setTransitionEffect(TransitionEffect.FlipVertical);
		//上扇形摆入
		//pager.setTransitionEffect(TransitionEffect.RotateDown);
		//下扇形摆入
		//pager.setTransitionEffect(TransitionEffect.RotateUp);
		//标准
		//pager.setTransitionEffect(TransitionEffect.Standard);
		//选项卡(卡片形式)
		///pager.setTransitionEffect(TransitionEffect.Tablet);
		
		//zoom in
		//pager.setTransitionEffect(TransitionEffect.ZoomIn);
		
		//zoom out
		//pager.setTransitionEffect(TransitionEffect.ZoomOut);
		
		
	}

	private ArrayList<View> getViews() {
		// TODO Auto-generated method stub
		ArrayList<View> list=new ArrayList<View>();

		ImageView i=new ImageView(this);
		LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		i.setLayoutParams(params);
		i.setBackgroundResource(R.drawable.a);


		ImageView i2=new ImageView(this);
		i2.setLayoutParams(params);
		i2.setBackgroundResource(R.drawable.b);

		ImageView i3=new ImageView(this);
		i3.setLayoutParams(params);
		i3.setBackgroundResource(R.drawable.c);

		ImageView i4=new ImageView(this);
		i4.setLayoutParams(params);
		i4.setBackgroundResource(R.drawable.e);

		list.add(i);
		list.add(i2);
		list.add(i3);
		list.add(i4);
		return list;
	}

	@Override
	public void callbackOnClickItem(int position) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "position:"+position, 1000).show();
	}


}
JazzyViewPager自带动画枚举11中,如需其他动画,自己添加即可


Android ViewPager 动画效果