Instantly share code, notes, and snippets.

@Gazer /PagerIndicator.java Secret
Last active Sep 19, 2016

Embed
What would you like to do?
public class PagerIndicator extends View {
// Constructors and auxiliars
@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 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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment