Skip to content

Commit

Permalink
FragmentContainerHelper支持用Interpolator设置弹性效果
Browse files Browse the repository at this point in the history
  • Loading branch information
hackware1993 committed Nov 26, 2016
1 parent 0ed9e28 commit 06dd559
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,6 +19,7 @@
* Created by hackware on 2016/9/4.
*/

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FragmentContainerHelper {
private List<MagicIndicator> mMagicIndicators = new ArrayList<MagicIndicator>();
private ValueAnimator mScrollAnimator;
Expand Down Expand Up @@ -51,6 +56,39 @@ public FragmentContainerHelper(MagicIndicator magicIndicator) {
mMagicIndicators.add(magicIndicator);
}

/**
* IPagerIndicator支持弹性效果的辅助方法
*
* @param positionDataList
* @param index
* @return
*/
public static PositionData getImitativePositionData(List<PositionData> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 06dd559

Please sign in to comment.