Created
September 19, 2016 11:53
-
-
Save Gazer/6702455190adefef62bf721e9c5be0ae to your computer and use it in GitHub Desktop.
A simple Page indicator for Android ViewPager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.os.Build; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import com.example.R; | |
/** | |
* Simple page indicator | |
* <p> | |
* Created by gazer on 9/13/16. | |
*/ | |
public class PagerIndicator extends View implements ViewPager.OnPageChangeListener { | |
private Paint paint; | |
private Paint currentPaint; | |
private ViewPager pager; | |
private int desiredRadius; | |
public PagerIndicator(Context context) { | |
super(context); | |
setup(); | |
} | |
public PagerIndicator(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setup(); | |
} | |
public PagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
setup(); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public PagerIndicator(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
setup(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
desiredRadius = getContext().getResources().getDimensionPixelSize(R.dimen.pager_indicator_radius); | |
int desiredWidth = 2 * desiredRadius + getPaddingLeft() + getPaddingRight(); | |
int desiredHeight = 2 * desiredRadius + getPaddingTop() + getPaddingBottom(); | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
int width; | |
int height; | |
//Measure Width | |
if (widthMode == MeasureSpec.EXACTLY) { | |
//Must be this size | |
width = widthSize; | |
} else if (widthMode == MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
width = Math.min(desiredWidth, widthSize); | |
} else { | |
//Be whatever you want | |
width = desiredWidth; | |
} | |
//Measure Height | |
if (heightMode == MeasureSpec.EXACTLY) { | |
//Must be this size | |
height = heightSize; | |
} else if (heightMode == MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
height = Math.min(desiredHeight, heightSize); | |
} else { | |
//Be whatever you want | |
height = desiredHeight; | |
} | |
//MUST CALL THIS | |
setMeasuredDimension(width, height); | |
} | |
private void setup() { | |
paint = new Paint(); | |
paint.setColor(Color.LTGRAY); | |
paint.setStyle(Paint.Style.FILL_AND_STROKE); | |
paint.setAntiAlias(true); | |
currentPaint = new Paint(); | |
currentPaint.setColor(Color.WHITE); | |
currentPaint.setStyle(Paint.Style.FILL_AND_STROKE); | |
currentPaint.setAntiAlias(true); | |
} | |
Rect bounds = new Rect(); | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (isInEditMode()) { | |
drawDots(canvas, 5, 2); | |
return; | |
} | |
if (pager != null) { | |
int total = pager.getAdapter().getCount(); | |
int currentPage = pager.getCurrentItem(); | |
drawDots(canvas, total, currentPage); | |
} | |
} | |
private void drawDots(Canvas canvas, int total, int currentPage) { | |
canvas.getClipBounds(bounds); | |
float centerX = bounds.centerX(); | |
float centerY = bounds.centerY(); | |
float totalWidth = total * desiredRadius + (total - 1) * desiredRadius; | |
float x = centerX - totalWidth + desiredRadius; | |
for (int i = 0; i < total; i++) { | |
canvas.drawCircle(x, centerY, desiredRadius, i == currentPage ? currentPaint : paint); | |
x += desiredRadius * 4; | |
} | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
// Not used | |
} | |
@Override | |
public void onPageSelected(int position) { | |
invalidate(); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
// Not used | |
} | |
public void setPager(ViewPager pager) { | |
this.pager = pager; | |
pager.addOnPageChangeListener(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment