|
|
@@ -0,0 +1,148 @@
|
|
|
+package kr.co.zumo.app.lifeplus.view.screen.event;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.res.Resources;
|
|
|
+import android.graphics.Canvas;
|
|
|
+import android.graphics.Paint;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.support.v7.widget.LinearLayoutManager;
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.view.View;
|
|
|
+
|
|
|
+import kr.co.zumo.app.lifeplus.view.IndexScrollListener;
|
|
|
+
|
|
|
+/**
|
|
|
+ * CustomIndicator
|
|
|
+ * <pre>
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author 하세미
|
|
|
+ * @version 1.0
|
|
|
+ * @history 하세미 [2019-01-03] [최초 작성]
|
|
|
+ * @since 2019-01-03
|
|
|
+ */
|
|
|
+public class CustomIndicator extends View {
|
|
|
+
|
|
|
+ public CustomIndicator(Context context) {
|
|
|
+ super(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CustomIndicator(Context context, @Nullable AttributeSet attrs) {
|
|
|
+ super(context, attrs);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CustomIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
+ super(context, attrs, defStyleAttr);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Context context;
|
|
|
+ private int activeColor;
|
|
|
+ private int inActiveColor;
|
|
|
+ private int bottomOffset;
|
|
|
+
|
|
|
+ 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 * 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicator width.
|
|
|
+ */
|
|
|
+ private final float mIndicatorItemLength = DP * 4;
|
|
|
+ /**
|
|
|
+ * Padding between indicators.
|
|
|
+ */
|
|
|
+ private final float mIndicatorItemPadding = DP * 8;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Some more natural animation interpolation
|
|
|
+ */
|
|
|
+
|
|
|
+ private final Paint mPaint = new Paint();
|
|
|
+
|
|
|
+ private int itemCount = -1;
|
|
|
+
|
|
|
+ private int index = -1;
|
|
|
+
|
|
|
+ private IndexScrollListener indexScrollListener;
|
|
|
+
|
|
|
+ public void onChangedIndex(int itemCount, int index) {
|
|
|
+ if (this.itemCount != itemCount || this.index != index) {
|
|
|
+ this.itemCount = itemCount;
|
|
|
+ this.index = index;
|
|
|
+
|
|
|
+ invalidate();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
|
|
|
+
|
|
|
+ if (null == parent || null == parent.getAdapter()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int itemCount = parent.getAdapter().getItemCount();
|
|
|
+
|
|
|
+ float totalLength = mIndicatorItemLength * itemCount;
|
|
|
+ float paddingBetweenItems = Math.max(0, itemCount - 1) * mIndicatorItemPadding;
|
|
|
+ float indicatorTotalWidth = totalLength + paddingBetweenItems;
|
|
|
+ float indicatorStartX = (parent.getWidth() - indicatorTotalWidth) / 2F;
|
|
|
+ float indicatorPosY = parent.getHeight() - mIndicatorHeight / 2F;
|
|
|
+
|
|
|
+ drawInactiveIndicators(canvas, indicatorStartX, indicatorPosY, itemCount);
|
|
|
+
|
|
|
+ // find active page (which should be highlighted)
|
|
|
+ LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
|
|
|
+ int activePosition = IndexScrollListener.getCurrentIndex(layoutManager);
|
|
|
+// Log.e("APP# MainBannerViewItemDotIndicator | onDrawOver", "| activePosition: " + activePosition);
|
|
|
+
|
|
|
+ if (activePosition == RecyclerView.NO_POSITION) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ drawHighlights(canvas, indicatorStartX, indicatorPosY, activePosition, itemCount);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void drawInactiveIndicators(Canvas c, float indicatorStartX, float indicatorPosY, int itemCount) {
|
|
|
+ mPaint.setColor(context.getResources().getColor(inActiveColor));
|
|
|
+
|
|
|
+ // width of item indicator including padding
|
|
|
+ final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
|
|
|
+
|
|
|
+ float start = indicatorStartX;
|
|
|
+ for (int i = 0; i < itemCount; i++) {
|
|
|
+ // itemCount 1은 안띄움
|
|
|
+ if (itemCount != 1) {
|
|
|
+ c.drawCircle(start, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
|
|
|
+ start += itemWidth;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void drawHighlights(Canvas canvas, float indicatorStartX, float indicatorPosY,
|
|
|
+ int highlightPosition, int itemCount) {
|
|
|
+ mPaint.setColor(context.getResources().getColor(activeColor));
|
|
|
+
|
|
|
+ // width of item indicator including padding
|
|
|
+ final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
|
|
|
+
|
|
|
+ float start = indicatorStartX;
|
|
|
+ for (int i = 0; i < itemCount; i++) {
|
|
|
+ if (itemCount != 1 && i == highlightPosition) {
|
|
|
+ canvas.drawCircle(start, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
|
|
|
+ }
|
|
|
+ start += itemWidth;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|