Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ViewPager 動畫效果

Android ViewPager 動畫效果

編輯:關於Android編程

找到個不錯的開源項目:https://github.com/jfeinstein10/JazzyViewPager

Android ViewPager 動畫效果

\ <喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20141126/2014112608433590.png" alt="\">

\

適配器:(這個是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 views;
	private ViewPageOnClick callback;
	private JazzyViewPager pager;

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

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

	public void changeItem(ArrayList 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 getViews() {
		// TODO Auto-generated method stub
		ArrayList list=new ArrayList();

		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中,如需其他動畫,自己添加即可


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved