Android 自定义 ViewPager 打造千变万化的图片切换效(2)

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity
{
 protected static final String TAG = "MainActivity";
 private int[] mImgIds;
 private MyJazzyViewPager mViewPager;

@Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mImgIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
    R.drawable.d };
  mViewPager = (MyJazzyViewPager) findViewById(R.id.id_viewPager);
  mViewPager.setAdapter(new PagerAdapter()
  {

@Override
   public boolean isViewFromObject(View arg0, Object arg1)
   {
    return arg0 == arg1;
   }

@Override
   public void destroyItem(ViewGroup container, int position,
     Object object)
   {
    container.removeView((View) object);
   }

@Override
   public Object instantiateItem(ViewGroup container, int position)
   {
    ImageView imageView = new ImageView(MainActivity.this);
    imageView.setImageResource(mImgIds[position]);
    imageView.setScaleType(ScaleType.CENTER_CROP);
    container.addView(imageView);
    mViewPager.setObjectForPosition(imageView, position);
    return imageView;
   }

@Override
   public int getCount()
   {
    return mImgIds.length;
   }
  });

}

}

这个很常见的代码,就是初始化ViewPager~~就没啥可说的了~~有一点需要注意:在instantiateItem方法,我们多调用了一个mViewPager.setObjectForPosition(imageView, position);其实就是为了给我们的Map存值

主要看自定义的ViewPager

package com.example.zhy_jazzyviewpager;

import java.util.HashMap;
import java.util.LinkedHashMap;

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

import com.nineoldandroids.view.ViewHelper;

public class MyJazzyViewPager extends ViewPager
{
 private float mTrans;
 private float mScale;
 /**
  * 最大的缩小比例
  */
 private static final float SCALE_MAX = 0.5f;
 private static final String TAG = "MyJazzyViewPager";
 /**
  * 保存position与对于的View
  */
 private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();
 /**
  * 滑动时左边的元素
  */
 private View mLeft;
 /**
  * 滑动时右边的元素
  */
 private View mRight;

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

@Override
 public void onPageScrolled(int position, float positionOffset,
   int positionOffsetPixels)
 {

//  Log.e(TAG, "position=" + position+", positionOffset = "+positionOffset+" ,positionOffsetPixels =  " + positionOffsetPixels+" , currentPos = " + getCurrentItem());
 
  //滑动特别小的距离时,我们认为没有动,可有可无的判断
  float effectOffset = isSmall(positionOffset) ? 0 : positionOffset;
 
  //获取左边的View
  mLeft = findViewFromObject(position);
  //获取右边的View
  mRight = findViewFromObject(position + 1);
 
  // 添加切换动画效果
  animateStack(mLeft, mRight, effectOffset, positionOffsetPixels);
  super.onPageScrolled(position, positionOffset, positionOffsetPixels);
 }

public void setObjectForPosition(View view, int position)
 {
  mChildrenViews.put(position, view);
 }

/**
  * 通过过位置获得对应的View
  *
  * @param position
  * @return
  */
 public View findViewFromObject(int position)
 {
  return mChildrenViews.get(position);
 }

private boolean isSmall(float positionOffset)
 {
  return Math.abs(positionOffset) < 0.0001;
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5101da8cfc0bb38eaf1dc72e3571e725.html