Skip to content

Commit

Permalink
FragmentContainerHelper支持duration和插值器
Browse files Browse the repository at this point in the history
  • Loading branch information
hackware1993 committed Sep 4, 2016
1 parent 9433d9c commit ceacf87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Simple steps, you can integrate **MagicIndicator**:
}
```

3. add **magicindicator** to your layout xml:
3. add **MagicIndicator** to your layout xml:

```
<?xml version="1.0" encoding="utf-8"?>
Expand All @@ -60,7 +60,7 @@ Simple steps, you can integrate **MagicIndicator**:
</LinearLayout>
```

4. find **magicindicator** through code, initialize it:
4. find **MagicIndicator** through code, initialize it:

```
MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.lucode.hackware.magicindicator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.view.animation.Interpolator;


/**
Expand All @@ -10,19 +12,21 @@
* Created by hackware on 2016/9/4.
*/

public class FragmentContainerHelper implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
public class FragmentContainerHelper extends AnimatorListenerAdapter implements ValueAnimator.AnimatorUpdateListener {
private MagicIndicator mMagicIndicator;
private ValueAnimator mScrollAnimator;
private int mDuration = 200;
private Interpolator mInterpolator;

public FragmentContainerHelper(MagicIndicator magicIndicator) {
mMagicIndicator = magicIndicator;
}

public void handlePageSelected(int selectedIndex) {
if (mScrollAnimator == null || !mScrollAnimator.isRunning()) {
mMagicIndicator.onPageScrollStateChanged(ScrollState.SCROLL_STATE_SETTLING);
dispatchPageScrollStateChanged(ScrollState.SCROLL_STATE_SETTLING);
}
mMagicIndicator.onPageSelected(selectedIndex);
dispatchPageSelected(selectedIndex);
float currentPositionOffsetSum = 0.0f; // position = 0, positionOffset = 0.0f
if (mScrollAnimator != null) {
currentPositionOffsetSum = (Float) mScrollAnimator.getAnimatedValue();
Expand All @@ -33,39 +37,41 @@ public void handlePageSelected(int selectedIndex) {
mScrollAnimator.setFloatValues(currentPositionOffsetSum, selectedIndex); // position = selectedIndex, positionOffset = 0.0f
mScrollAnimator.addUpdateListener(this);
mScrollAnimator.addListener(this);
mScrollAnimator.setInterpolator(mInterpolator);
mScrollAnimator.setDuration(mDuration);
mScrollAnimator.start();
}

public MagicIndicator getMagicIndicator() {
return mMagicIndicator;
public void setDuration(int duration) {
mDuration = duration;
}

public void setMagicIndicator(MagicIndicator magicIndicator) {
mMagicIndicator = magicIndicator;
public void setInterpolator(Interpolator interpolator) {
mInterpolator = interpolator;
}

@Override
public void onAnimationUpdate(ValueAnimator animation) {
float positionOffsetSum = (Float) animation.getAnimatedValue();
int position = (int) positionOffsetSum;
float positionOffset = positionOffsetSum - position;
mMagicIndicator.onPageScrolled(position, positionOffset, 0);
private void dispatchPageSelected(int pageIndex) {
mMagicIndicator.onPageSelected(pageIndex);
}

@Override
public void onAnimationStart(Animator animation) {
private void dispatchPageScrollStateChanged(int state) {
mMagicIndicator.onPageScrollStateChanged(state);
}

@Override
public void onAnimationEnd(Animator animation) {
mMagicIndicator.onPageScrollStateChanged(ScrollState.SCROLL_STATE_IDLE);
private void dispatchPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
mMagicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
}

@Override
public void onAnimationCancel(Animator animation) {
public void onAnimationUpdate(ValueAnimator animation) {
float positionOffsetSum = (Float) animation.getAnimatedValue();
int position = (int) positionOffsetSum;
float positionOffset = positionOffsetSum - position;
dispatchPageScrolled(position, positionOffset, 0);
}

@Override
public void onAnimationRepeat(Animator animation) {
public void onAnimationEnd(Animator animation) {
mMagicIndicator.onPageScrollStateChanged(ScrollState.SCROLL_STATE_IDLE);
}
}

0 comments on commit ceacf87

Please sign in to comment.