Skip to content

Instantly share code, notes, and snippets.

@Gazer
Last active September 19, 2016 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gazer/6ecbed1b078ac0c2a07608e533698c13 to your computer and use it in GitHub Desktop.
Save Gazer/6ecbed1b078ac0c2a07608e533698c13 to your computer and use it in GitHub Desktop.
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