Skip to content

Instantly share code, notes, and snippets.

@nevack
Created February 5, 2018 14:14
Show Gist options
  • Save nevack/120d52b5f9287f457020d6612017156c to your computer and use it in GitHub Desktop.
Save nevack/120d52b5f9287f457020d6612017156c to your computer and use it in GitHub Desktop.
package org.nevack.nevedomskymobdev.welcome;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class PageIndicatorView extends View{
private int currentPage = 2;
private int pageCount = 5;
private Paint active;
private Paint inactive;
private int radius;
private int pad;
private int width= 200;
private int height = 40;
public PageIndicatorView(Context context) {
super(context);
init();
}
public PageIndicatorView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public PageIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
radius = 20;
pad = radius * 2;
width = (pad * (pageCount - 1)) + (radius * 2 * pageCount);
height = pad;
// Try for a width based on our minimum
int minw = getPaddingLeft() + getPaddingRight() + width;
int w = resolveSizeAndState(minw, widthMeasureSpec, 0);
// Whatever the width ends up being, ask for a height that would let the pie
// get as big as it can
int minh = getPaddingBottom() + getPaddingTop() + height;
int h = resolveSizeAndState(minh, heightMeasureSpec, 0);
setMeasuredDimension(w, h);
}
@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i < pageCount; i++) {
canvas.drawCircle(
(canvas.getWidth() - width) / 2 + pad * i + radius * 2 * i + radius,
canvas.getHeight() / 2,
radius,
inactive
);
}
int x = currentPage - 1;
canvas.drawCircle(
(canvas.getWidth() - width) / 2 + pad * x + radius * 2 * x + radius,
canvas.getHeight() / 2,
radius,
active
);
}
private void init() {
active = new Paint(Paint.ANTI_ALIAS_FLAG);
active.setColor(Color.DKGRAY);
inactive = new Paint(Paint.ANTI_ALIAS_FLAG);
inactive.setColor(Color.GRAY);
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
invalidate();
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
invalidate();
requestLayout();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment