Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ViewPager導航------平凡中有奇跡

Android ViewPager導航------平凡中有奇跡

編輯:關於Android編程

ViewPager做導航想不想有這樣的效果:

比如說有四張圖片,下面有四個圓點,當頁面滑動的時候一個點變大一個點變小(或者是一個點變小一個點變大),等於說同時在執行兩個動畫。

\

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">package com.example.day_2014_10_17_viewpager; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.widget.ImageView; import com.nineoldandroids.view.ViewHelper; import com.panpass.adapter.MyViewPagerAdapter; import com.panpass.view.MyViewPager; import com.panpass.view.MyViewPager.DiyPageScrollListener; public class MainActivity extends Activity { private Context mContext = this; private MyViewPager mViewPager; private ImageView imageView1, imageView2, imageView3, imageView4; private ArrayList mList; private HashMap mHashMap = new LinkedHashMap(); private static final float SCALE_MAX = 0.55f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mList = new ArrayList(); ImageView img = new ImageView(mContext); img.setBackgroundResource(R.drawable.guide_page1); mList.add(img); img = new ImageView(mContext); img.setBackgroundResource(R.drawable.guide_page2); mList.add(img); img = new ImageView(mContext); img.setBackgroundResource(R.drawable.guide_page3); mList.add(img); img = new ImageView(mContext); img.setBackgroundResource(R.drawable.guide_page4); mList.add(img); mViewPager = (MyViewPager) findViewById(R.id.view_pager); mViewPager.setAdapter(new MyViewPagerAdapter(mList)); imageView1 = (ImageView) findViewById(R.id.imageView1); imageView2 = (ImageView) findViewById(R.id.imageView2); imageView3 = (ImageView) findViewById(R.id.imageView3); imageView4 = (ImageView) findViewById(R.id.imageView4); ViewHelper.setScaleX(imageView2, SCALE_MAX); ViewHelper.setScaleY(imageView2, SCALE_MAX); ViewHelper.setScaleX(imageView3, SCALE_MAX); ViewHelper.setScaleY(imageView3, SCALE_MAX); ViewHelper.setScaleX(imageView4, SCALE_MAX); ViewHelper.setScaleY(imageView4, SCALE_MAX); mHashMap.put(0, imageView1); mHashMap.put(1, imageView2); mHashMap.put(2, imageView3); mHashMap.put(3, imageView4); mViewPager.setDiyOnPageScrollListener(new DiyPageScrollListener() { @Override public void diyOnPageScrollListener(int position, float positionOffset, int positionOffsetPixels, boolean left, boolean right) { // 獲取左邊的View ImageView mLeftImg = mHashMap.get(position); // 獲取右邊的View ImageView mRightImg = mHashMap.get(position + 1); // 判斷滑動的方向 float mScaleRight; float mScaleLeft; /** * 注意事件: 1 注意你要很靈敏的判斷ViewPager的切換方向是很難的,對於要求很高的應用來說是做不到了。 2 * 不管是向左滑動還是向右滑動,他們有的都是同一種算法 */ if (mRightImg != null) { /** * 縮小比例 如果手指從右到左的滑動(切換到後一個):0.0~1.0,即從一半到最大 * 如果手指從左到右的滑動(切換到前一個):1.0~0,即從最大到一半 */ mScaleRight = (1-SCALE_MAX) * positionOffset + SCALE_MAX; /** * x偏移量: 如果手指從右到左的滑動(切換到後一個):0-720 如果手指從左到右的滑動(切換到前一個):720-0 */ ViewHelper.setScaleX(mRightImg, mScaleRight); ViewHelper.setScaleY(mRightImg, mScaleRight); } if (mLeftImg != null) { mScaleLeft = (1-SCALE_MAX) * (1 - positionOffset) + SCALE_MAX; /** * x偏移量: 如果手指從右到左的滑動(切換到後一個):0-720 如果手指從左到右的滑動(切換到前一個):720-0 */ ViewHelper.setScaleX(mLeftImg, mScaleLeft); ViewHelper.setScaleY(mLeftImg, mScaleLeft); } } }); } }


package com.panpass.adapter;

import java.util.ArrayList;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MyViewPagerAdapter extends PagerAdapter {


	private ArrayList imgList;
	
	public MyViewPagerAdapter(ArrayList imgList) {
		super();
		this.imgList = imgList;
	}

	@Override
	public int getCount() {
		return imgList.size();
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		
		ImageView img = imgList.get(position);
		container.addView(img);
		return img;
	}
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		
		container.removeView((ImageView)object);
		
	}
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == (View)arg1;
	}

}


package com.panpass.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

public class MyViewPager extends ViewPager {

	private boolean left = false;  
	private boolean right = false;  
	private boolean isScrolling = false; 
	
	private int lastValue = -1;  
	private ChangeViewCallback changeViewCallback = null;  

	public MyViewPager(Context context, AttributeSet attrs) {  
		super(context, attrs);  
		init();  
	}  

	public MyViewPager(Context context) {  
		super(context);  
		init();  
	}  

	private DiyPageScrollListener listener1;

	public interface DiyPageScrollListener{

		void diyOnPageScrollListener(int position, float positionOffset,  
				int positionOffsetPixels,boolean left,boolean right);
	}

	public void setDiyOnPageScrollListener(DiyPageScrollListener listener){

		this.listener1 = listener;
	}
	@Override
	protected void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		listener1.diyOnPageScrollListener(position, positionOffset, positionOffsetPixels,left,right);
		super.onPageScrolled(position, positionOffset, positionOffsetPixels);
	}

	/** 
	 * init method . 
	 */  
	private void init() {  
//		setOnPageChangeListener(listener);  
	}  

	/** 
	 * listener ,to get move direction . 
	 */  
	public  OnPageChangeListener listener = new OnPageChangeListener() {  
		@Override  
		public void onPageScrollStateChanged(int arg0) {  
			if (arg0 == 1) {  
				isScrolling = true;  
			} else {  
				isScrolling = false;  
			}  

			if (arg0 == 2) {  
			
				if(changeViewCallback!=null){  
					changeViewCallback.changeView(left, right);  
				}  
				right = left = false;  
			}  

		}  

		@Override  
		public void onPageScrolled(int arg0, float arg1, int arg2) {  
			if (isScrolling) {  
				if (lastValue > arg2) {  
					// 遞減,向右側滑動  
					right = true;  
					left = false;  
				} else if (lastValue < arg2) {  
					// 遞減,向右側滑動  
					right = false;  
					left = true;  
				} else if (lastValue == arg2) {  
					right = left = false;  
				}  
			}  
			lastValue = arg2;  
		}  

		@Override  
		public void onPageSelected(int arg0) {  
			if(changeViewCallback!=null){  
				changeViewCallback.getCurrentPageIndex(arg0);  
			}  
		}  
	};  

	/** 
	 * 得到是否向右側滑動 
	 * @return true 為右滑動 
	 */  
	public boolean getMoveRight(){  
		return right;  
	}  

	/** 
	 * 得到是否向左側滑動 
	 * @return true 為左做滑動 
	 */  
	public boolean getMoveLeft(){  
		return left;  
	}  

	/** 
	 *  滑動狀態改變回調 
	 * @author zxy 
	 * 
	 */  
	public interface ChangeViewCallback{  
		public  void changeView(boolean left,boolean right);  
		public  void  getCurrentPageIndex(int index);  
	}  

	public void  setChangeViewCallback(ChangeViewCallback callback){  
		changeViewCallback = callback;  
	}  


}



    

    

        

        

        

        
    





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