Skip to content

Instantly share code, notes, and snippets.

@hattorix
Created May 26, 2011 03:48
Show Gist options
  • Save hattorix/992511 to your computer and use it in GitHub Desktop.
Save hattorix/992511 to your computer and use it in GitHub Desktop.
iPhone like PageIndicator for Android
package com.gist.hattorix;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class PageIndicator extends View {
static final String TAG = "PageIndicator";
/** 総ページ数。 */
private int pageCount = 0;
/** 現在のページ。 */
private int currentPage = 0;
/** インジケータの半径。 */
private float indicatorRadius = 5;
/** インジケータの間隔。 */
private int indicatorGap = 10;
public PageIndicator(Context context) {
this(context, null);
}
public PageIndicator(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PageIndicator(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
pageCount = attrs.getAttributeIntValue(null, "page_count", pageCount);
indicatorRadius = attrs.getAttributeFloatValue(null,
"inidicator_diameter", indicatorRadius);
indicatorGap = attrs.getAttributeIntValue(null, "inidicator_gap",
indicatorGap);
}
// ------------------------------------------------------------------------
// getter/setter
// ------------------------------------------------------------------------
/**
* @return pageCount
*/
public int getPageCount() {
return pageCount;
}
/**
* @param pageCount セットする pageCount
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
if (currentPage >= pageCount) {
currentPage = pageCount - 1;
}
invalidate();
}
/**
* @return currentPage
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage セットする currentPage
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
invalidate();
}
/**
* @return indicatorRadius
*/
public float getIndicatorRadius() {
return indicatorRadius;
}
/**
* @param indicatorRadius セットする indicatorRadius
*/
public void setIndicatorRadius(float indicatorRadius) {
this.indicatorRadius = indicatorRadius;
invalidate();
}
/**
* @return indicatorGap
*/
public int getIndicatorGap() {
return indicatorGap;
}
/**
* @param indicatorGap セットする indicatorGap
*/
public void setIndicatorGap(int indicatorGap) {
this.indicatorGap = indicatorGap;
invalidate();
}
// ------------------------------------------------------------------------
// Override
// ------------------------------------------------------------------------
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height = 0;
// 幅の計算
if (widthMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
width = widthSize;
} else {
width = getDrawingWidth();
}
// 高さの計算
if (heightMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
height = heightSize;
} else {
if (pageCount > 0) {
height = (int) Math.ceil(indicatorRadius * 2) + getPaddingTop()
+ getPaddingBottom();
}
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
// Draw the background for this view
super.onDraw(canvas);
if (pageCount == 0) return;
float cy = (float) getHeight() / 2;
int width = getWidth();
int drawingWidth = getDrawingWidth();
float vspec = (float) (width - drawingWidth) / 2 + indicatorRadius;
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setAlpha(64);
for (int i = 0; i < pageCount; i++) {
if (i == currentPage) {
int alpha = paint.getAlpha();
paint.setAlpha(255);
canvas.drawCircle(vspec, cy, indicatorRadius, paint);
paint.setAlpha(alpha);
} else {
canvas.drawCircle(vspec, cy, indicatorRadius, paint);
}
vspec += indicatorRadius * 2 + indicatorGap;
}
}
// ------------------------------------------------------------------------
// private
// ------------------------------------------------------------------------
/**
* 描画幅のサイズを取得します。
*
* @return 描画幅のサイズ
*/
private int getDrawingWidth() {
if (pageCount == 0) return 0;
float width = (pageCount - 1) * indicatorGap;
width += (pageCount * indicatorRadius * 2);
return (int) width;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment