From 06dd55995ec35639b569901229af9d578c8b247a Mon Sep 17 00:00:00 2001 From: hackware1993 Date: Sat, 26 Nov 2016 10:11:26 +0800 Subject: [PATCH] =?UTF-8?q?FragmentContainerHelper=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=94=A8Interpolator=E8=AE=BE=E7=BD=AE=E5=BC=B9=E6=80=A7?= =?UTF-8?q?=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ext/indicators/CommonPagerIndicator.java | 7 ++-- .../FragmentContainerHelper.java | 38 +++++++++++++++++++ .../magicindicator/NavigatorHelper.java | 8 ++-- .../indicators/BezierPagerIndicator.java | 11 +++--- .../indicators/LinePagerIndicator.java | 11 +++--- .../indicators/TestPagerIndicator.java | 7 ++-- .../indicators/TriangularPagerIndicator.java | 7 ++-- .../indicators/WrapPagerIndicator.java | 7 ++-- 8 files changed, 64 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/net/lucode/hackware/magicindicatordemo/ext/indicators/CommonPagerIndicator.java b/app/src/main/java/net/lucode/hackware/magicindicatordemo/ext/indicators/CommonPagerIndicator.java index fc0298fd..8e1385c2 100644 --- a/app/src/main/java/net/lucode/hackware/magicindicatordemo/ext/indicators/CommonPagerIndicator.java +++ b/app/src/main/java/net/lucode/hackware/magicindicatordemo/ext/indicators/CommonPagerIndicator.java @@ -8,6 +8,7 @@ import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; +import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData; @@ -53,10 +54,8 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } // 计算锚点位置 - int currentPosition = Math.min(mPositionDataList.size() - 1, position); - int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); - PositionData current = mPositionDataList.get(currentPosition); - PositionData next = mPositionDataList.get(nextPosition); + PositionData current = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position); + PositionData next = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position + 1); float leftX; float nextLeftX; diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/FragmentContainerHelper.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/FragmentContainerHelper.java index e6682000..29eb7418 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/FragmentContainerHelper.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/FragmentContainerHelper.java @@ -3,9 +3,13 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; +import android.annotation.TargetApi; +import android.os.Build; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Interpolator; +import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData; + import java.util.ArrayList; import java.util.List; @@ -15,6 +19,7 @@ * Created by hackware on 2016/9/4. */ +@TargetApi(Build.VERSION_CODES.HONEYCOMB) public class FragmentContainerHelper { private List mMagicIndicators = new ArrayList(); private ValueAnimator mScrollAnimator; @@ -51,6 +56,39 @@ public FragmentContainerHelper(MagicIndicator magicIndicator) { mMagicIndicators.add(magicIndicator); } + /** + * IPagerIndicator支持弹性效果的辅助方法 + * + * @param positionDataList + * @param index + * @return + */ + public static PositionData getImitativePositionData(List positionDataList, int index) { + if (index >= 0 && index <= positionDataList.size() - 1) { // 越界后,返回假的PositionData + return positionDataList.get(index); + } else { + PositionData result = new PositionData(); + PositionData referenceData; + int offset; + if (index < 0) { + offset = index; + referenceData = positionDataList.get(0); + } else { + offset = index - positionDataList.size() + 1; + referenceData = positionDataList.get(positionDataList.size() - 1); + } + result.mLeft = referenceData.mLeft + offset * referenceData.width(); + result.mTop = referenceData.mTop; + result.mRight = referenceData.mRight + offset * referenceData.width(); + result.mBottom = referenceData.mBottom; + result.mContentLeft = referenceData.mContentLeft + offset * referenceData.width(); + result.mContentTop = referenceData.mContentTop; + result.mContentRight = referenceData.mContentRight + offset * referenceData.width(); + result.mContentBottom = referenceData.mContentBottom; + return result; + } + } + public void handlePageSelected(int selectedIndex) { handlePageSelected(selectedIndex, true); } diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/NavigatorHelper.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/NavigatorHelper.java index 2db493fe..68859a16 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/NavigatorHelper.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/NavigatorHelper.java @@ -140,16 +140,16 @@ public void setSkimOver(boolean skimOver) { mSkimOver = skimOver; } + public int getTotalCount() { + return mTotalCount; + } + public void setTotalCount(int totalCount) { mTotalCount = totalCount; mDeselectedItems.clear(); mLeavedPercents.clear(); } - public int getTotalCount() { - return mTotalCount; - } - public int getCurrentIndex() { return mCurrentIndex; } diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/BezierPagerIndicator.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/BezierPagerIndicator.java index 502f10e4..1e679e7a 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/BezierPagerIndicator.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/BezierPagerIndicator.java @@ -9,6 +9,7 @@ import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; +import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.buildins.ArgbEvaluatorHolder; import net.lucode.hackware.magicindicator.buildins.UIUtil; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; @@ -86,17 +87,15 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse // 计算颜色 if (mColors != null && mColors.size() > 0) { - int currentColor = mColors.get(position % mColors.size()); - int nextColor = mColors.get((position + 1) % mColors.size()); + int currentColor = mColors.get(Math.abs(position) % mColors.size()); + int nextColor = mColors.get(Math.abs(position + 1) % mColors.size()); int color = ArgbEvaluatorHolder.eval(positionOffset, currentColor, nextColor); mPaint.setColor(color); } // 计算锚点位置 - int currentPosition = Math.min(mPositionDataList.size() - 1, position); - int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); - PositionData current = mPositionDataList.get(currentPosition); - PositionData next = mPositionDataList.get(nextPosition); + PositionData current = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position); + PositionData next = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position + 1); float leftX = current.mLeft + (current.mRight - current.mLeft) / 2; float rightX = next.mLeft + (next.mRight - next.mLeft) / 2; diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/LinePagerIndicator.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/LinePagerIndicator.java index d105d534..49841948 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/LinePagerIndicator.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/LinePagerIndicator.java @@ -8,6 +8,7 @@ import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; +import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.buildins.ArgbEvaluatorHolder; import net.lucode.hackware.magicindicator.buildins.UIUtil; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; @@ -69,17 +70,15 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse // 计算颜色 if (mColors != null && mColors.size() > 0) { - int currentColor = mColors.get(position % mColors.size()); - int nextColor = mColors.get((position + 1) % mColors.size()); + int currentColor = mColors.get(Math.abs(position) % mColors.size()); + int nextColor = mColors.get(Math.abs(position + 1) % mColors.size()); int color = ArgbEvaluatorHolder.eval(positionOffset, currentColor, nextColor); mPaint.setColor(color); } // 计算锚点位置 - int currentPosition = Math.min(mPositionDataList.size() - 1, position); - int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); - PositionData current = mPositionDataList.get(currentPosition); - PositionData next = mPositionDataList.get(nextPosition); + PositionData current = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position); + PositionData next = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position + 1); float leftX; float nextLeftX; diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TestPagerIndicator.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TestPagerIndicator.java index c6716513..fdfb2605 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TestPagerIndicator.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TestPagerIndicator.java @@ -7,6 +7,7 @@ import android.graphics.RectF; import android.view.View; +import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData; @@ -54,10 +55,8 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } // 计算锚点位置 - int currentPosition = Math.min(mPositionDataList.size() - 1, position); - int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); - PositionData current = mPositionDataList.get(currentPosition); - PositionData next = mPositionDataList.get(nextPosition); + PositionData current = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position); + PositionData next = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position + 1); mOutRect.left = current.mLeft + (next.mLeft - current.mLeft) * positionOffset; mOutRect.top = current.mTop + (next.mTop - current.mTop) * positionOffset; diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TriangularPagerIndicator.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TriangularPagerIndicator.java index 13ef1009..f1ea13c7 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TriangularPagerIndicator.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/TriangularPagerIndicator.java @@ -8,6 +8,7 @@ import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; +import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.buildins.UIUtil; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData; @@ -75,10 +76,8 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } // 计算锚点位置 - int currentPosition = Math.min(mPositionDataList.size() - 1, position); - int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); - PositionData current = mPositionDataList.get(currentPosition); - PositionData next = mPositionDataList.get(nextPosition); + PositionData current = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position); + PositionData next = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position + 1); float leftX = current.mLeft + (current.mRight - current.mLeft) / 2; float rightX = next.mLeft + (next.mRight - next.mLeft) / 2; diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/WrapPagerIndicator.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/WrapPagerIndicator.java index 8f4830d0..806e1bc0 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/WrapPagerIndicator.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/indicators/WrapPagerIndicator.java @@ -8,6 +8,7 @@ import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; +import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.buildins.UIUtil; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData; @@ -59,10 +60,8 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } // 计算锚点位置 - int currentPosition = Math.min(mPositionDataList.size() - 1, position); - int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); - PositionData current = mPositionDataList.get(currentPosition); - PositionData next = mPositionDataList.get(nextPosition); + PositionData current = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position); + PositionData next = FragmentContainerHelper.getImitativePositionData(mPositionDataList, position + 1); mRect.left = current.mContentLeft - mHorizontalPadding + (next.mContentLeft - current.mContentLeft) * mEndInterpolator.getInterpolation(positionOffset); mRect.top = current.mContentTop - mVerticalPadding;