|
|
@@ -0,0 +1,136 @@
|
|
|
+package kr.co.zumo.app.lifeplus.view.custom.main.banner;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.res.Resources;
|
|
|
+import android.graphics.Canvas;
|
|
|
+import android.graphics.Paint;
|
|
|
+import android.support.v7.widget.LinearLayoutManager;
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
+import android.view.View;
|
|
|
+
|
|
|
+/**
|
|
|
+ * MainBannerViewItemDotIndicator
|
|
|
+ * <pre>
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author 하세미
|
|
|
+ * @version 1.0
|
|
|
+ * @history 하세미 [2018-11-01] [최초 작성]
|
|
|
+ * @since 2018-11-01
|
|
|
+ */
|
|
|
+public class MainBannerViewItemDotIndicator extends RecyclerView.ItemDecoration {
|
|
|
+
|
|
|
+ private Context context;
|
|
|
+ private int colorActive = 0xFFFFFFFF;
|
|
|
+ private int colorInactive = 0x4DFFFFFF;
|
|
|
+ private static final float DP = Resources.getSystem().getDisplayMetrics().density;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Height of the space the indicator takes up at the bottom of the view.
|
|
|
+ */
|
|
|
+ private final int mIndicatorHeight = (int) (DP * 16);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicator stroke width.
|
|
|
+ */
|
|
|
+ private final float mIndicatorStrokeWidth = DP * 4;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicator width.
|
|
|
+ */
|
|
|
+ private final float mIndicatorItemLength = DP * 4;
|
|
|
+ /**
|
|
|
+ * Padding between indicators.
|
|
|
+ */
|
|
|
+ private final float mIndicatorItemPadding = DP * 8;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Some more natural animation interpolation
|
|
|
+ */
|
|
|
+ //private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
|
|
|
+
|
|
|
+ private final Paint mPaint = new Paint();
|
|
|
+
|
|
|
+ public MainBannerViewItemDotIndicator() {
|
|
|
+ //this.context = context;
|
|
|
+ mPaint.setStrokeCap(Paint.Cap.ROUND);
|
|
|
+ mPaint.setStrokeWidth(mIndicatorStrokeWidth);
|
|
|
+ mPaint.setStyle(Paint.Style.STROKE);
|
|
|
+ mPaint.setAntiAlias(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
|
|
+ super.onDrawOver(c, parent, state);
|
|
|
+
|
|
|
+ int itemCount = parent.getAdapter().getItemCount();
|
|
|
+
|
|
|
+ // center horizontally, calculate width and subtract half from center
|
|
|
+ float totalLength = mIndicatorItemLength * itemCount;
|
|
|
+ float paddingBetweenItems = Math.max(0, itemCount - 1) * mIndicatorItemPadding;
|
|
|
+ float indicatorTotalWidth = totalLength + paddingBetweenItems;
|
|
|
+ float indicatorStartX = (parent.getWidth() - indicatorTotalWidth) / 2F;
|
|
|
+
|
|
|
+ // center vertically in the allotted space
|
|
|
+ float indicatorPosY = parent.getHeight() - mIndicatorHeight / 2F;
|
|
|
+
|
|
|
+ drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount);
|
|
|
+
|
|
|
+
|
|
|
+ // find active page (which should be highlighted)
|
|
|
+ LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
|
|
|
+ int activePosition = layoutManager.findFirstVisibleItemPosition();
|
|
|
+ if (activePosition == RecyclerView.NO_POSITION) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // find offset of active page (if the user is scrolling)
|
|
|
+ final View activeChild = layoutManager.findViewByPosition(activePosition);
|
|
|
+ int left = activeChild.getLeft();
|
|
|
+ int width = activeChild.getWidth();
|
|
|
+
|
|
|
+ // on swipe the active item will be positioned from [-width, 0]
|
|
|
+ // interpolate offset for smooth animation
|
|
|
+ //float progress = mInterpolator.getInterpolation(left * -1 / (float) width);
|
|
|
+
|
|
|
+ drawHighlights(c, indicatorStartX, indicatorPosY, activePosition, itemCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void drawInactiveIndicators(Canvas c, float indicatorStartX, float indicatorPosY, int itemCount) {
|
|
|
+ mPaint.setColor(colorInactive);
|
|
|
+
|
|
|
+ // width of item indicator including padding
|
|
|
+ final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
|
|
|
+
|
|
|
+ float start = indicatorStartX;
|
|
|
+ for (int i = 0; i < itemCount; i++) {
|
|
|
+ // draw the line for every item
|
|
|
+ if (itemCount != 1) {
|
|
|
+ c.drawCircle(start, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
|
|
|
+
|
|
|
+ start += itemWidth;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void drawHighlights(Canvas c, float indicatorStartX, float indicatorPosY,
|
|
|
+ int highlightPosition, int itemCount) {
|
|
|
+ mPaint.setColor(colorActive);
|
|
|
+
|
|
|
+ // width of item indicator including padding
|
|
|
+ final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
|
|
|
+
|
|
|
+
|
|
|
+ // no swipe, draw a normal indicator
|
|
|
+ float highlightStart = indicatorStartX + itemWidth * highlightPosition;
|
|
|
+ c.drawCircle(highlightStart, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
|
|
|
+
|
|
|
+ // draw the highlight overlapping to the next item as well
|
|
|
+ if (highlightPosition < itemCount - 1) {
|
|
|
+ highlightStart += itemWidth;
|
|
|
+ c.drawCircle(highlightStart, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|