ViewPager禁止预加载以及ViewPagerAdapter的处理

禁用方案来源:http://blog.csdn.net/qq_21898059/article/details/51453938

ViewPager会默认预加载左右两个未展示的页面缓存下来,有利于更流畅的滑动效果。那么如果需求方要求禁止预加载,该怎么实现呢?很简单使用低版本v4包里的ViewPager,完全copy一份,将其中的DEFAULT_OFFSCREEN_PAGES(默认为1)值改为0即可。 API 14 即 Android 4.0的v4包里ViewPager 有效。

修改后的源码如下,拷贝该类即可使用。

package com.wtdc.codebook.camera.utils;

import android.content.Context;  
import android.database.DataSetObserver;  
import android.graphics.Canvas;  
import android.graphics.Rect;  
import android.graphics.drawable.Drawable;  
import android.os.Parcel;  
import android.os.Parcelable;  
import android.os.SystemClock;  
import android.support.v4.os.ParcelableCompat;  
import android.support.v4.os.ParcelableCompatCreatorCallbacks;  
import android.support.v4.view.KeyEventCompat;  
import android.support.v4.view.MotionEventCompat;  
import android.support.v4.view.PagerAdapter;  
import android.support.v4.view.VelocityTrackerCompat;  
import android.support.v4.view.ViewCompat;  
import android.support.v4.view.ViewConfigurationCompat;  
import android.support.v4.widget.EdgeEffectCompat;  
import android.util.AttributeSet;  
import android.util.Log;  
import android.view.FocusFinder;  
import android.view.KeyEvent;  
import android.view.MotionEvent;  
import android.view.SoundEffectConstants;  
import android.view.VelocityTracker;  
import android.view.View;  
import android.view.ViewConfiguration;  
import android.view.ViewGroup;  
import android.view.ViewParent;  
import android.view.accessibility.AccessibilityEvent;  
import android.view.animation.Interpolator;  
import android.widget.Scroller;  
  
import java.util.ArrayList;  
import java.util.Collections;  
import java.util.Comparator;  
  
public class NoPreloadViewPager extends ViewGroup {  
	private static final String TAG = "NoPreLoadViewPager";  
    private static final boolean DEBUG = false;  
  
    private static final boolean USE_CACHE = false;  
  
    private static final int DEFAULT_OFFSCREEN_PAGES = 0;//禁用ViewPager预加载  默认是1  
    private static final int MAX_SETTLE_DURATION = 600; // 毫秒  
  
    static class ItemInfo {  
        Object object;  
        int position;  
        boolean scrolling;  
    }  
  
    private static final Comparator<ItemInfo> COMPARATOR = new Comparator<ItemInfo>(){  
        @Override  
        public int compare(ItemInfo lhs, ItemInfo rhs) {  
            return lhs.position - rhs.position;  
        }};  
  
    private static final Interpolator sInterpolator = new Interpolator() {  
        public float getInterpolation(float t) {  
            // _o(t) = t * t * ((tension + 1) * t + tension)  
            // o(t) = _o(t - 1) + 1  
            t -= 1.0f;  
            return t * t * t + 1.0f;  
        }  
    };  
  
    private final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();  
  
    private PagerAdapter mAdapter;  
    private int mCurItem;   // Index of currently displayed page.  
    private int mRestoredCurItem = -1;  
    private Parcelable mRestoredAdapterState = null;  
    private ClassLoader mRestoredClassLoader = null;  
    private Scroller mScroller;  
    private PagerObserver mObserver;  
  
    private int mPageMargin;  
    private Drawable mMarginDrawable;  
  
    private int mChildWidthMeasureSpec;  
    private int mChildHeightMeasureSpec;  
    private boolean mInLayout;  
  
    private boolean mScrollingCacheEnabled;  
  
    private boolean mPopulatePending;  
    private boolean mScrolling;  
    private int mOffscreenPageLimit = DEFAULT_OFFSCREEN_PAGES;  
  
    private boolean mIsBeingDragged;  
    private boolean mIsUnableToDrag;  
    private int mTouchSlop;  
    private float mInitialMotionX;  
    /** 
     * Position of the last motion event. 
     */  
    private float mLastMotionX;  
    private float mLastMotionY;  
    /** 
     * ID of the active pointer. This is used to retain consistency during 
     * drags/flings if multiple pointers are used. 
     */  
    private int mActivePointerId = INVALID_POINTER;  
    /** 
     * Sentinel value for no current active pointer. 
     * Used by {@link #mActivePointerId}. 
     */  
    private static final int INVALID_POINTER = -1;  
  
    /** 
     * Determines speed during touch scrolling 
     */  
    private VelocityTracker mVelocityTracker;  
    private int mMinimumVelocity;  
    private int mMaximumVelocity;  
    private float mBaseLineFlingVelocity;  
    private float mFlingVelocityInfluence;  
  
    private boolean mFakeDragging;  
    private long mFakeDragBeginTime;  
  
    private EdgeEffectCompat mLeftEdge;  
    private EdgeEffectCompat mRightEdge;  
  
    private boolean mFirstLayout = true;  
  
    private OnPageChangeListener mOnPageChangeListener;  
  
    /** 
     * Indicates that the pager is in an idle, settled state. The current page 
     * is fully in view and no animation is in progress. 
     */  
    public static final int SCROLL_STATE_IDLE = 0;  
  
    /** 
     * Indicates that the pager is currently being dragged by the user. 
     */  
    public static final int SCROLL_STATE_DRAGGING = 1;  
  
    /** 
     * Indicates that the pager is in the process of settling to a final position. 
     */  
    public static final int SCROLL_STATE_SETTLING = 2;  
  
    private int mScrollState = SCROLL_STATE_IDLE;  
  
    /** 
     * Callback interface for responding to changing state of the selected page. 
     */  
    public interface OnPageChangeListener {  
  
        /** 
         * 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. 
         * 
         * @param position Position index of the first page currently being displayed. 
         *                 Page position+1 will be visible if positionOffset is nonzero. 
         * @param positionOffset Value from [0, 1) indicating the offset from the page at position. 
         * @param positionOffsetPixels Value in pixels indicating the offset from position. 
         */  
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);  
  
        /** 
         * This method will be invoked when a new page becomes selected. Animation is not 
         * necessarily complete. 
         * 
         * @param position Position index of the new selected page. 
         */  
        public void onPageSelected(int position);  
  
        /** 
         * 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. 
         * 
         * @param state The new scroll state. 
         * @see android.support.v4.view.ViewPager#SCROLL_STATE_IDLE 
         * @see android.support.v4.view.ViewPager#SCROLL_STATE_DRAGGING 
         * @see android.support.v4.view.ViewPager#SCROLL_STATE_SETTLING 
         */  
        public void onPageScrollStateChanged(int state);  
    }  
  
    /** 
     * Simple implementation of the {@link android.support.v4.view.LazyViewPager.OnPageChangeListener} interface with stub 
     * implementations of each method. Extend this if you do not intend to override 
     * every method of {@link android.support.v4.view.LazyViewPager.OnPageChangeListener}. 
     */  
    public static class SimpleOnPageChangeListener implements OnPageChangeListener {  
        @Override  
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
            // This space for rent  
        }  
  
        @Override  
        public void onPageSelected(int position) {  
            // This space for rent  
        }  
  
        @Override  
        public void onPageScrollStateChanged(int state) {  
            // This space for rent  
        }  
    }  
  
    public NoPreloadViewPager(Context context) {  
        super(context);  
        initViewPager();  
    }  
  
    public NoPreloadViewPager(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        initViewPager();  
    }  
  
    void initViewPager() {  
        setWillNotDraw(false);  
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);  
        setFocusable(true);  
        final Context context = getContext();  
        mScroller = new Scroller(context, sInterpolator);  
        final ViewConfiguration configuration = ViewConfiguration.get(context);  
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);  
        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();  
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();  
        mLeftEdge = new EdgeEffectCompat(context);  
        mRightEdge = new EdgeEffectCompat(context);  
  
        float density = context.getResources().getDisplayMetrics().density;  
        mBaseLineFlingVelocity = 2500.0f * density;  
        mFlingVelocityInfluence = 0.4f;  
    }  
  
    private void setScrollState(int newState) {  
        if (mScrollState == newState) {  
            return;  
        }  
  
        mScrollState = newState;  
        if (mOnPageChangeListener != null) {  
            mOnPageChangeListener.onPageScrollStateChanged(newState);  
        }  
    }  
  
    public void setAdapter(PagerAdapter adapter) {  
        if (mAdapter != null) {  
//            mAdapter.unregisterDataSetObserver(mObserver);  
            mAdapter.startUpdate(this);  
            for (int i = 0; i < mItems.size(); i++) {  
                final ItemInfo ii = mItems.get(i);  
                mAdapter.destroyItem(this, ii.position, ii.object);  
            }  
            mAdapter.finishUpdate(this);  
            mItems.clear();  
            removeAllViews();  
            mCurItem = 0;  
            scrollTo(0, 0);  
        }  
  
        mAdapter = adapter;  
  
        if (mAdapter != null) {  
            if (mObserver == null) {  
                mObserver = new PagerObserver();  
            }  
//            mAdapter.registerDataSetObserver(mObserver);  
            mPopulatePending = false;  
            if (mRestoredCurItem >= 0) {  
                mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader);  
                setCurrentItemInternal(mRestoredCurItem, false, true);  
                mRestoredCurItem = -1;  
                mRestoredAdapterState = null;  
                mRestoredClassLoader = null;  
            } else {  
                populate();  
            }  
        }  
    }  
  
    public PagerAdapter getAdapter() {  
        return mAdapter;  
    }  
  
    /** 
     * Set the currently selected page. If the ViewPager has already been through its first 
     * layout there will be a smooth animated transition between the current item and the 
     * specified item. 
     * 
     * @param item Item index to select 
     */  
    public void setCurrentItem(int item) {  
        mPopulatePending = false;  
        setCurrentItemInternal(item, !mFirstLayout, false);  
    }  
  
    /** 
     * Set the currently selected page. 
     * 
     * @param item Item index to select 
     * @param smoothScroll True to smoothly scroll to the new item, false to transition immediately 
     */  
    public void setCurrentItem(int item, boolean smoothScroll) {  
        mPopulatePending = false;  
        setCurrentItemInternal(item, smoothScroll, false);  
    }  
  
    public int getCurrentItem() {  
        return mCurItem;  
    }  
  
    void setCurrentItemInternal(int item, boolean smoothScroll, boolean always) {  
        setCurrentItemInternal(item, smoothScroll, always, 0);  
    }  
  
    void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {  
        if (mAdapter == null || mAdapter.getCount() <= 0) {  
            setScrollingCacheEnabled(false);  
            return;  
        }  
        if (!always && mCurItem == item && mItems.size() != 0) {  
            setScrollingCacheEnabled(false);  
            return;  
        }  
        if (item < 0) {  
            item = 0;  
        } else if (item >= mAdapter.getCount()) {  
            item = mAdapter.getCount() - 1;  
        }  
        final int pageLimit = mOffscreenPageLimit;  
        if (item > (mCurItem + pageLimit) || item < (mCurItem - pageLimit)) {  
            // We are doing a jump by more than one page.  To avoid  
            // glitches, we want to keep all current pages in the view  
            // until the scroll ends.  
            for (int i=0; i<mItems.size(); i++) {  
                mItems.get(i).scrolling = true;  
            }  
        }  
  
        final boolean dispatchSelected = mCurItem != item;  
        mCurItem = item;  
        populate();  
        final int destX = (getWidth() + mPageMargin) * item;  
        if (smoothScroll) {  
            smoothScrollTo(destX, 0, velocity);  
            if (dispatchSelected && mOnPageChangeListener != null) {  
                mOnPageChangeListener.onPageSelected(item);  
            }  
        } else {  
            if (dispatchSelected && mOnPageChangeListener != null) {  
                mOnPageChangeListener.onPageSelected(item);  
            }  
            completeScroll();  
            scrollTo(destX, 0);  
        }  
    }  
  
    public void setOnPageChangeListener(OnPageChangeListener listener) {  
        mOnPageChangeListener = listener;  
    }  
  
    /** 
     * Returns the number of pages that will be retained to either side of the 
     * current page in the view hierarchy in an idle state. Defaults to 1. 
     * 
     * @return How many pages will be kept offscreen on either side 
     * @see #setOffscreenPageLimit(int) 
     */  
    public int getOffscreenPageLimit() {  
        return mOffscreenPageLimit;  
    }  
  
    /** 
     * Set the number of pages that should be retained to either side of the 
     * current page in the view hierarchy in an idle state. Pages beyond this 
     * limit will be recreated from the adapter when needed. 
     * 
     * <p>This is offered as an optimization. If you know in advance the number 
     * of pages you will need to support or have lazy-loading mechanisms in place 
     * on your pages, tweaking this setting can have benefits in perceived smoothness 
     * of paging animations and interaction. If you have a small number of pages (3-4) 
     * that you can keep active all at once, less time will be spent in layout for 
     * newly created view subtrees as the user pages back and forth.</p> 
     * 
     * <p>You should keep this limit low, especially if your pages have complex layouts. 
     * This setting defaults to 1.</p> 
     * 
     * @param limit How many pages will be kept offscreen in an idle state. 
     */  
    public void setOffscreenPageLimit(int limit) {  
        if (limit < DEFAULT_OFFSCREEN_PAGES) {  
            Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +  
                    DEFAULT_OFFSCREEN_PAGES);  
            limit = DEFAULT_OFFSCREEN_PAGES;  
        }  
        if (limit != mOffscreenPageLimit) {  
            mOffscreenPageLimit = limit;  
            populate();  
        }  
    }  
  
    /** 
     * Set the margin between pages. 
     * 
     * @param marginPixels Distance between adjacent pages in pixels 
     * @see #getPageMargin() 
     * @see #setPageMarginDrawable(android.graphics.drawable.Drawable) 
     * @see #setPageMarginDrawable(int) 
     */  
    public void setPageMargin(int marginPixels) {  
        final int oldMargin = mPageMargin;  
        mPageMargin = marginPixels;  
  
        final int width = getWidth();  
        recomputeScrollPosition(width, width, marginPixels, oldMargin);  
  
        requestLayout();  
    }  
  
    /** 
     * Return the margin between pages. 
     * 
     * @return The size of the margin in pixels 
     */  
    public int getPageMargin() {  
        return mPageMargin;  
    }  
  
    /** 
     * Set a drawable that will be used to fill the margin between pages. 
     * 
     * @param d Drawable to display between pages 
     */  
    public void setPageMarginDrawable(Drawable d) {  
        mMarginDrawable = d;  
        if (d != null) refreshDrawableState();  
        setWillNotDraw(d == null);  
        invalidate();  
    }  
  
    /** 
     * Set a drawable that will be used to fill the margin between pages. 
     * 
     * @param resId Resource ID of a drawable to display between pages 
     */  
    public void setPageMarginDrawable(int resId) {  
        setPageMarginDrawable(getContext().getResources().getDrawable(resId));  
    }  
  
    @Override  
    protected boolean verifyDrawable(Drawable who) {  
        return super.verifyDrawable(who) || who == mMarginDrawable;  
    }  
  
    @Override  
    protected void drawableStateChanged() {  
        super.drawableStateChanged();  
        final Drawable d = mMarginDrawable;  
        if (d != null && d.isStateful()) {  
            d.setState(getDrawableState());  
        }  
    }  
  
    // We want the duration of the page snap animation to be influenced by the distance that  
    // the screen has to travel, however, we don't want this duration to be effected in a  
    // purely linear fashion. Instead, we use this method to moderate the effect that the distance  
    // of travel has on the overall snap duration.  
    float distanceInfluenceForSnapDuration(float f) {  
        f -= 0.5f; // center the values about 0.  
        f *= 0.3f * Math.PI / 2.0f;  
        return (float) Math.sin(f);  
    }  
  
    /** 
     * Like {@link android.view.View#scrollBy}, but scroll smoothly instead of immediately. 
     * 
     * @param x the number of pixels to scroll by on the X axis 
     * @param y the number of pixels to scroll by on the Y axis 
     */  
    void smoothScrollTo(int x, int y) {  
        smoothScrollTo(x, y, 0);  
    }  
  
    /** 
     * Like {@link android.view.View#scrollBy}, but scroll smoothly instead of immediately. 
     * 
     * @param x the number of pixels to scroll by on the X axis 
     * @param y the number of pixels to scroll by on the Y axis 
     * @param velocity the velocity associated with a fling, if applicable. (0 otherwise) 
     */  
    void smoothScrollTo(int x, int y, int velocity) {  
        if (getChildCount() == 0) {  
            // Nothing to do.  
            setScrollingCacheEnabled(false);  
            return;  
        }  
        int sx = getScrollX();  
        int sy = getScrollY();  
        int dx = x - sx;  
        int dy = y - sy;  
        if (dx == 0 && dy == 0) {  
            completeScroll();  
            setScrollState(SCROLL_STATE_IDLE);  
            return;  
        }  
  
        setScrollingCacheEnabled(true);  
        mScrolling = true;  
        setScrollState(SCROLL_STATE_SETTLING);  
  
        final float pageDelta = (float) Math.abs(dx) / (getWidth() + mPageMargin);  
        int duration = (int) (pageDelta * 100);  
  
        velocity = Math.abs(velocity);  
        if (velocity > 0) {  
            duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence;  
        } else {  
            duration += 100;  
        }  
        duration = Math.min(duration, MAX_SETTLE_DURATION);  
  
        mScroller.startScroll(sx, sy, dx, dy, duration);  
        invalidate();  
    }  
  
    void addNewItem(int position, int index) {  
        ItemInfo ii = new ItemInfo();  
        ii.position = position;  
        ii.object = mAdapter.instantiateItem(this, position);  
        if (index < 0) {  
            mItems.add(ii);  
        } else {  
            mItems.add(index, ii);  
        }  
    }  
  
    void dataSetChanged() {  
        // This method only gets called if our observer is attached, so mAdapter is non-null.  
  
        boolean needPopulate = mItems.size() < 3 && mItems.size() < mAdapter.getCount();  
        int newCurrItem = -1;  
  
        for (int i = 0; i < mItems.size(); i++) {  
            final ItemInfo ii = mItems.get(i);  
            final int newPos = mAdapter.getItemPosition(ii.object);  
  
            if (newPos == PagerAdapter.POSITION_UNCHANGED) {  
                continue;  
            }  
  
            if (newPos == PagerAdapter.POSITION_NONE) {  
                mItems.remove(i);  
                i--;  
                mAdapter.destroyItem(this, ii.position, ii.object);  
                needPopulate = true;  
  
                if (mCurItem == ii.position) {  
                    // Keep the current item in the valid range  
                    newCurrItem = Math.max(0, Math.min(mCurItem, mAdapter.getCount() - 1));  
                }  
                continue;  
            }  
  
            if (ii.position != newPos) {  
                if (ii.position == mCurItem) {  
                    // Our current item changed position. Follow it.  
                    newCurrItem = newPos;  
                }  
  
                ii.position = newPos;  
                needPopulate = true;  
            }  
        }  
  
        Collections.sort(mItems, COMPARATOR);  
  
        if (newCurrItem >= 0) {  
            // TODO This currently causes a jump.  
            setCurrentItemInternal(newCurrItem, false, true);  
            needPopulate = true;  
        }  
        if (needPopulate) {  
            populate();  
            requestLayout();  
        }  
    }  
  
    void populate() {  
        if (mAdapter == null) {  
            return;  
        }  
  
        // Bail now if we are waiting to populate.  This is to hold off  
        // on creating views from the time the user releases their finger to  
        // fling to a new position until we have finished the scroll to  
        // that position, avoiding glitches from happening at that point.  
        if (mPopulatePending) {  
            if (DEBUG) Log.i(TAG, "populate is pending, skipping for now...");  
            return;  
        }  
  
        // Also, don't populate until we are attached to a window.  This is to  
        // avoid trying to populate before we have restored our view hierarchy  
        // state and conflicting with what is restored.  
        if (getWindowToken() == null) {  
            return;  
        }  
  
        mAdapter.startUpdate(this);  
  
        final int pageLimit = mOffscreenPageLimit;  
        final int startPos = Math.max(0, mCurItem - pageLimit);  
        final int N = mAdapter.getCount();  
        final int endPos = Math.min(N-1, mCurItem + pageLimit);  
  
        if (DEBUG) Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);  
  
        // Add and remove pages in the existing list.  
        int lastPos = -1;  
        for (int i=0; i<mItems.size(); i++) {  
            ItemInfo ii = mItems.get(i);  
            if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {  
                if (DEBUG) Log.i(TAG, "removing: " + ii.position + " @ " + i);  
                mItems.remove(i);  
                i--;  
                mAdapter.destroyItem(this, ii.position, ii.object);  
            } else if (lastPos < endPos && ii.position > startPos) {  
                // The next item is outside of our range, but we have a gap  
                // between it and the last item where we want to have a page  
                // shown.  Fill in the gap.  
                lastPos++;  
                if (lastPos < startPos) {  
                    lastPos = startPos;  
                }  
                while (lastPos <= endPos && lastPos < ii.position) {  
                    if (DEBUG) Log.i(TAG, "inserting: " + lastPos + " @ " + i);  
                    addNewItem(lastPos, i);  
                    lastPos++;  
                    i++;  
                }  
            }  
            lastPos = ii.position;  
        }  
  
        // Add any new pages we need at the end.  
        lastPos = mItems.size() > 0 ? mItems.get(mItems.size()-1).position : -1;  
        if (lastPos < endPos) {  
            lastPos++;  
            lastPos = lastPos > startPos ? lastPos : startPos;  
            while (lastPos <= endPos) {  
                if (DEBUG) Log.i(TAG, "appending: " + lastPos);  
                addNewItem(lastPos, -1);  
                lastPos++;  
            }  
        }  
  
        if (DEBUG) {  
            Log.i(TAG, "Current page list:");  
            for (int i=0; i<mItems.size(); i++) {  
                Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);  
            }  
        }  
  
        ItemInfo curItem = null;  
        for (int i=0; i<mItems.size(); i++) {  
            if (mItems.get(i).position == mCurItem) {  
                curItem = mItems.get(i);  
                break;  
            }  
        }  
        mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);  
  
        mAdapter.finishUpdate(this);  
  
        if (hasFocus()) {  
            View currentFocused = findFocus();  
            ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;  
            if (ii == null || ii.position != mCurItem) {  
                for (int i=0; i<getChildCount(); i++) {  
                    View child = getChildAt(i);  
                    ii = infoForChild(child);  
                    if (ii != null && ii.position == mCurItem) {  
                        if (child.requestFocus(FOCUS_FORWARD)) {  
                            break;  
                        }  
                    }  
                }  
            }  
        }  
    }  
  
    public static class SavedState extends BaseSavedState {  
        int position;  
        Parcelable adapterState;  
        ClassLoader loader;  
  
        public SavedState(Parcelable superState) {  
            super(superState);  
        }  
  
        @Override  
        public void writeToParcel(Parcel out, int flags) {  
            super.writeToParcel(out, flags);  
            out.writeInt(position);  
            out.writeParcelable(adapterState, flags);  
        }  
  
        @Override  
        public String toString() {  
            return "FragmentPager.SavedState{"  
                    + Integer.toHexString(System.identityHashCode(this))  
                    + " position=" + position + "}";  
        }  
  
        public static final Creator<SavedState> CREATOR  
                = ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {  
                    @Override  
                    public SavedState createFromParcel(Parcel in, ClassLoader loader) {  
                        return new SavedState(in, loader);  
                    }  
                    @Override  
                    public SavedState[] newArray(int size) {  
                        return new SavedState[size];  
                    }  
                });  
  
        SavedState(Parcel in, ClassLoader loader) {  
            super(in);  
            if (loader == null) {  
                loader = getClass().getClassLoader();  
            }  
            position = in.readInt();  
            adapterState = in.readParcelable(loader);  
            this.loader = loader;  
        }  
    }  
  
    @Override  
    public Parcelable onSaveInstanceState() {  
        Parcelable superState = super.onSaveInstanceState();  
        SavedState ss = new SavedState(superState);  
        ss.position = mCurItem;  
        if (mAdapter != null) {  
            ss.adapterState = mAdapter.saveState();  
        }  
        return ss;  
    }  
  
    @Override  
    public void onRestoreInstanceState(Parcelable state) {  
        if (!(state instanceof SavedState)) {  
            super.onRestoreInstanceState(state);  
            return;  
        }  
  
        SavedState ss = (SavedState)state;  
        super.onRestoreInstanceState(ss.getSuperState());  
  
        if (mAdapter != null) {  
            mAdapter.restoreState(ss.adapterState, ss.loader);  
            setCurrentItemInternal(ss.position, false, true);  
        } else {  
            mRestoredCurItem = ss.position;  
            mRestoredAdapterState = ss.adapterState;  
            mRestoredClassLoader = ss.loader;  
        }  
    }  
  
    @Override  
    public void addView(View child, int index, LayoutParams params) {  
        if (mInLayout) {  
            addViewInLayout(child, index, params);  
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);  
        } else {  
            super.addView(child, index, params);  
        }  
  
        if (USE_CACHE) {  
            if (child.getVisibility() != GONE) {  
                child.setDrawingCacheEnabled(mScrollingCacheEnabled);  
            } else {  
                child.setDrawingCacheEnabled(false);  
            }  
        }  
    }  
  
    ItemInfo infoForChild(View child) {  
        for (int i=0; i<mItems.size(); i++) {  
            ItemInfo ii = mItems.get(i);  
            if (mAdapter.isViewFromObject(child, ii.object)) {  
                return ii;  
            }  
        }  
        return null;  
    }  
  
    ItemInfo infoForAnyChild(View child) {  
        ViewParent parent;  
        while ((parent=child.getParent()) != this) {  
            if (parent == null || !(parent instanceof View)) {  
                return null;  
            }  
            child = (View)parent;  
        }  
        return infoForChild(child);  
    }  
  
    @Override  
    protected void onAttachedToWindow() {  
        super.onAttachedToWindow();  
        mFirstLayout = true;  
    }  
  
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        // For simple implementation, or internal size is always 0.  
        // We depend on the container to specify the layout size of  
        // our view.  We can't really know what it is since we will be  
        // adding and removing different arbitrary views and do not  
        // want the layout to change as this happens.  
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),  
                getDefaultSize(0, heightMeasureSpec));  
  
        // Children are just made to fill our space.  
        mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth() -  
                getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);  
        mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight() -  
                getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY);  
  
        // Make sure we have created all fragments that we need to have shown.  
        mInLayout = true;  
        populate();  
        mInLayout = false;  
  
        // Make sure all children have been properly measured.  
        final int size = getChildCount();  
        for (int i = 0; i < size; ++i) {  
            final View child = getChildAt(i);  
            if (child.getVisibility() != GONE) {  
                if (DEBUG) Log.v(TAG, "Measuring #" + i + " " + child  
                + ": " + mChildWidthMeasureSpec);  
                child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);  
            }  
        }  
    }  
  
    @Override  
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
        super.onSizeChanged(w, h, oldw, oldh);  
  
        // Make sure scroll position is set correctly.  
        if (w != oldw) {  
            recomputeScrollPosition(w, oldw, mPageMargin, mPageMargin);  
        }  
    }  
  
    private void recomputeScrollPosition(int width, int oldWidth, int margin, int oldMargin) {  
        final int widthWithMargin = width + margin;  
        if (oldWidth > 0) {  
            final int oldScrollPos = getScrollX();  
            final int oldwwm = oldWidth + oldMargin;  
            final int oldScrollItem = oldScrollPos / oldwwm;  
            final float scrollOffset = (float) (oldScrollPos % oldwwm) / oldwwm;  
            final int scrollPos = (int) ((oldScrollItem + scrollOffset) * widthWithMargin);  
            scrollTo(scrollPos, getScrollY());  
            if (!mScroller.isFinished()) {  
                // We now return to your regularly scheduled scroll, already in progress.  
                final int newDuration = mScroller.getDuration() - mScroller.timePassed();  
                mScroller.startScroll(scrollPos, 0, mCurItem * widthWithMargin, 0, newDuration);  
            }  
        } else {  
            int scrollPos = mCurItem * widthWithMargin;  
            if (scrollPos != getScrollX()) {  
                completeScroll();  
                scrollTo(scrollPos, getScrollY());  
            }  
        }  
    }  
  
    @Override  
    protected void onLayout(boolean changed, int l, int t, int r, int b) {  
        mInLayout = true;  
        populate();  
        mInLayout = false;  
  
        final int count = getChildCount();  
        final int width = r-l;  
  
        for (int i = 0; i < count; i++) {  
            View child = getChildAt(i);  
            ItemInfo ii;  
            if (child.getVisibility() != GONE && (ii=infoForChild(child)) != null) {  
                int loff = (width + mPageMargin) * ii.position;  
                int childLeft = getPaddingLeft() + loff;  
                int childTop = getPaddingTop();  
                if (DEBUG) Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object  
                + ":" + childLeft + "," + childTop + " " + child.getMeasuredWidth()  
                + "x" + child.getMeasuredHeight());  
                child.layout(childLeft, childTop,  
                        childLeft + child.getMeasuredWidth(),  
                        childTop + child.getMeasuredHeight());  
            }  
        }  
        mFirstLayout = false;  
    }  
  
    @Override  
    public void computeScroll() {  
        if (DEBUG) Log.i(TAG, "computeScroll: finished=" + mScroller.isFinished());  
        if (!mScroller.isFinished()) {  
            if (mScroller.computeScrollOffset()) {  
                if (DEBUG) Log.i(TAG, "computeScroll: still scrolling");  
                int oldX = getScrollX();  
                int oldY = getScrollY();  
                int x = mScroller.getCurrX();  
                int y = mScroller.getCurrY();  
  
                if (oldX != x || oldY != y) {  
                    scrollTo(x, y);  
                }  
  
                if (mOnPageChangeListener != null) {  
                    final int widthWithMargin = getWidth() + mPageMargin;  
                    final int position = x / widthWithMargin;  
                    final int offsetPixels = x % widthWithMargin;  
                    final float offset = (float) offsetPixels / widthWithMargin;  
                    mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);  
                }  
  
                // Keep on drawing until the animation has finished.  
                invalidate();  
                return;  
            }  
        }  
  
        // Done with scroll, clean up state.  
        completeScroll();  
    }  
  
    private void completeScroll() {  
        boolean needPopulate = mScrolling;  
        if (needPopulate) {  
            // Done with scroll, no longer want to cache view drawing.  
            setScrollingCacheEnabled(false);  
            mScroller.abortAnimation();  
            int oldX = getScrollX();  
            int oldY = getScrollY();  
            int x = mScroller.getCurrX();  
            int y = mScroller.getCurrY();  
            if (oldX != x || oldY != y) {  
                scrollTo(x, y);  
            }  
            setScrollState(SCROLL_STATE_IDLE);  
        }  
        mPopulatePending = false;  
        mScrolling = false;  
        for (int i=0; i<mItems.size(); i++) {  
            ItemInfo ii = mItems.get(i);  
            if (ii.scrolling) {  
                needPopulate = true;  
                ii.scrolling = false;  
            }  
        }  
        if (needPopulate) {  
            populate();  
        }  
    }  
  
    @Override  
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        /* 
         * This method JUST determines whether we want to intercept the motion. 
         * If we return true, onMotionEvent will be called and we do the actual 
         * scrolling there. 
         */  
  
        final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;  
  
        // Always take care of the touch gesture being complete.  
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {  
            // Release the drag.  
            if (DEBUG) Log.v(TAG, "Intercept done!");  
            mIsBeingDragged = false;  
            mIsUnableToDrag = false;  
            mActivePointerId = INVALID_POINTER;  
            return false;  
        }  
  
        // Nothing more to do here if we have decided whether or not we  
        // are dragging.  
        if (action != MotionEvent.ACTION_DOWN) {  
            if (mIsBeingDragged) {  
                if (DEBUG) Log.v(TAG, "Intercept returning true!");  
                return true;  
            }  
            if (mIsUnableToDrag) {  
                if (DEBUG) Log.v(TAG, "Intercept returning false!");  
                return false;  
            }  
        }  
  
        switch (action) {  
            case MotionEvent.ACTION_MOVE: {  
                /* 
                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check 
                 * whether the user has moved far enough from his original down touch. 
                 */  
  
                /* 
                * Locally do absolute value. mLastMotionY is set to the y value 
                * of the down event. 
                */  
                final int activePointerId = mActivePointerId;  
                if (activePointerId == INVALID_POINTER) {  
                    // If we don't have a valid id, the touch down wasn't on content.  
                    break;  
                }  
  
                final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);  
                final float x = MotionEventCompat.getX(ev, pointerIndex);  
                final float dx = x - mLastMotionX;  
                final float xDiff = Math.abs(dx);  
                final float y = MotionEventCompat.getY(ev, pointerIndex);  
                final float yDiff = Math.abs(y - mLastMotionY);  
                final int scrollX = getScrollX();  
                final boolean atEdge = (dx > 0 && scrollX == 0) || (dx < 0 && mAdapter != null &&  
                        scrollX >= (mAdapter.getCount() - 1) * getWidth() - 1);  
                if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);  
  
                if (canScroll(this, false, (int) dx, (int) x, (int) y)) {  
                    // Nested view has scrollable area under this point. Let it be handled there.  
                    mInitialMotionX = mLastMotionX = x;  
                    mLastMotionY = y;  
                    return false;  
                }  
                if (xDiff > mTouchSlop && xDiff > yDiff) {  
                    if (DEBUG) Log.v(TAG, "Starting drag!");  
                    mIsBeingDragged = true;  
                    setScrollState(SCROLL_STATE_DRAGGING);  
                    mLastMotionX = x;  
                    setScrollingCacheEnabled(true);  
                } else {  
                    if (yDiff > mTouchSlop) {  
                        // The finger has moved enough in the vertical  
                        // direction to be counted as a drag...  abort  
                        // any attempt to drag horizontally, to work correctly  
                        // with children that have scrolling containers.  
                        if (DEBUG) Log.v(TAG, "Starting unable to drag!");  
                        mIsUnableToDrag = true;  
                    }  
                }  
                break;  
            }  
  
            case MotionEvent.ACTION_DOWN: {  
                /* 
                 * Remember location of down touch. 
                 * ACTION_DOWN always refers to pointer index 0. 
                 */  
                mLastMotionX = mInitialMotionX = ev.getX();  
                mLastMotionY = ev.getY();  
                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);  
  
                if (mScrollState == SCROLL_STATE_SETTLING) {  
                    // Let the user 'catch' the pager as it animates.  
                    mIsBeingDragged = true;  
                    mIsUnableToDrag = false;  
                    setScrollState(SCROLL_STATE_DRAGGING);  
                } else {  
                    completeScroll();  
                    mIsBeingDragged = false;  
                    mIsUnableToDrag = false;  
                }  
  
                if (DEBUG) Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY  
                        + " mIsBeingDragged=" + mIsBeingDragged  
                        + "mIsUnableToDrag=" + mIsUnableToDrag);  
                break;  
            }  
  
            case MotionEventCompat.ACTION_POINTER_UP:  
                onSecondaryPointerUp(ev);  
                break;  
        }  
  
        /* 
        * The only time we want to intercept motion events is if we are in the 
        * drag mode. 
        */  
        return mIsBeingDragged;  
    }  
  
    @Override  
    public boolean onTouchEvent(MotionEvent ev) {  
        if (mFakeDragging) {  
            // A fake drag is in progress already, ignore this real one  
            // but still eat the touch events.  
            // (It is likely that the user is multi-touching the screen.)  
            return true;  
        }  
  
        if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {  
            // Don't handle edge touches immediately -- they may actually belong to one of our  
            // descendants.  
            return false;  
        }  
  
        if (mAdapter == null || mAdapter.getCount() == 0) {  
            // Nothing to present or scroll; nothing to touch.  
            return false;  
        }  
  
        if (mVelocityTracker == null) {  
            mVelocityTracker = VelocityTracker.obtain();  
        }  
        mVelocityTracker.addMovement(ev);  
  
        final int action = ev.getAction();  
        boolean needsInvalidate = false;  
  
        switch (action & MotionEventCompat.ACTION_MASK) {  
            case MotionEvent.ACTION_DOWN: {  
                /* 
                 * If being flinged and user touches, stop the fling. isFinished 
                 * will be false if being flinged. 
                 */  
                completeScroll();  
  
                // Remember where the motion event started  
                mLastMotionX = mInitialMotionX = ev.getX();  
                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);  
                break;  
            }  
            case MotionEvent.ACTION_MOVE:  
                if (!mIsBeingDragged) {  
                    final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);  
                    final float x = MotionEventCompat.getX(ev, pointerIndex);  
                    final float xDiff = Math.abs(x - mLastMotionX);  
                    final float y = MotionEventCompat.getY(ev, pointerIndex);  
                    final float yDiff = Math.abs(y - mLastMotionY);  
                    if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);  
                    if (xDiff > mTouchSlop && xDiff > yDiff) {  
                        if (DEBUG) Log.v(TAG, "Starting drag!");  
                        mIsBeingDragged = true;  
                        mLastMotionX = x;  
                        setScrollState(SCROLL_STATE_DRAGGING);  
                        setScrollingCacheEnabled(true);  
                    }  
                }  
                if (mIsBeingDragged) {  
                    // Scroll to follow the motion event  
                    final int activePointerIndex = MotionEventCompat.findPointerIndex(  
                            ev, mActivePointerId);  
                    final float x = MotionEventCompat.getX(ev, activePointerIndex);  
                    final float deltaX = mLastMotionX - x;  
                    mLastMotionX = x;  
                    float oldScrollX = getScrollX();  
                    float scrollX = oldScrollX + deltaX;  
                    final int width = getWidth();  
                    final int widthWithMargin = width + mPageMargin;  
  
                    final int lastItemIndex = mAdapter.getCount() - 1;  
                    final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);  
                    final float rightBound =  
                            Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin;  
                    if (scrollX < leftBound) {  
                        if (leftBound == 0) {  
                            float over = -scrollX;  
                            needsInvalidate = mLeftEdge.onPull(over / width);  
                        }  
                        scrollX = leftBound;  
                    } else if (scrollX > rightBound) {  
                        if (rightBound == lastItemIndex * widthWithMargin) {  
                            float over = scrollX - rightBound;  
                            needsInvalidate = mRightEdge.onPull(over / width);  
                        }  
                        scrollX = rightBound;  
                    }  
                    // Don't lose the rounded component  
                    mLastMotionX += scrollX - (int) scrollX;  
                    scrollTo((int) scrollX, getScrollY());  
                    if (mOnPageChangeListener != null) {  
                        final int position = (int) scrollX / widthWithMargin;  
                        final int positionOffsetPixels = (int) scrollX % widthWithMargin;  
                        final float positionOffset = (float) positionOffsetPixels / widthWithMargin;  
                        mOnPageChangeListener.onPageScrolled(position, positionOffset,  
                                positionOffsetPixels);  
                    }  
                }  
                break;  
            case MotionEvent.ACTION_UP:  
                if (mIsBeingDragged) {  
                    final VelocityTracker velocityTracker = mVelocityTracker;  
                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);  
                    int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(  
                            velocityTracker, mActivePointerId);  
                    mPopulatePending = true;  
                    final int widthWithMargin = getWidth() + mPageMargin;  
                    final int scrollX = getScrollX();  
                    final int currentPage = scrollX / widthWithMargin;  
                    int nextPage = initialVelocity > 0 ? currentPage : currentPage + 1;  
                    setCurrentItemInternal(nextPage, true, true, initialVelocity);  
  
                    mActivePointerId = INVALID_POINTER;  
                    endDrag();  
                    needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();  
                }  
                break;  
            case MotionEvent.ACTION_CANCEL:  
                if (mIsBeingDragged) {  
                    setCurrentItemInternal(mCurItem, true, true);  
                    mActivePointerId = INVALID_POINTER;  
                    endDrag();  
                    needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();  
                }  
                break;  
            case MotionEventCompat.ACTION_POINTER_DOWN: {  
                final int index = MotionEventCompat.getActionIndex(ev);  
                final float x = MotionEventCompat.getX(ev, index);  
                mLastMotionX = x;  
                mActivePointerId = MotionEventCompat.getPointerId(ev, index);  
                break;  
            }  
            case MotionEventCompat.ACTION_POINTER_UP:  
                onSecondaryPointerUp(ev);  
                mLastMotionX = MotionEventCompat.getX(ev,  
                        MotionEventCompat.findPointerIndex(ev, mActivePointerId));  
                break;  
        }  
        if (needsInvalidate) {  
            invalidate();  
        }  
        return true;  
    }  
  
    @Override  
    public void draw(Canvas canvas) {  
        super.draw(canvas);  
        boolean needsInvalidate = false;  
  
        final int overScrollMode = ViewCompat.getOverScrollMode(this);  
        if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS ||  
                (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS &&  
                        mAdapter != null && mAdapter.getCount() > 1)) {  
            if (!mLeftEdge.isFinished()) {  
                final int restoreCount = canvas.save();  
                final int height = getHeight() - getPaddingTop() - getPaddingBottom();  
  
                canvas.rotate(270);  
                canvas.translate(-height + getPaddingTop(), 0);  
                mLeftEdge.setSize(height, getWidth());  
                needsInvalidate |= mLeftEdge.draw(canvas);  
                canvas.restoreToCount(restoreCount);  
            }  
            if (!mRightEdge.isFinished()) {  
                final int restoreCount = canvas.save();  
                final int width = getWidth();  
                final int height = getHeight() - getPaddingTop() - getPaddingBottom();  
                final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;  
  
                canvas.rotate(90);  
                canvas.translate(-getPaddingTop(),  
                        -itemCount * (width + mPageMargin) + mPageMargin);  
                mRightEdge.setSize(height, width);  
                needsInvalidate |= mRightEdge.draw(canvas);  
                canvas.restoreToCount(restoreCount);  
            }  
        } else {  
            mLeftEdge.finish();  
            mRightEdge.finish();  
        }  
  
        if (needsInvalidate) {  
            // Keep animating  
            invalidate();  
        }  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
  
        // Draw the margin drawable if needed.  
        if (mPageMargin > 0 && mMarginDrawable != null) {  
            final int scrollX = getScrollX();  
            final int width = getWidth();  
            final int offset = scrollX % (width + mPageMargin);  
            if (offset != 0) {  
                // Pages fit completely when settled; we only need to draw when in between  
                final int left = scrollX - offset + width;  
                mMarginDrawable.setBounds(left, 0, left + mPageMargin, getHeight());  
                mMarginDrawable.draw(canvas);  
            }  
        }  
    }  
  
    /** 
     * Start a fake drag of the pager. 
     * 
     * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager 
     * with the touch scrolling of another view, while still letting the ViewPager 
     * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) 
     * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call 
     * {@link #endFakeDrag()} to complete the fake drag and fling as necessary. 
     * 
     * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag 
     * is already in progress, this method will return false. 
     * 
     * @return true if the fake drag began successfully, false if it could not be started. 
     * 
     * @see #fakeDragBy(float) 
     * @see #endFakeDrag() 
     */  
    public boolean beginFakeDrag() {  
        if (mIsBeingDragged) {  
            return false;  
        }  
        mFakeDragging = true;  
        setScrollState(SCROLL_STATE_DRAGGING);  
        mInitialMotionX = mLastMotionX = 0;  
        if (mVelocityTracker == null) {  
            mVelocityTracker = VelocityTracker.obtain();  
        } else {  
            mVelocityTracker.clear();  
        }  
        final long time = SystemClock.uptimeMillis();  
        final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);  
        mVelocityTracker.addMovement(ev);  
        ev.recycle();  
        mFakeDragBeginTime = time;  
        return true;  
    }  
  
    /** 
     * End a fake drag of the pager. 
     * 
     * @see #beginFakeDrag() 
     * @see #fakeDragBy(float) 
     */  
    public void endFakeDrag() {  
        if (!mFakeDragging) {  
            throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");  
        }  
  
        final VelocityTracker velocityTracker = mVelocityTracker;  
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);  
        int initialVelocity = (int)VelocityTrackerCompat.getYVelocity(  
                velocityTracker, mActivePointerId);  
        mPopulatePending = true;  
        if ((Math.abs(initialVelocity) > mMinimumVelocity)  
                || Math.abs(mInitialMotionX-mLastMotionX) >= (getWidth()/3)) {  
            if (mLastMotionX > mInitialMotionX) {  
                setCurrentItemInternal(mCurItem-1, true, true);  
            } else {  
                setCurrentItemInternal(mCurItem+1, true, true);  
            }  
        } else {  
            setCurrentItemInternal(mCurItem, true, true);  
        }  
        endDrag();  
  
        mFakeDragging = false;  
    }  
  
    /** 
     * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first. 
     * 
     * @param xOffset Offset in pixels to drag by. 
     * @see #beginFakeDrag() 
     * @see #endFakeDrag() 
     */  
    public void fakeDragBy(float xOffset) {  
        if (!mFakeDragging) {  
            throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");  
        }  
  
        mLastMotionX += xOffset;  
        float scrollX = getScrollX() - xOffset;  
        final int width = getWidth();  
        final int widthWithMargin = width + mPageMargin;  
  
        final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);  
        final float rightBound =  
                Math.min(mCurItem + 1, mAdapter.getCount() - 1) * widthWithMargin;  
        if (scrollX < leftBound) {  
            scrollX = leftBound;  
        } else if (scrollX > rightBound) {  
            scrollX = rightBound;  
        }  
        // Don't lose the rounded component  
        mLastMotionX += scrollX - (int) scrollX;  
        scrollTo((int) scrollX, getScrollY());  
        if (mOnPageChangeListener != null) {  
            final int position = (int) scrollX / widthWithMargin;  
            final int positionOffsetPixels = (int) scrollX % widthWithMargin;  
            final float positionOffset = (float) positionOffsetPixels / widthWithMargin;  
            mOnPageChangeListener.onPageScrolled(position, positionOffset,  
                    positionOffsetPixels);  
        }  
  
        // Synthesize an event for the VelocityTracker.  
        final long time = SystemClock.uptimeMillis();  
        final MotionEvent ev = MotionEvent.obtain(mFakeDragBeginTime, time, MotionEvent.ACTION_MOVE,  
                mLastMotionX, 0, 0);  
        mVelocityTracker.addMovement(ev);  
        ev.recycle();  
    }  
  
    /** 
     * Returns true if a fake drag is in progress. 
     * 
     * @return true if currently in a fake drag, false otherwise. 
     * 
     * @see #beginFakeDrag() 
     * @see #fakeDragBy(float) 
     * @see #endFakeDrag() 
     */  
    public boolean isFakeDragging() {  
        return mFakeDragging;  
    }  
  
    private void onSecondaryPointerUp(MotionEvent ev) {  
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);  
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);  
        if (pointerId == mActivePointerId) {  
            // This was our active pointer going up. Choose a new  
            // active pointer and adjust accordingly.  
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;  
            mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);  
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);  
            if (mVelocityTracker != null) {  
                mVelocityTracker.clear();  
            }  
        }  
    }  
  
    private void endDrag() {  
        mIsBeingDragged = false;  
        mIsUnableToDrag = false;  
  
        if (mVelocityTracker != null) {  
            mVelocityTracker.recycle();  
            mVelocityTracker = null;  
        }  
    }  
  
    private void setScrollingCacheEnabled(boolean enabled) {  
        if (mScrollingCacheEnabled != enabled) {  
            mScrollingCacheEnabled = enabled;  
            if (USE_CACHE) {  
                final int size = getChildCount();  
                for (int i = 0; i < size; ++i) {  
                    final View child = getChildAt(i);  
                    if (child.getVisibility() != GONE) {  
                        child.setDrawingCacheEnabled(enabled);  
                    }  
                }  
            }  
        }  
    }  
  
    /** 
     * Tests scrollability within child views of v given a delta of dx. 
     * 
     * @param v View to test for horizontal scrollability 
     * @param checkV Whether the view v passed should itself be checked for scrollability (true), 
     *               or just its children (false). 
     * @param dx Delta scrolled in pixels 
     * @param x X coordinate of the active touch point 
     * @param y Y coordinate of the active touch point 
     * @return true if child views of v can be scrolled by delta of dx. 
     */  
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {  
        if (v instanceof ViewGroup) {  
            final ViewGroup group = (ViewGroup) v;  
            final int scrollX = v.getScrollX();  
            final int scrollY = v.getScrollY();  
            final int count = group.getChildCount();  
            // Count backwards - let topmost views consume scroll distance first.  
            for (int i = count - 1; i >= 0; i--) {  
                // TODO: Add versioned support here for transformed views.  
                // This will not work for transformed views in Honeycomb+  
                final View child = group.getChildAt(i);  
                if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&  
                        y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&  
                        canScroll(child, true, dx, x + scrollX - child.getLeft(),  
                                y + scrollY - child.getTop())) {  
                    return true;  
                }  
            }  
        }  
  
        return checkV && ViewCompat.canScrollHorizontally(v, -dx);  
    }  
  
    @Override  
    public boolean dispatchKeyEvent(KeyEvent event) {  
        // Let the focused view and/or our descendants get the key first  
        return super.dispatchKeyEvent(event) || executeKeyEvent(event);  
    }  
  
    /** 
     * You can call this function yourself to have the scroll view perform 
     * scrolling from a key event, just as if the event had been dispatched to 
     * it by the view hierarchy. 
     * 
     * @param event The key event to execute. 
     * @return Return true if the event was handled, else false. 
     */  
    public boolean executeKeyEvent(KeyEvent event) {  
        boolean handled = false;  
        if (event.getAction() == KeyEvent.ACTION_DOWN) {  
            switch (event.getKeyCode()) {  
                case KeyEvent.KEYCODE_DPAD_LEFT:  
                    handled = arrowScroll(FOCUS_LEFT);  
                    break;  
                case KeyEvent.KEYCODE_DPAD_RIGHT:  
                    handled = arrowScroll(FOCUS_RIGHT);  
                    break;  
                case KeyEvent.KEYCODE_TAB:  
                    if (KeyEventCompat.hasNoModifiers(event)) {  
                        handled = arrowScroll(FOCUS_FORWARD);  
                    } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {  
                        handled = arrowScroll(FOCUS_BACKWARD);  
                    }  
                    break;  
            }  
        }  
        return handled;  
    }  
  
    public boolean arrowScroll(int direction) {  
        View currentFocused = findFocus();  
        if (currentFocused == this) currentFocused = null;  
  
        boolean handled = false;  
  
        View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,  
                direction);  
        if (nextFocused != null && nextFocused != currentFocused) {  
            if (direction == View.FOCUS_LEFT) {  
                // If there is nothing to the left, or this is causing us to  
                // jump to the right, then what we really want to do is page left.  
                if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {  
                    handled = pageLeft();  
                } else {  
                    handled = nextFocused.requestFocus();  
                }  
            } else if (direction == View.FOCUS_RIGHT) {  
                // If there is nothing to the right, or this is causing us to  
                // jump to the left, then what we really want to do is page right.  
                if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {  
                    handled = pageRight();  
                } else {  
                    handled = nextFocused.requestFocus();  
                }  
            }  
        } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {  
            // Trying to move left and nothing there; try to page.  
            handled = pageLeft();  
        } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {  
            // Trying to move right and nothing there; try to page.  
            handled = pageRight();  
        }  
        if (handled) {  
            playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));  
        }  
        return handled;  
    }  
  
    boolean pageLeft() {  
        if (mCurItem > 0) {  
            setCurrentItem(mCurItem-1, true);  
            return true;  
        }  
        return false;  
    }  
  
    boolean pageRight() {  
        if (mAdapter != null && mCurItem < (mAdapter.getCount()-1)) {  
            setCurrentItem(mCurItem+1, true);  
            return true;  
        }  
        return false;  
    }  
  
    /** 
     * We only want the current page that is being shown to be focusable. 
     */  
    @Override  
    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {  
        final int focusableCount = views.size();  
  
        final int descendantFocusability = getDescendantFocusability();  
  
        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {  
            for (int i = 0; i < getChildCount(); i++) {  
                final View child = getChildAt(i);  
                if (child.getVisibility() == VISIBLE) {  
                    ItemInfo ii = infoForChild(child);  
                    if (ii != null && ii.position == mCurItem) {  
                        child.addFocusables(views, direction, focusableMode);  
                    }  
                }  
            }  
        }  
  
        // we add ourselves (if focusable) in all cases except for when we are  
        // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable.  this is  
        // to avoid the focus search finding layouts when a more precise search  
        // among the focusable children would be more interesting.  
        if (  
            descendantFocusability != FOCUS_AFTER_DESCENDANTS ||  
                // No focusable descendants  
                (focusableCount == views.size())) {  
            // Note that we can't call the superclass here, because it will  
            // add all views in.  So we need to do the same thing View does.  
            if (!isFocusable()) {  
                return;  
            }  
            if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&  
                    isInTouchMode() && !isFocusableInTouchMode()) {  
                return;  
            }  
            if (views != null) {  
                views.add(this);  
            }  
        }  
    }  
  
    /** 
     * We only want the current page that is being shown to be touchable. 
     */  
    @Override  
    public void addTouchables(ArrayList<View> views) {  
        // Note that we don't call super.addTouchables(), which means that  
        // we don't call View.addTouchables().  This is okay because a ViewPager  
        // is itself not touchable.  
        for (int i = 0; i < getChildCount(); i++) {  
            final View child = getChildAt(i);  
            if (child.getVisibility() == VISIBLE) {  
                ItemInfo ii = infoForChild(child);  
                if (ii != null && ii.position == mCurItem) {  
                    child.addTouchables(views);  
                }  
            }  
        }  
    }  
  
    /** 
     * We only want the current page that is being shown to be focusable. 
     */  
    @Override  
    protected boolean onRequestFocusInDescendants(int direction,  
            Rect previouslyFocusedRect) {  
        int index;  
        int increment;  
        int end;  
        int count = getChildCount();  
        if ((direction & FOCUS_FORWARD) != 0) {  
            index = 0;  
            increment = 1;  
            end = count;  
        } else {  
            index = count - 1;  
            increment = -1;  
            end = -1;  
        }  
        for (int i = index; i != end; i += increment) {  
            View child = getChildAt(i);  
            if (child.getVisibility() == VISIBLE) {  
                ItemInfo ii = infoForChild(child);  
                if (ii != null && ii.position == mCurItem) {  
                    if (child.requestFocus(direction, previouslyFocusedRect)) {  
                        return true;  
                    }  
                }  
            }  
        }  
        return false;  
    }  
  
    @Override  
    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {  
        // ViewPagers should only report accessibility info for the current page,  
        // otherwise things get very confusing.  
  
        // TODO: Should this note something about the paging container?  
  
        final int childCount = getChildCount();  
        for (int i = 0; i < childCount; i++) {  
            final View child = getChildAt(i);  
            if (child.getVisibility() == VISIBLE) {  
                final ItemInfo ii = infoForChild(child);  
                if (ii != null && ii.position == mCurItem &&  
                        child.dispatchPopulateAccessibilityEvent(event)) {  
                    return true;  
                }  
            }  
        }  
  
        return false;  
    }  
  
    private class PagerObserver extends DataSetObserver {  
  
        @Override  
        public void onChanged() {  
            dataSetChanged();  
        }  
  
        @Override  
        public void onInvalidated() {  
            dataSetChanged();  
        }  
    }  
}  


那么对应的Adapter中该如何实例化item和销毁item呢?

来源:http://blog.csdn.net/wangbaochu/article/details/51485226

package com.wtdc.codebook.camera.adapter;

import java.util.List;

import com.bumptech.glide.Glide;
import com.wtdc.codebook.R;
import com.wtdc.codebook.camera.utils.NoPreloadViewPager;
import com.wtdc.codebook.ui.view.PinchImageView;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public class IntruderViewPagerAdapter extends PagerAdapter {

	private Context mContext;
	private List<String> mDrawableResIdList;

	public IntruderViewPagerAdapter(Context context, List<String> resIdList) {
		super();
		mContext = context;
		mDrawableResIdList = resIdList;
	}

	@Override
	public int getCount() {
		if (mDrawableResIdList != null) {
			return mDrawableResIdList.size();
		}
		return 0;
	}

	@Override
	public int getItemPosition(Object object) {
		if (object != null && mDrawableResIdList != null) {
			String resId = (String)((PinchImageView) object).getTag();
			if (resId != null) {
				for (int i = 0; i < mDrawableResIdList.size(); i++) {
					if (resId.equals(mDrawableResIdList.get(i))) {
						return i;
					}
				}
			}
		}
		return 0;
	}

	@Override
	public Object instantiateItem(View container, int position) {
		if (mDrawableResIdList != null && position < mDrawableResIdList.size()) {
			String resId = mDrawableResIdList.get(position);
			if (resId != null) {
				PinchImageView piv = new PinchImageView(mContext);
				Glide.with(mContext)
                .load(resId)
                .placeholder(R.drawable.ic_stub)
                .error(R.drawable.ic_error)
                .into(piv);
				piv.setTag(resId);
				((NoPreloadViewPager) container).addView(piv);
				return piv;
			}
		}
		return null;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		// 注意:此处position是ViewPager中所有要显示的页面的position,与Adapter
		// mDrawableResIdList并不是一一对应的。
		// 因为mDrawableResIdList有可能被修改删除某一个item,在调用notifyDataSetChanged()的时候,ViewPager中的页面
		// 数量并没有改变,只有当ViewPager遍历完自己所有的页面,并将不存在的页面删除后,二者才能对应起来
		if (object != null) {
			ViewGroup viewPager = ((ViewGroup) container);
			int count = viewPager.getChildCount();
			for (int i = 0; i < count; i++) {
				View childView = viewPager.getChildAt(i);
				if (childView == object) {
					viewPager.removeView(childView);
					break;
				}
			}
		}
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return (view == object);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public void startUpdate(View container) {
	}

	@Override
	public void finishUpdate(View container) {
	}

	public void updateData(List<String> itemsResId) {
		if (itemsResId == null) {
			return;
		}
		mDrawableResIdList = itemsResId;
		this.notifyDataSetChanged();
	}
}

以上两个类结合使用便可以有效地实现ViewPager的禁用预加载。当然,谷歌既然有这么一种ViewPager的预加载机制肯定有它的道理,所以一般还是预加载的好,毕竟流畅性体验方面都会更好。



下面介绍另一个类,ViewPager这种禁用多使用在加载大量的高清原图,那么ImageView的单一功能不能方便的手势控制预览高清大图,下面就是一种ImageView的替代方式。

package com.wtdc.codebook.ui.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 手势图片控件
 *
 * @author clifford
 */
public class PinchImageView extends ImageView  {


    配置参数

    /**
     * 图片缩放动画时间
     */
    public static final int SCALE_ANIMATOR_DURATION = 200;

    /**
     * 惯性动画衰减参数
     */
    public static final float FLING_DAMPING_FACTOR = 0.9f;

    /**
     * 图片最大放大比例
     */
    private static final float MAX_SCALE = 4f;


    监听器

    /**
     * 外界点击事件
     *
     * @see #setOnClickListener(OnClickListener)
     */
    private OnClickListener mOnClickListener;

    /**
     * 外界长按事件
     *
     * @see #setOnLongClickListener(OnLongClickListener)
     */
    private OnLongClickListener mOnLongClickListener;

    @Override
    public void setOnClickListener(OnClickListener l) {
        //默认的click会在任何点击情况下都会触发,所以搞成自己的
        mOnClickListener = l;
    }

    @Override
    public void setOnLongClickListener(OnLongClickListener l) {
        //默认的long click会在任何长按情况下都会触发,所以搞成自己的
        mOnLongClickListener = l;
    }


    公共状态获取

    /**
     * 手势状态:自由状态
     *
     * @see #getPinchMode()
     */
    public static final int PINCH_MODE_FREE = 0;

    /**
     * 手势状态:单指滚动状态
     *
     * @see #getPinchMode()
     */
    public static final int PINCH_MODE_SCROLL = 1;

    /**
     * 手势状态:双指缩放状态
     *
     * @see #getPinchMode()
     */
    public static final int PINCH_MODE_SCALE = 2;

    /**
     * 外层变换矩阵,如果是单位矩阵,那么图片是fit center状态
     *
     * @see #getOuterMatrix(Matrix)
     * @see #outerMatrixTo(Matrix, long)
     */
    private Matrix mOuterMatrix = new Matrix();

    /**
     * 矩形遮罩
     *
     * @see #getMask()
     * @see #zoomMaskTo(RectF, long)
     */
    private RectF mMask;

    /**
     * 当前手势状态
     *
     * @see #getPinchMode()
     * @see #PINCH_MODE_FREE
     * @see #PINCH_MODE_SCROLL
     * @see #PINCH_MODE_SCALE
     */
    private int mPinchMode = PINCH_MODE_FREE;

    /**
     * 获取外部变换矩阵.
     *
     * 外部变换矩阵记录了图片手势操作的最终结果,是相对于图片fit center状态的变换.
     * 默认值为单位矩阵,此时图片为fit center状态.
     *
     * @param matrix 用于填充结果的对象
     * @return 如果传了matrix参数则将matrix填充后返回,否则new一个填充返回
     */
    public Matrix getOuterMatrix(Matrix matrix) {
        if (matrix == null) {
            matrix = new Matrix(mOuterMatrix);
        } else {
            matrix.set(mOuterMatrix);
        }
        return matrix;
    }

    /**
     * 获取内部变换矩阵.
     *
     * 内部变换矩阵是原图到fit center状态的变换,当原图尺寸变化或者控件大小变化都会发生改变
     * 当尚未布局或者原图不存在时,其值无意义.所以在调用前需要确保前置条件有效,否则将影响计算结果.
     *
     * @param matrix 用于填充结果的对象
     * @return 如果传了matrix参数则将matrix填充后返回,否则new一个填充返回
     */
    public Matrix getInnerMatrix(Matrix matrix) {
        if (matrix == null) {
            matrix = new Matrix();
        } else {
            matrix.reset();
        }
        if (isReady()) {
            //原图大小
            RectF tempSrc = MathUtils.rectFTake(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
            //控件大小
            RectF tempDst = MathUtils.rectFTake(0, 0, getWidth(), getHeight());
            //计算fit center矩阵
            matrix.setRectToRect(tempSrc, tempDst, Matrix.ScaleToFit.CENTER);
            //释放临时对象
            MathUtils.rectFGiven(tempDst);
            MathUtils.rectFGiven(tempSrc);
        }
        return matrix;
    }

    /**
     * 获取图片总变换矩阵.
     *
     * 总变换矩阵为内部变换矩阵x外部变换矩阵,决定了原图到所见最终状态的变换
     * 当尚未布局或者原图不存在时,其值无意义.所以在调用前需要确保前置条件有效,否则将影响计算结果.
     *
     * @param matrix 用于填充结果的对象
     * @return 如果传了matrix参数则将matrix填充后返回,否则new一个填充返回
     *
     * @see #getOuterMatrix(Matrix)
     * @see #getInnerMatrix(Matrix)
     */
    public Matrix getCurrentImageMatrix(Matrix matrix) {
        //获取内部变换矩阵
        matrix = getInnerMatrix(matrix);
        //乘上外部变换矩阵
        matrix.postConcat(mOuterMatrix);
        return matrix;
    }

    /**
     * 获取当前变换后的图片位置和尺寸
     *
     * 当尚未布局或者原图不存在时,其值无意义.所以在调用前需要确保前置条件有效,否则将影响计算结果.
     *
     * @param rectF 用于填充结果的对象
     * @return 如果传了rectF参数则将rectF填充后返回,否则new一个填充返回
     *
     * @see #getCurrentImageMatrix(Matrix)
     */
    public RectF getImageBound(RectF rectF) {
        if (rectF == null) {
            rectF = new RectF();
        } else {
            rectF.setEmpty();
        }
        if (!isReady()) {
            return rectF;
        } else {
            //申请一个空matrix
            Matrix matrix = MathUtils.matrixTake();
            //获取当前总变换矩阵
            getCurrentImageMatrix(matrix);
            //对原图矩形进行变换得到当前显示矩形
            rectF.set(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
            matrix.mapRect(rectF);
            //释放临时matrix
            MathUtils.matrixGiven(matrix);
            return rectF;
        }
    }

    /**
     * 获取当前设置的mask
     *
     * @return 返回当前的mask对象副本,如果当前没有设置mask则返回null
     */
    public RectF getMask() {
        if (mMask != null) {
            return new RectF(mMask);
        } else {
            return null;
        }
    }

    /**
     * 获取当前手势状态
     *
     * @see #PINCH_MODE_FREE
     * @see #PINCH_MODE_SCROLL
     * @see #PINCH_MODE_SCALE
     */
    public int getPinchMode() {
        return mPinchMode;
    }

    /**
     * 与ViewPager结合的时候使用
     * @param direction
     * @return
     */
    @Override
    public boolean canScrollHorizontally(int direction) {
        if (mPinchMode == PinchImageView.PINCH_MODE_SCALE) {
            return true;
        }
        RectF bound = getImageBound(null);
        if (bound == null) {
            return false;
        }
        if (bound.isEmpty()) {
            return false;
        }
        if (direction > 0) {
            return bound.right > getWidth();
        } else {
            return bound.left < 0;
        }
    }

    /**
     * 与ViewPager结合的时候使用
     * @param direction
     * @return
     */
    @Override
    public boolean canScrollVertically(int direction) {
        if (mPinchMode == PinchImageView.PINCH_MODE_SCALE) {
            return true;
        }
        RectF bound = getImageBound(null);
        if (bound == null) {
            return false;
        }
        if (bound.isEmpty()) {
            return false;
        }
        if (direction > 0) {
            return bound.bottom > getHeight();
        } else {
            return bound.top < 0;
        }
    }


    公共状态设置

    /**
     * 执行当前outerMatrix到指定outerMatrix渐变的动画
     *
     * 调用此方法会停止正在进行中的手势以及手势动画.
     * 当duration为0时,outerMatrix值会被立即设置而不会启动动画.
     *
     * @param endMatrix 动画目标矩阵
     * @param duration 动画持续时间
     *
     * @see #getOuterMatrix(Matrix)
     */
    public void outerMatrixTo(Matrix endMatrix, long duration) {
        if (endMatrix == null) {
            return;
        }
        //将手势设置为PINCH_MODE_FREE将停止后续手势的触发
        mPinchMode = PINCH_MODE_FREE;
        //停止所有正在进行的动画
        cancelAllAnimator();
        //如果时间不合法立即执行结果
        if (duration <= 0) {
            mOuterMatrix.set(endMatrix);
            dispatchOuterMatrixChanged();
            invalidate();
        } else {
            //创建矩阵变化动画
            mScaleAnimator = new ScaleAnimator(mOuterMatrix, endMatrix, duration);
            mScaleAnimator.start();
        }
    }

    /**
     * 执行当前mask到指定mask的变化动画
     *
     * 调用此方法不会停止手势以及手势相关动画,但会停止正在进行的mask动画.
     * 当前mask为null时,则不执行动画立即设置为目标mask.
     * 当duration为0时,立即将当前mask设置为目标mask,不会执行动画.
     *
     * @param mask 动画目标mask
     * @param duration 动画持续时间
     *
     * @see #getMask()
     */
    public void zoomMaskTo(RectF mask, long duration) {
        if (mask == null) {
            return;
        }
        //停止mask动画
        if (mMaskAnimator != null) {
            mMaskAnimator.cancel();
            mMaskAnimator = null;
        }
        //如果duration为0或者之前没有设置过mask,不执行动画,立即设置
        if (duration <= 0 || mMask == null) {
            if (mMask == null) {
                mMask = new RectF();
            }
            mMask.set(mask);
            invalidate();
        } else {
            //执行mask动画
            mMaskAnimator = new MaskAnimator(mMask, mask, duration);
            mMaskAnimator.start();
        }
    }

    /**
     * 重置所有状态
     *
     * 重置位置到fit center状态,清空mask,停止所有手势,停止所有动画.
     * 但不清空drawable,以及事件绑定相关数据.
     */
    public void reset() {
        //重置位置到fit
        mOuterMatrix.reset();
        dispatchOuterMatrixChanged();
        //清空mask
        mMask = null;
        //停止所有手势
        mPinchMode = PINCH_MODE_FREE;
        mLastMovePoint.set(0, 0);
        mScaleCenter.set(0, 0);
        mScaleBase = 0;
        //停止所有动画
        if (mMaskAnimator != null) {
            mMaskAnimator.cancel();
            mMaskAnimator = null;
        }
        cancelAllAnimator();
        //重绘
        invalidate();
    }


    对外广播事件

    /**
     * 外部矩阵变化事件通知监听器
     */
    public interface OuterMatrixChangedListener {

        /**
         * 外部矩阵变化回调
         *
         * 外部矩阵的任何变化后都收到此回调.
         * 外部矩阵变化后,总变化矩阵,图片的展示位置都将发生变化.
         *
         * @param pinchImageView
         *
         * @see #getOuterMatrix(Matrix)
         * @see #getCurrentImageMatrix(Matrix)
         * @see #getImageBound(RectF)
         */
        void onOuterMatrixChanged(PinchImageView pinchImageView);
    }

    /**
     * 所有OuterMatrixChangedListener监听列表
     *
     * @see #addOuterMatrixChangedListener(OuterMatrixChangedListener)
     * @see #removeOuterMatrixChangedListener(OuterMatrixChangedListener)
     */
    private List<OuterMatrixChangedListener> mOuterMatrixChangedListeners;

    /**
     * 当mOuterMatrixChangedListeners被锁定不允许修改时,临时将修改写到这个副本中
     *
     * @see #mOuterMatrixChangedListeners
     */
    private List<OuterMatrixChangedListener> mOuterMatrixChangedListenersCopy;

    /**
     * mOuterMatrixChangedListeners的修改锁定
     *
     * 当进入dispatchOuterMatrixChanged方法时,被加1,退出前被减1
     *
     * @see #dispatchOuterMatrixChanged()
     * @see #addOuterMatrixChangedListener(OuterMatrixChangedListener)
     * @see #removeOuterMatrixChangedListener(OuterMatrixChangedListener)
     */
    private int mDispatchOuterMatrixChangedLock;

    /**
     * 添加外部矩阵变化监听
     *
     * @param listener
     */
    public void addOuterMatrixChangedListener(OuterMatrixChangedListener listener) {
        if (listener == null) {
            return;
        }
        //如果监听列表没有被修改锁定直接将监听添加到监听列表
        if (mDispatchOuterMatrixChangedLock == 0) {
            if (mOuterMatrixChangedListeners == null) {
                mOuterMatrixChangedListeners = new ArrayList<OuterMatrixChangedListener>();
            }
            mOuterMatrixChangedListeners.add(listener);
        } else {
            //如果监听列表修改被锁定,那么尝试在监听列表副本上添加
            //监听列表副本将会在锁定被解除时替换到监听列表里
            if (mOuterMatrixChangedListenersCopy == null) {
                if (mOuterMatrixChangedListeners != null) {
                    mOuterMatrixChangedListenersCopy = new ArrayList<OuterMatrixChangedListener>(mOuterMatrixChangedListeners);
                } else {
                    mOuterMatrixChangedListenersCopy = new ArrayList<OuterMatrixChangedListener>();
                }
            }
            mOuterMatrixChangedListenersCopy.add(listener);
        }
    }

    /**
     * 删除外部矩阵变化监听
     *
     * @param listener
     */
    public void removeOuterMatrixChangedListener(OuterMatrixChangedListener listener) {
        if (listener == null) {
            return;
        }
        //如果监听列表没有被修改锁定直接在监听列表数据结构上修改
        if (mDispatchOuterMatrixChangedLock == 0) {
            if (mOuterMatrixChangedListeners != null) {
                mOuterMatrixChangedListeners.remove(listener);
            }
        } else {
            //如果监听列表被修改锁定,那么就在其副本上修改
            //其副本将会在锁定解除时替换回监听列表
            if (mOuterMatrixChangedListenersCopy == null) {
                if (mOuterMatrixChangedListeners != null) {
                    mOuterMatrixChangedListenersCopy = new ArrayList<OuterMatrixChangedListener>(mOuterMatrixChangedListeners);
                }
            }
            if (mOuterMatrixChangedListenersCopy != null) {
                mOuterMatrixChangedListenersCopy.remove(listener);
            }
        }
    }

    /**
     * 触发外部矩阵修改事件
     *
     * 需要在每次给外部矩阵设置值时都调用此方法.
     *
     * @see #mOuterMatrix
     */
    private void dispatchOuterMatrixChanged() {
        if (mOuterMatrixChangedListeners == null) {
            return;
        }
        //增加锁
        //这里之所以用计数器做锁定是因为可能在锁定期间又间接调用了此方法产生递归
        //使用boolean无法判断递归结束
        mDispatchOuterMatrixChangedLock++;
        //在列表循环过程中不允许修改列表,否则将引发崩溃
        for (OuterMatrixChangedListener listener : mOuterMatrixChangedListeners) {
            listener.onOuterMatrixChanged(this);
        }
        //减锁
        mDispatchOuterMatrixChangedLock--;
        //如果是递归的情况,mDispatchOuterMatrixChangedLock可能大于1,只有减到0才能算列表的锁定解除
        if (mDispatchOuterMatrixChangedLock == 0) {
            //如果期间有修改列表,那么副本将不为null
            if (mOuterMatrixChangedListenersCopy != null) {
                //将副本替换掉正式的列表
                mOuterMatrixChangedListeners = mOuterMatrixChangedListenersCopy;
                //清空副本
                mOuterMatrixChangedListenersCopy = null;
            }
        }
    }


    用于重载定制

    /**
     * 获取图片最大可放大的比例
     *
     * 如果放大大于这个比例则不被允许.
     * 在双手缩放过程中如果图片放大比例大于这个值,手指释放将回弹到这个比例.
     * 在双击放大过程中不允许放大比例大于这个值.
     * 覆盖此方法可以定制不同情况使用不同的最大可放大比例.
     *
     * @return 缩放比例
     *
     * @see #scaleEnd()
     * @see #doubleTap(float, float)
     */
    protected float getMaxScale() {
        return MAX_SCALE;
    }

    /**
     * 计算双击之后图片接下来应该被缩放的比例
     *
     * 如果值大于getMaxScale或者小于fit center尺寸,则实际使用取边界值.
     * 通过覆盖此方法可以定制不同的图片被双击时使用不同的放大策略.
     *
     * @param innerScale 当前内部矩阵的缩放值
     * @param outerScale 当前外部矩阵的缩放值
     * @return 接下来的缩放比例
     *
     * @see #doubleTap(float, float)
     * @see #getMaxScale()
     */
    protected float calculateNextScale(float innerScale, float outerScale) {
        float currentScale = innerScale * outerScale;
        if (currentScale < MAX_SCALE) {
            return MAX_SCALE;
        } else {
            return innerScale;
        }
    }


    初始化

    public PinchImageView(Context context) {
        super(context);
        initView();
    }

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

    public PinchImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        //强制设置图片scaleType为matrix
        super.setScaleType(ScaleType.MATRIX);
    }

    //不允许设置scaleType,只能用内部设置的matrix
    @Override
    public void setScaleType(ScaleType scaleType) {}


    绘制

    @Override
    protected void onDraw(Canvas canvas) {
        //在绘制前设置变换矩阵
        if (isReady()) {
            Matrix matrix = MathUtils.matrixTake();
            setImageMatrix(getCurrentImageMatrix(matrix));
            MathUtils.matrixGiven(matrix);
        }
        //对图像做遮罩处理
        if (mMask != null) {
            canvas.save();
            canvas.clipRect(mMask);
            super.onDraw(canvas);
            canvas.restore();
        } else {
            super.onDraw(canvas);
        }
    }


    有效性判断

    /**
     * 判断当前情况是否能执行手势相关计算
     *
     * 包括:是否有图片,图片是否有尺寸,控件是否有尺寸.
     *
     * @return 是否能执行手势相关计算
     */
    private boolean isReady() {
        return getDrawable() != null && getDrawable().getIntrinsicWidth() > 0 && getDrawable().getIntrinsicHeight() > 0
                && getWidth() > 0 && getHeight() > 0;
    }


    mask动画处理

    /**
     * mask修改的动画
     *
     * 和图片的动画相互独立.
     *
     * @see #zoomMaskTo(RectF, long)
     */
    private MaskAnimator mMaskAnimator;

    /**
     * mask变换动画
     *
     * 将mask从一个rect动画到另外一个rect
     */
    private class MaskAnimator extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener {

        /**
         * 开始mask
         */
        private float[] mStart = new float[4];

        /**
         * 结束mask
         */
        private float[] mEnd = new float[4];

        /**
         * 中间结果mask
         */
        private float[] mResult = new float[4];

        /**
         * 创建mask变换动画
         *
         * @param start 动画起始状态
         * @param end 动画终点状态
         * @param duration 动画持续时间
         */
        public MaskAnimator(RectF start, RectF end, long duration) {
            super();
            setFloatValues(0, 1f);
            setDuration(duration);
            addUpdateListener(this);
            //将起点终点拷贝到数组方便计算
            mStart[0] = start.left;
            mStart[1] = start.top;
            mStart[2] = start.right;
            mStart[3] = start.bottom;
            mEnd[0] = end.left;
            mEnd[1] = end.top;
            mEnd[2] = end.right;
            mEnd[3] = end.bottom;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            //获取动画进度,0-1范围
            float value = (Float) animation.getAnimatedValue();
            //根据进度对起点终点之间做插值
            for (int i = 0; i < 4; i++) {
                mResult[i] = mStart[i] + (mEnd[i] - mStart[i]) * value;
            }
            //期间mask有可能被置空了,所以判断一下
            if (mMask == null) {
                mMask = new RectF();
            }
            //设置新的mask并绘制
            mMask.set(mResult[0], mResult[1], mResult[2], mResult[3]);
            invalidate();
        }
    }


    手势动画处理

    /**
     * 在单指模式下:
     * 记录上一次手指的位置,用于计算新的位置和上一次位置的差值.
     *
     * 双指模式下:
     * 记录两个手指的中点,作为和mScaleCenter绑定的点.
     * 这个绑定可以保证mScaleCenter无论如何都会跟随这个中点.
     *
     * @see #mScaleCenter
     * @see #scale(PointF, float, float, PointF)
     * @see #scaleEnd()
     */
    private PointF mLastMovePoint = new PointF();

    /**
     * 缩放模式下图片的缩放中点.
     *
     * 为其指代的点经过innerMatrix变换之后的值.
     * 其指代的点在手势过程中始终跟随mLastMovePoint.
     * 通过双指缩放时,其为缩放中心点.
     *
     * @see #saveScaleContext(float, float, float, float)
     * @see #mLastMovePoint
     * @see #scale(PointF, float, float, PointF)
     */
    private PointF mScaleCenter = new PointF();

    /**
     * 缩放模式下的基础缩放比例
     *
     * 为外层缩放值除以开始缩放时两指距离.
     * 其值乘上最新的两指之间距离为最新的图片缩放比例.
     *
     * @see #saveScaleContext(float, float, float, float)
     * @see #scale(PointF, float, float, PointF)
     */
    private float mScaleBase = 0;

    /**
     * 图片缩放动画
     *
     * 缩放模式把图片的位置大小超出限制之后触发.
     * 双击图片放大或缩小时触发.
     * 手动调用outerMatrixTo触发.
     *
     * @see #scaleEnd()
     * @see #doubleTap(float, float)
     * @see #outerMatrixTo(Matrix, long)
     */
    private ScaleAnimator mScaleAnimator;

    /**
     * 滑动产生的惯性动画
     *
     * @see #fling(float, float)
     */
    private FlingAnimator mFlingAnimator;

    /**
     * 常用手势处理
     *
     * 在onTouchEvent末尾被执行.
     */
    private GestureDetector mGestureDetector = new GestureDetector(PinchImageView.this.getContext(), new GestureDetector.SimpleOnGestureListener() {

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            //只有在单指模式结束之后才允许执行fling
            if (mPinchMode == PINCH_MODE_FREE && !(mScaleAnimator != null && mScaleAnimator.isRunning())) {
                fling(velocityX, velocityY);
            }
            return true;
        }

        public void onLongPress(MotionEvent e) {
            //触发长按
            if (mOnLongClickListener != null) {
                mOnLongClickListener.onLongClick(PinchImageView.this);
            }
        }

        public boolean onDoubleTap(MotionEvent e) {
            //当手指快速第二次按下触发,此时必须是单指模式才允许执行doubleTap
            if (mPinchMode == PINCH_MODE_SCROLL && !(mScaleAnimator != null && mScaleAnimator.isRunning())) {
                doubleTap(e.getX(), e.getY());
            }
            return true;
        }

        public boolean onSingleTapConfirmed(MotionEvent e) {
            //触发点击
            if (mOnClickListener != null) {
                mOnClickListener.onClick(PinchImageView.this);
            }
            return true;
        }
    });

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        //最后一个点抬起或者取消,结束所有模式
        if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            //如果之前是缩放模式,还需要触发一下缩放结束动画
            if (mPinchMode == PINCH_MODE_SCALE) {
                scaleEnd();
            }
            mPinchMode = PINCH_MODE_FREE;
        } else if (action == MotionEvent.ACTION_POINTER_UP) {
            //多个手指情况下抬起一个手指,此时需要是缩放模式才触发
            if (mPinchMode == PINCH_MODE_SCALE) {
                //抬起的点如果大于2,那么缩放模式还有效,但是有可能初始点变了,重新测量初始点
                if (event.getPointerCount() > 2) {
                    //如果还没结束缩放模式,但是第一个点抬起了,那么让第二个点和第三个点作为缩放控制点
                    if (event.getAction() >> 8 == 0) {
                        saveScaleContext(event.getX(1), event.getY(1), event.getX(2), event.getY(2));
                        //如果还没结束缩放模式,但是第二个点抬起了,那么让第一个点和第三个点作为缩放控制点
                    } else if (event.getAction() >> 8 == 1) {
                        saveScaleContext(event.getX(0), event.getY(0), event.getX(2), event.getY(2));
                    }
                }
                //如果抬起的点等于2,那么此时只剩下一个点,也不允许进入单指模式,因为此时可能图片没有在正确的位置上
            }
            //第一个点按下,开启滚动模式,记录开始滚动的点
        } else if (action == MotionEvent.ACTION_DOWN) {
            //在矩阵动画过程中不允许启动滚动模式
            if (!(mScaleAnimator != null && mScaleAnimator.isRunning())) {
                //停止所有动画
                cancelAllAnimator();
                //切换到滚动模式
                mPinchMode = PINCH_MODE_SCROLL;
                //保存触发点用于move计算差值
                mLastMovePoint.set(event.getX(), event.getY());
            }
            //非第一个点按下,关闭滚动模式,开启缩放模式,记录缩放模式的一些初始数据
        } else if (action == MotionEvent.ACTION_POINTER_DOWN) {
            //停止所有动画
            cancelAllAnimator();
            //切换到缩放模式
            mPinchMode = PINCH_MODE_SCALE;
            //保存缩放的两个手指
            saveScaleContext(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (!(mScaleAnimator != null && mScaleAnimator.isRunning())) {
                //在滚动模式下移动
                if (mPinchMode == PINCH_MODE_SCROLL) {
                    //每次移动产生一个差值累积到图片位置上
                    scrollBy(event.getX() - mLastMovePoint.x, event.getY() - mLastMovePoint.y);
                    //记录新的移动点
                    mLastMovePoint.set(event.getX(), event.getY());
                    //在缩放模式下移动
                } else if (mPinchMode == PINCH_MODE_SCALE && event.getPointerCount() > 1) {
                    //两个缩放点间的距离
                    float distance = MathUtils.getDistance(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
                    //保存缩放点中点
                    float[] lineCenter = MathUtils.getCenterPoint(event.getX(0), event.getY(0), event.getX(1), event.getY(1));
                    mLastMovePoint.set(lineCenter[0], lineCenter[1]);
                    //处理缩放
                    scale(mScaleCenter, mScaleBase, distance, mLastMovePoint);
                }
            }
        }
        //无论如何都处理各种外部手势
        mGestureDetector.onTouchEvent(event);
        return true;
    }

    /**
     * 让图片移动一段距离
     *
     * 不能移动超过可移动范围,超过了就到可移动范围边界为止.
     *
     * @param xDiff 移动距离
     * @param yDiff 移动距离
     * @return 是否改变了位置
     */
    private boolean scrollBy(float xDiff, float yDiff) {
        if (!isReady()) {
            return false;
        }
        //原图方框
        RectF bound = MathUtils.rectFTake();
        getImageBound(bound);
        //控件大小
        float displayWidth = getWidth();
        float displayHeight = getHeight();
        //如果当前图片宽度小于控件宽度,则不能移动
        if (bound.right - bound.left < displayWidth) {
            xDiff = 0;
            //如果图片左边在移动后超出控件左边
        } else if (bound.left + xDiff > 0) {
            //如果在移动之前是没超出的,计算应该移动的距离
            if (bound.left < 0) {
                xDiff = -bound.left;
                //否则无法移动
            } else {
                xDiff = 0;
            }
            //如果图片右边在移动后超出控件右边
        } else if (bound.right + xDiff < displayWidth) {
            //如果在移动之前是没超出的,计算应该移动的距离
            if (bound.right > displayWidth) {
                xDiff = displayWidth - bound.right;
                //否则无法移动
            } else {
                xDiff = 0;
            }
        }
        //以下同理
        if (bound.bottom - bound.top < displayHeight) {
            yDiff = 0;
        } else if (bound.top + yDiff > 0) {
            if (bound.top < 0) {
                yDiff = -bound.top;
            } else {
                yDiff = 0;
            }
        } else if (bound.bottom + yDiff < displayHeight) {
            if (bound.bottom > displayHeight) {
                yDiff = displayHeight - bound.bottom;
            } else {
                yDiff = 0;
            }
        }
        MathUtils.rectFGiven(bound);
        //应用移动变换
        mOuterMatrix.postTranslate(xDiff, yDiff);
        dispatchOuterMatrixChanged();
        //触发重绘
        invalidate();
        //检查是否有变化
        if (xDiff != 0 || yDiff != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 记录缩放前的一些信息
     *
     * 保存基础缩放值.
     * 保存图片缩放中点.
     *
     * @param x1 缩放第一个手指
     * @param y1 缩放第一个手指
     * @param x2 缩放第二个手指
     * @param y2 缩放第二个手指
     */
    private void saveScaleContext(float x1, float y1, float x2, float y2) {
        //记录基础缩放值,其中图片缩放比例按照x方向来计算
        //理论上图片应该是等比的,x和y方向比例相同
        //但是有可能外部设定了不规范的值.
        //但是后续的scale操作会将xy不等的缩放值纠正,改成和x方向相同
        mScaleBase = MathUtils.getMatrixScale(mOuterMatrix)[0] / MathUtils.getDistance(x1, y1, x2, y2);
        //两手指的中点在屏幕上落在了图片的某个点上,图片上的这个点在经过总矩阵变换后和手指中点相同
        //现在我们需要得到图片上这个点在图片是fit center状态下在屏幕上的位置
        //因为后续的计算都是基于图片是fit center状态下进行变换
        //所以需要把两手指中点除以外层变换矩阵得到mScaleCenter
        float[] center = MathUtils.inverseMatrixPoint(MathUtils.getCenterPoint(x1, y1, x2, y2), mOuterMatrix);
        mScaleCenter.set(center[0], center[1]);
    }

    /**
     * 对图片按照一些手势信息进行缩放
     *
     * @param scaleCenter mScaleCenter
     * @param scaleBase mScaleBase
     * @param distance 手指两点之间距离
     * @param lineCenter 手指两点之间中点
     *
     * @see #mScaleCenter
     * @see #mScaleBase
     */
    private void scale(PointF scaleCenter, float scaleBase, float distance, PointF lineCenter) {
        if (!isReady()) {
            return;
        }
        //计算图片从fit center状态到目标状态的缩放比例
        float scale = scaleBase * distance;
        Matrix matrix = MathUtils.matrixTake();
        //按照图片缩放中心缩放,并且让缩放中心在缩放点中点上
        matrix.postScale(scale, scale, scaleCenter.x, scaleCenter.y);
        //让图片的缩放中点跟随手指缩放中点
        matrix.postTranslate(lineCenter.x - scaleCenter.x, lineCenter.y - scaleCenter.y);
        //应用变换
        mOuterMatrix.set(matrix);
        MathUtils.matrixGiven(matrix);
        dispatchOuterMatrixChanged();
        //重绘
        invalidate();
    }

    /**
     * 双击后放大或者缩小
     *
     * 将图片缩放比例缩放到nextScale指定的值.
     * 但nextScale值不能大于最大缩放值不能小于fit center情况下的缩放值.
     * 将双击的点尽量移动到控件中心.
     *
     * @param x 双击的点
     * @param y 双击的点
     *
     * @see #calculateNextScale(float, float)
     * @see #getMaxScale()
     */
    private void doubleTap(float x, float y) {
        if (!isReady()) {
            return;
        }
        //获取第一层变换矩阵
        Matrix innerMatrix = MathUtils.matrixTake();
        getInnerMatrix(innerMatrix);
        //当前总的缩放比例
        float innerScale = MathUtils.getMatrixScale(innerMatrix)[0];
        float outerScale = MathUtils.getMatrixScale(mOuterMatrix)[0];
        float currentScale = innerScale * outerScale;
        //控件大小
        float displayWidth = getWidth();
        float displayHeight = getHeight();
        //最大放大大小
        float maxScale = getMaxScale();
        //接下来要放大的大小
        float nextScale = calculateNextScale(innerScale, outerScale);
        //如果接下来放大大于最大值或者小于fit center值,则取边界
        if (nextScale > maxScale) {
            nextScale = maxScale;
        }
        if (nextScale < innerScale) {
            nextScale = innerScale;
        }
        //开始计算缩放动画的结果矩阵
        Matrix animEnd = MathUtils.matrixTake(mOuterMatrix);
        //计算还需缩放的倍数
        animEnd.postScale(nextScale / currentScale, nextScale / currentScale, x, y);
        //将放大点移动到控件中心
        animEnd.postTranslate(displayWidth / 2f - x, displayHeight / 2f - y);
        //得到放大之后的图片方框
        Matrix testMatrix = MathUtils.matrixTake(innerMatrix);
        testMatrix.postConcat(animEnd);
        RectF testBound = MathUtils.rectFTake(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
        testMatrix.mapRect(testBound);
        //修正位置
        float postX = 0;
        float postY = 0;
        if (testBound.right - testBound.left < displayWidth) {
            postX = displayWidth / 2f - (testBound.right + testBound.left) / 2f;
        } else if (testBound.left > 0) {
            postX = -testBound.left;
        } else if (testBound.right < displayWidth) {
            postX = displayWidth - testBound.right;
        }
        if (testBound.bottom - testBound.top < displayHeight) {
            postY = displayHeight / 2f - (testBound.bottom + testBound.top) / 2f;
        } else if (testBound.top > 0) {
            postY = -testBound.top;
        } else if (testBound.bottom < displayHeight) {
            postY = displayHeight - testBound.bottom;
        }
        //应用修正位置
        animEnd.postTranslate(postX, postY);
        //清理当前可能正在执行的动画
        cancelAllAnimator();
        //启动矩阵动画
        mScaleAnimator = new ScaleAnimator(mOuterMatrix, animEnd);
        mScaleAnimator.start();
        //清理临时变量
        MathUtils.rectFGiven(testBound);
        MathUtils.matrixGiven(testMatrix);
        MathUtils.matrixGiven(animEnd);
        MathUtils.matrixGiven(innerMatrix);
    }

    /**
     * 当缩放操作结束动画
     *
     * 如果图片超过边界,找到最近的位置动画恢复.
     * 如果图片缩放尺寸超过最大值或者最小值,找到最近的值动画恢复.
     */
    private void scaleEnd() {
        if (!isReady()) {
            return;
        }
        //是否修正了位置
        boolean change = false;
        //获取图片整体的变换矩阵
        Matrix currentMatrix = MathUtils.matrixTake();
        getCurrentImageMatrix(currentMatrix);
        //整体缩放比例
        float currentScale = MathUtils.getMatrixScale(currentMatrix)[0];
        //第二层缩放比例
        float outerScale = MathUtils.getMatrixScale(mOuterMatrix)[0];
        //控件大小
        float displayWidth = getWidth();
        float displayHeight = getHeight();
        //最大缩放比例
        float maxScale = getMaxScale();
        //比例修正
        float scalePost = 1f;
        //位置修正
        float postX = 0;
        float postY = 0;
        //如果整体缩放比例大于最大比例,进行缩放修正
        if (currentScale > maxScale) {
            scalePost = maxScale / currentScale;
        }
        //如果缩放修正后整体导致第二层缩放小于1(就是图片比fit center状态还小),重新修正缩放
        if (outerScale * scalePost < 1f) {
            scalePost = 1f / outerScale;
        }
        //如果缩放修正不为1,说明进行了修正
        if (scalePost != 1f) {
            change = true;
        }
        //尝试根据缩放点进行缩放修正
        Matrix testMatrix = MathUtils.matrixTake(currentMatrix);
        testMatrix.postScale(scalePost, scalePost, mLastMovePoint.x, mLastMovePoint.y);
        RectF testBound = MathUtils.rectFTake(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
        //获取缩放修正后的图片方框
        testMatrix.mapRect(testBound);
        //检测缩放修正后位置有无超出,如果超出进行位置修正
        if (testBound.right - testBound.left < displayWidth) {
            postX = displayWidth / 2f - (testBound.right + testBound.left) / 2f;
        } else if (testBound.left > 0) {
            postX = -testBound.left;
        } else if (testBound.right < displayWidth) {
            postX = displayWidth - testBound.right;
        }
        if (testBound.bottom - testBound.top < displayHeight) {
            postY = displayHeight / 2f - (testBound.bottom + testBound.top) / 2f;
        } else if (testBound.top > 0) {
            postY = -testBound.top;
        } else if (testBound.bottom < displayHeight) {
            postY = displayHeight - testBound.bottom;
        }
        //如果位置修正不为0,说明进行了修正
        if (postX != 0 || postY != 0) {
            change = true;
        }
        //只有有执行修正才执行动画
        if (change) {
            //计算结束矩阵
            Matrix animEnd = MathUtils.matrixTake(mOuterMatrix);
            animEnd.postScale(scalePost, scalePost, mLastMovePoint.x, mLastMovePoint.y);
            animEnd.postTranslate(postX, postY);
            //清理当前可能正在执行的动画
            cancelAllAnimator();
            //启动矩阵动画
            mScaleAnimator = new ScaleAnimator(mOuterMatrix, animEnd);
            mScaleAnimator.start();
            //清理临时变量
            MathUtils.matrixGiven(animEnd);
        }
        //清理临时变量
        MathUtils.rectFGiven(testBound);
        MathUtils.matrixGiven(testMatrix);
        MathUtils.matrixGiven(currentMatrix);
    }

    /**
     * 执行惯性动画
     *
     * 动画在遇到不能移动就停止.
     * 动画速度衰减到很小就停止.
     *
     * 其中参数速度单位为 像素/秒
     *
     * @param vx x方向速度
     * @param vy y方向速度
     */
    private void fling(float vx, float vy) {
        if (!isReady()) {
            return;
        }
        //清理当前可能正在执行的动画
        cancelAllAnimator();
        //创建惯性动画
        //FlingAnimator单位为 像素/帧,一秒60帧
        mFlingAnimator = new FlingAnimator(vx / 60f, vy / 60f);
        mFlingAnimator.start();
    }

    /**
     * 停止所有手势动画
     */
    private void cancelAllAnimator() {
        if (mScaleAnimator != null) {
            mScaleAnimator.cancel();
            mScaleAnimator = null;
        }
        if (mFlingAnimator != null) {
            mFlingAnimator.cancel();
            mFlingAnimator = null;
        }
    }

    /**
     * 惯性动画
     *
     * 速度逐渐衰减,每帧速度衰减为原来的FLING_DAMPING_FACTOR,当速度衰减到小于1时停止.
     * 当图片不能移动时,动画停止.
     */
    private class FlingAnimator extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener {

        /**
         * 速度向量
         */
        private float[] mVector;

        /**
         * 创建惯性动画
         *
         * 参数单位为 像素/帧
         *
         * @param vectorX 速度向量
         * @param vectorY 速度向量
         */
        public FlingAnimator(float vectorX, float vectorY) {
            super();
            setFloatValues(0, 1f);
            setDuration(1000000);
            addUpdateListener(this);
            mVector = new float[]{vectorX, vectorY};
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            //移动图像并给出结果
            boolean result = scrollBy(mVector[0], mVector[1]);
            //衰减速度
            mVector[0] *= FLING_DAMPING_FACTOR;
            mVector[1] *= FLING_DAMPING_FACTOR;
            //速度太小或者不能移动了就结束
            if (!result || MathUtils.getDistance(0, 0, mVector[0], mVector[1]) < 1f) {
                animation.cancel();
            }
        }
    }

    /**
     * 缩放动画
     *
     * 在给定时间内从一个矩阵的变化逐渐动画到另一个矩阵的变化
     */
    private class ScaleAnimator extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener {

        /**
         * 开始矩阵
         */
        private float[] mStart = new float[9];

        /**
         * 结束矩阵
         */
        private float[] mEnd = new float[9];

        /**
         * 中间结果矩阵
         */
        private float[] mResult = new float[9];

        /**
         * 构建一个缩放动画
         *
         * 从一个矩阵变换到另外一个矩阵
         *
         * @param start 开始矩阵
         * @param end 结束矩阵
         */
        public ScaleAnimator(Matrix start, Matrix end) {
            this(start, end, SCALE_ANIMATOR_DURATION);
        }

        /**
         * 构建一个缩放动画
         *
         * 从一个矩阵变换到另外一个矩阵
         *
         * @param start 开始矩阵
         * @param end 结束矩阵
         * @param duration 动画时间
         */
        public ScaleAnimator(Matrix start, Matrix end, long duration) {
            super();
            setFloatValues(0, 1f);
            setDuration(duration);
            addUpdateListener(this);
            start.getValues(mStart);
            end.getValues(mEnd);
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            //获取动画进度
            float value = (Float) animation.getAnimatedValue();
            //根据动画进度计算矩阵中间插值
            for (int i = 0; i < 9; i++) {
                mResult[i] = mStart[i] + (mEnd[i] - mStart[i]) * value;
            }
            //设置矩阵并重绘
            mOuterMatrix.setValues(mResult);
            dispatchOuterMatrixChanged();
            invalidate();
        }
    }


    防止内存抖动复用对象

    /**
     * 对象池
     *
     * 防止频繁new对象产生内存抖动.
     * 由于对象池最大长度限制,如果吞度量超过对象池容量,仍然会发生抖动.
     * 此时需要增大对象池容量,但是会占用更多内存.
     *
     * @param <T> 对象池容纳的对象类型
     */
    private static abstract class ObjectsPool<T> {

        /**
         * 对象池的最大容量
         */
        private int mSize;

        /**
         * 对象池队列
         */
        private Queue<T> mQueue;

        /**
         * 创建一个对象池
         *
         * @param size 对象池最大容量
         */
        public ObjectsPool(int size) {
            mSize = size;
            mQueue = new LinkedList<T>();
        }

        /**
         * 获取一个空闲的对象
         *
         * 如果对象池为空,则对象池自己会new一个返回.
         * 如果对象池内有对象,则取一个已存在的返回.
         * take出来的对象用完要记得调用given归还.
         * 如果不归还,让然会发生内存抖动,但不会引起泄漏.
         *
         * @return 可用的对象
         *
         * @see #given(Object)
         */
        public T take() {
            //如果池内为空就创建一个
            if (mQueue.size() == 0) {
                return newInstance();
            } else {
                //对象池里有就从顶端拿出来一个返回
                return resetInstance(mQueue.poll());
            }
        }

        /**
         * 归还对象池内申请的对象
         *
         * 如果归还的对象数量超过对象池容量,那么归还的对象就会被丢弃.
         *
         * @param obj 归还的对象
         *
         * @see #take()
         */
        public void given(T obj) {
            //如果对象池还有空位子就归还对象
            if (obj != null && mQueue.size() < mSize) {
                mQueue.offer(obj);
            }
        }

        /**
         * 实例化对象
         *
         * @return 创建的对象
         */
        abstract protected T newInstance();

        /**
         * 重置对象
         *
         * 把对象数据清空到就像刚创建的一样.
         *
         * @param obj 需要被重置的对象
         * @return 被重置之后的对象
         */
        abstract protected T resetInstance(T obj);
    }

    /**
     * 矩阵对象池
     */
    private static class MatrixPool extends ObjectsPool<Matrix> {

        public MatrixPool(int size) {
            super(size);
        }

        @Override
        protected Matrix newInstance() {
            return new Matrix();
        }

        @Override
        protected Matrix resetInstance(Matrix obj) {
            obj.reset();
            return obj;
        }
    }

    /**
     * 矩形对象池
     */
    private static class RectFPool extends ObjectsPool<RectF> {

        public RectFPool(int size) {
            super(size);
        }

        @Override
        protected RectF newInstance() {
            return new RectF();
        }

        @Override
        protected RectF resetInstance(RectF obj) {
            obj.setEmpty();
            return obj;
        }
    }


    数学计算工具类

    /**
     * 数学计算工具类
     */
    public static class MathUtils {

        /**
         * 矩阵对象池
         */
        private static MatrixPool mMatrixPool = new MatrixPool(16);

        /**
         * 获取矩阵对象
         */
        public static Matrix matrixTake() {
            return mMatrixPool.take();
        }

        /**
         * 获取某个矩阵的copy
         */
        public static Matrix matrixTake(Matrix matrix) {
            Matrix result = mMatrixPool.take();
            if (matrix != null) {
                result.set(matrix);
            }
            return result;
        }

        /**
         * 归还矩阵对象
         */
        public static void matrixGiven(Matrix matrix) {
            mMatrixPool.given(matrix);
        }

        /**
         * 矩形对象池
         */
        private static RectFPool mRectFPool = new RectFPool(16);

        /**
         * 获取矩形对象
         */
        public static RectF rectFTake() {
            return mRectFPool.take();
        }

        /**
         * 按照指定值获取矩形对象
         */
        public static RectF rectFTake(float left, float top, float right, float bottom) {
            RectF result = mRectFPool.take();
            result.set(left, top, right, bottom);
            return result;
        }

        /**
         * 获取某个矩形的副本
         */
        public static RectF rectFTake(RectF rectF) {
            RectF result = mRectFPool.take();
            if (rectF != null) {
                result.set(rectF);
            }
            return result;
        }

        /**
         * 归还矩形对象
         */
        public static void rectFGiven(RectF rectF) {
            mRectFPool.given(rectF);
        }

        /**
         * 获取两点之间距离
         *
         * @param x1 点1
         * @param y1 点1
         * @param x2 点2
         * @param y2 点2
         * @return 距离
         */
        public static float getDistance(float x1, float y1, float x2, float y2) {
            float x = x1 - x2;
            float y = y1 - y2;
            return (float) Math.sqrt(x * x + y * y);
        }

        /**
         * 获取两点的中点
         *
         * @param x1 点1
         * @param y1 点1
         * @param x2 点2
         * @param y2 点2
         * @return float[]{x, y}
         */
        public static float[] getCenterPoint(float x1, float y1, float x2, float y2) {
            return new float[]{(x1 + x2) / 2f, (y1 + y2) / 2f};
        }

        /**
         * 获取矩阵的缩放值
         *
         * @param matrix 要计算的矩阵
         * @return float[]{scaleX, scaleY}
         */
        public static float[] getMatrixScale(Matrix matrix) {
            if (matrix != null) {
                float[] value = new float[9];
                matrix.getValues(value);
                return new float[]{value[0], value[4]};
            } else {
                return new float[2];
            }
        }

        /**
         * 计算点除以矩阵的值
         *
         * matrix.mapPoints(unknownPoint) -> point
         * 已知point和matrix,求unknownPoint的值.
         *
         * @param point
         * @param matrix
         * @return unknownPoint
         */
        public static float[] inverseMatrixPoint(float[] point, Matrix matrix) {
            if (point != null && matrix != null) {
                float[] dst = new float[2];
                //计算matrix的逆矩阵
                Matrix inverse = matrixTake();
                matrix.invert(inverse);
                //用逆矩阵变换point到dst,dst就是结果
                inverse.mapPoints(dst, point);
                //清除临时变量
                matrixGiven(inverse);
                return dst;
            } else {
                return new float[2];
            }
        }

        /**
         * 计算两个矩形之间的变换矩阵
         *
         * unknownMatrix.mapRect(to, from)
         * 已知from矩形和to矩形,求unknownMatrix
         *
         * @param from
         * @param to
         * @param result unknownMatrix
         */
        public static void calculateRectTranslateMatrix(RectF from, RectF to, Matrix result) {
            if (from == null || to == null || result == null) {
                return;
            }
            if (from.width() == 0 || from.height() == 0) {
                return;
            }
            result.reset();
            result.postTranslate(-from.left, -from.top);
            result.postScale(to.width() / from.width(), to.height() / from.height());
            result.postTranslate(to.left, to.top);
        }

        /**
         * 计算图片在某个ImageView中的显示矩形
         *
         * @param container ImageView的Rect
         * @param srcWidth 图片的宽度
         * @param srcHeight 图片的高度
         * @param scaleType 图片在ImageView中的ScaleType
         * @param result 图片应该在ImageView中展示的矩形
         */
        public static void calculateScaledRectInContainer(RectF container, float srcWidth, float srcHeight, ScaleType scaleType, RectF result) {
            if (container == null || result == null) {
                return;
            }
            if (srcWidth == 0 || srcHeight == 0) {
                return;
            }
            //默认scaleType为fit center
            if (scaleType == null) {
                scaleType = ScaleType.FIT_CENTER;
            }
            result.setEmpty();
            if (ScaleType.FIT_XY.equals(scaleType)) {
                result.set(container);
            } else if (ScaleType.CENTER.equals(scaleType)) {
                Matrix matrix = matrixTake();
                RectF rect = rectFTake(0, 0, srcWidth, srcHeight);
                matrix.setTranslate((container.width() - srcWidth) * 0.5f, (container.height() - srcHeight) * 0.5f);
                matrix.mapRect(result, rect);
                rectFGiven(rect);
                matrixGiven(matrix);
                result.left += container.left;
                result.right += container.left;
                result.top += container.top;
                result.bottom += container.top;
            } else if (ScaleType.CENTER_CROP.equals(scaleType)) {
                Matrix matrix = matrixTake();
                RectF rect = rectFTake(0, 0, srcWidth, srcHeight);
                float scale;
                float dx = 0;
                float dy = 0;
                if (srcWidth * container.height() > container.width() * srcHeight) {
                    scale = container.height() / srcHeight;
                    dx = (container.width() - srcWidth * scale) * 0.5f;
                } else {
                    scale = container.width() / srcWidth;
                    dy = (container.height() - srcHeight * scale) * 0.5f;
                }
                matrix.setScale(scale, scale);
                matrix.postTranslate(dx, dy);
                matrix.mapRect(result, rect);
                rectFGiven(rect);
                matrixGiven(matrix);
                result.left += container.left;
                result.right += container.left;
                result.top += container.top;
                result.bottom += container.top;
            } else if (ScaleType.CENTER_INSIDE.equals(scaleType)) {
                Matrix matrix = matrixTake();
                RectF rect = rectFTake(0, 0, srcWidth, srcHeight);
                float scale;
                float dx;
                float dy;
                if (srcWidth <= container.width() && srcHeight <= container.height()) {
                    scale = 1f;
                } else {
                    scale = Math.min(container.width() / srcWidth, container.height() / srcHeight);
                }
                dx = (container.width() - srcWidth * scale) * 0.5f;
                dy = (container.height() - srcHeight * scale) * 0.5f;
                matrix.setScale(scale, scale);
                matrix.postTranslate(dx, dy);
                matrix.mapRect(result, rect);
                rectFGiven(rect);
                matrixGiven(matrix);
                result.left += container.left;
                result.right += container.left;
                result.top += container.top;
                result.bottom += container.top;
            } else if (ScaleType.FIT_CENTER.equals(scaleType)) {
                Matrix matrix = matrixTake();
                RectF rect = rectFTake(0, 0, srcWidth, srcHeight);
                RectF tempSrc = rectFTake(0, 0, srcWidth, srcHeight);
                RectF tempDst = rectFTake(0, 0, container.width(), container.height());
                matrix.setRectToRect(tempSrc, tempDst, Matrix.ScaleToFit.CENTER);
                matrix.mapRect(result, rect);
                rectFGiven(tempDst);
                rectFGiven(tempSrc);
                rectFGiven(rect);
                matrixGiven(matrix);
                result.left += container.left;
                result.right += container.left;
                result.top += container.top;
                result.bottom += container.top;
            } else if (ScaleType.FIT_START.equals(scaleType)) {
                Matrix matrix = matrixTake();
                RectF rect = rectFTake(0, 0, srcWidth, srcHeight);
                RectF tempSrc = rectFTake(0, 0, srcWidth, srcHeight);
                RectF tempDst = rectFTake(0, 0, container.width(), container.height());
                matrix.setRectToRect(tempSrc, tempDst, Matrix.ScaleToFit.START);
                matrix.mapRect(result, rect);
                rectFGiven(tempDst);
                rectFGiven(tempSrc);
                rectFGiven(rect);
                matrixGiven(matrix);
                result.left += container.left;
                result.right += container.left;
                result.top += container.top;
                result.bottom += container.top;
            } else if (ScaleType.FIT_END.equals(scaleType)) {
                Matrix matrix = matrixTake();
                RectF rect = rectFTake(0, 0, srcWidth, srcHeight);
                RectF tempSrc = rectFTake(0, 0, srcWidth, srcHeight);
                RectF tempDst = rectFTake(0, 0, container.width(), container.height());
                matrix.setRectToRect(tempSrc, tempDst, Matrix.ScaleToFit.END);
                matrix.mapRect(result, rect);
                rectFGiven(tempDst);
                rectFGiven(tempSrc);
                rectFGiven(rect);
                matrixGiven(matrix);
                result.left += container.left;
                result.right += container.left;
                result.top += container.top;
                result.bottom += container.top;
            } else {
                result.set(container);
            }
        }
    }
}

好了本章的内容介绍到此结束,转载整理一是方便自己日后查找,另一方面也能让更多的人利用这些开源的东西,如果转载内容有妨害博主的地方请联系我删除。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值