|
| 1 | +package net.lucode.hackware.magicindicatordemo.ext.indicators; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Canvas; |
| 5 | +import android.graphics.Rect; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.view.View; |
| 8 | +import android.view.animation.Interpolator; |
| 9 | +import android.view.animation.LinearInterpolator; |
| 10 | + |
| 11 | +import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; |
| 12 | +import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/** |
| 17 | + * 通用的indicator,支持外面设置Drawable |
| 18 | + * Created by hackware on 2016/11/14. |
| 19 | + */ |
| 20 | + |
| 21 | +public class CommonPagerIndicator extends View implements IPagerIndicator { |
| 22 | + public static final int MODE_MATCH_EDGE = 0; // drawable宽度 == title宽度 - 2 * mXOffset |
| 23 | + public static final int MODE_WRAP_CONTENT = 1; // drawable宽度 == title内容宽度 - 2 * mXOffset |
| 24 | + public static final int MODE_EXACTLY = 2; |
| 25 | + |
| 26 | + private int mMode; // 默认为MODE_MATCH_EDGE模式 |
| 27 | + private Drawable mIndicatorDrawable; |
| 28 | + |
| 29 | + // 控制动画 |
| 30 | + private Interpolator mStartInterpolator = new LinearInterpolator(); |
| 31 | + private Interpolator mEndInterpolator = new LinearInterpolator(); |
| 32 | + |
| 33 | + private float mDrawableHeight; |
| 34 | + private float mDrawableWidth; |
| 35 | + private float mYOffset; |
| 36 | + private float mXOffset; |
| 37 | + |
| 38 | + private List<PositionData> mPositionDataList; |
| 39 | + private Rect mDrawableRect = new Rect(); |
| 40 | + |
| 41 | + public CommonPagerIndicator(Context context) { |
| 42 | + super(context); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { |
| 47 | + if (mIndicatorDrawable == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (mPositionDataList == null || mPositionDataList.isEmpty()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // 计算锚点位置 |
| 56 | + int currentPosition = Math.min(mPositionDataList.size() - 1, position); |
| 57 | + int nextPosition = Math.min(mPositionDataList.size() - 1, position + 1); |
| 58 | + PositionData current = mPositionDataList.get(currentPosition); |
| 59 | + PositionData next = mPositionDataList.get(nextPosition); |
| 60 | + |
| 61 | + float leftX; |
| 62 | + float nextLeftX; |
| 63 | + float rightX; |
| 64 | + float nextRightX; |
| 65 | + if (mMode == MODE_MATCH_EDGE) { |
| 66 | + leftX = current.mLeft + mXOffset; |
| 67 | + nextLeftX = next.mLeft + mXOffset; |
| 68 | + rightX = current.mRight - mXOffset; |
| 69 | + nextRightX = next.mRight - mXOffset; |
| 70 | + mDrawableRect.top = (int) mYOffset; |
| 71 | + mDrawableRect.bottom = (int) (getHeight() - mYOffset); |
| 72 | + } else if (mMode == MODE_WRAP_CONTENT) { |
| 73 | + leftX = current.mContentLeft + mXOffset; |
| 74 | + nextLeftX = next.mContentLeft + mXOffset; |
| 75 | + rightX = current.mContentRight - mXOffset; |
| 76 | + nextRightX = next.mContentRight - mXOffset; |
| 77 | + mDrawableRect.top = (int) (current.mContentTop - mYOffset); |
| 78 | + mDrawableRect.bottom = (int) (current.mContentBottom + mYOffset); |
| 79 | + } else { // MODE_EXACTLY |
| 80 | + leftX = current.mLeft + (current.width() - mDrawableWidth) / 2; |
| 81 | + nextLeftX = next.mLeft + (next.width() - mDrawableWidth) / 2; |
| 82 | + rightX = current.mLeft + (current.width() + mDrawableWidth) / 2; |
| 83 | + nextRightX = next.mLeft + (next.width() + mDrawableWidth) / 2; |
| 84 | + mDrawableRect.top = (int) (getHeight() - mDrawableHeight - mYOffset); |
| 85 | + mDrawableRect.bottom = (int) (getHeight() - mYOffset); |
| 86 | + } |
| 87 | + |
| 88 | + mDrawableRect.left = (int) (leftX + (nextLeftX - leftX) * mStartInterpolator.getInterpolation(positionOffset)); |
| 89 | + mDrawableRect.right = (int) (rightX + (nextRightX - rightX) * mEndInterpolator.getInterpolation(positionOffset)); |
| 90 | + mIndicatorDrawable.setBounds(mDrawableRect); |
| 91 | + |
| 92 | + invalidate(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onPageSelected(int position) { |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onPageScrollStateChanged(int state) { |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void onDraw(Canvas canvas) { |
| 105 | + if (mIndicatorDrawable != null) { |
| 106 | + mIndicatorDrawable.draw(canvas); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void onPositionDataProvide(List<PositionData> dataList) { |
| 112 | + mPositionDataList = dataList; |
| 113 | + } |
| 114 | + |
| 115 | + public Drawable getIndicatorDrawable() { |
| 116 | + return mIndicatorDrawable; |
| 117 | + } |
| 118 | + |
| 119 | + public void setIndicatorDrawable(Drawable indicatorDrawable) { |
| 120 | + mIndicatorDrawable = indicatorDrawable; |
| 121 | + } |
| 122 | + |
| 123 | + public Interpolator getStartInterpolator() { |
| 124 | + return mStartInterpolator; |
| 125 | + } |
| 126 | + |
| 127 | + public void setStartInterpolator(Interpolator startInterpolator) { |
| 128 | + mStartInterpolator = startInterpolator; |
| 129 | + } |
| 130 | + |
| 131 | + public Interpolator getEndInterpolator() { |
| 132 | + return mEndInterpolator; |
| 133 | + } |
| 134 | + |
| 135 | + public void setEndInterpolator(Interpolator endInterpolator) { |
| 136 | + mEndInterpolator = endInterpolator; |
| 137 | + } |
| 138 | + |
| 139 | + public int getMode() { |
| 140 | + return mMode; |
| 141 | + } |
| 142 | + |
| 143 | + public void setMode(int mode) { |
| 144 | + if (mode == MODE_EXACTLY || mode == MODE_MATCH_EDGE || mode == MODE_WRAP_CONTENT) { |
| 145 | + mMode = mode; |
| 146 | + } else { |
| 147 | + throw new IllegalArgumentException("mode " + mode + " not supported."); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public float getDrawableHeight() { |
| 152 | + return mDrawableHeight; |
| 153 | + } |
| 154 | + |
| 155 | + public void setDrawableHeight(float drawableHeight) { |
| 156 | + mDrawableHeight = drawableHeight; |
| 157 | + } |
| 158 | + |
| 159 | + public float getDrawableWidth() { |
| 160 | + return mDrawableWidth; |
| 161 | + } |
| 162 | + |
| 163 | + public void setDrawableWidth(float drawableWidth) { |
| 164 | + mDrawableWidth = drawableWidth; |
| 165 | + } |
| 166 | + |
| 167 | + public float getYOffset() { |
| 168 | + return mYOffset; |
| 169 | + } |
| 170 | + |
| 171 | + public void setYOffset(float yOffset) { |
| 172 | + mYOffset = yOffset; |
| 173 | + } |
| 174 | + |
| 175 | + public float getXOffset() { |
| 176 | + return mXOffset; |
| 177 | + } |
| 178 | + |
| 179 | + public void setXOffset(float xOffset) { |
| 180 | + mXOffset = xOffset; |
| 181 | + } |
| 182 | +} |
0 commit comments