Skip to content

Instantly share code, notes, and snippets.

@KazaKago
Last active September 14, 2015 02:24
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 KazaKago/81123f712ab36bfa77cf to your computer and use it in GitHub Desktop.
Save KazaKago/81123f712ab36bfa77cf to your computer and use it in GitHub Desktop.
2重円弧描画Viewクラス
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleView">
<attr name="start_angle" format="float|reference"/>
<attr name="draw_angle" format="float|reference"/>
<attr name="primary_line_color" format="color|reference"/>
<attr name="secondary_line_color" format="color|reference"/>
<attr name="stroke_width" format="dimension|reference"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
/**
* 円弧描画クラス
* <p/>
* Created by tamura_k on 2015/09/10.
*/
public class CircleView extends View {
private float mStartAngle;
private float mDrawAngle;
private final Paint mFirstPaint = new Paint();
private final Paint mSecondPaint = new Paint();
private final RectF mOval = new RectF();
public CircleView(Context context) {
this(context, null);
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs, defStyleAttr);
}
private void initView(Context context, AttributeSet attrs, int defStyle) {
mFirstPaint.setAntiAlias(true);
mFirstPaint.setStyle(Paint.Style.STROKE);
mSecondPaint.setAntiAlias(true);
mSecondPaint.setStyle(Paint.Style.STROKE);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView, defStyle, 0);
setStartAngle(typedArray.getFloat(R.styleable.CircleView_start_angle, -90.0f));
setDrawAngle(typedArray.getFloat(R.styleable.CircleView_draw_angle, 270.0f));
setPrimaryLineColor(typedArray.getColor(R.styleable.CircleView_primary_line_color, Color.BLACK));
setSecondaryLineColor(typedArray.getColor(R.styleable.CircleView_secondary_line_color, Color.LTGRAY));
setStrokeWidth(typedArray.getDimension(R.styleable.CircleView_stroke_width, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10.0f, getResources().getDisplayMetrics())));
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
mOval.set(getStrokeWidth(), getStrokeWidth(), canvas.getWidth() - getStrokeWidth(), canvas.getHeight() - getStrokeWidth());
canvas.drawArc(mOval, mStartAngle, 360, false, mSecondPaint);
canvas.drawArc(mOval, mStartAngle, mDrawAngle, false, mFirstPaint);
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable parent = super.onSaveInstanceState();
SavedState saved = new SavedState(parent);
saved.startAngle = getStartAngle();
saved.drawAngle = getDrawAngle();
saved.primaryLineColor = getPrimaryLineColor();
saved.secondaryLineColor = getSecondaryLineColor();
saved.strokeWidth = getStrokeWidth();
return saved;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState saved = (SavedState) state;
super.onRestoreInstanceState(saved.getSuperState());
setStartAngle(saved.startAngle);
setDrawAngle(saved.drawAngle);
setPrimaryLineColor(saved.primaryLineColor);
setSecondaryLineColor(saved.secondaryLineColor);
setStrokeWidth(saved.strokeWidth);
}
/**
* 孤の開始点を取得
*
* @retrun
*/
public float getStartAngle() {
return mStartAngle;
}
/**
* 孤の開始点を指定
* 時計の3時が0、時計の6時が90
*
* @param startAngle
*/
public void setStartAngle(float startAngle) {
mStartAngle = startAngle;
}
/**
* 弧の描画角度を取得
*
* @return
*/
public float getDrawAngle() {
return mDrawAngle;
}
/**
* 弧の描画角度を指定
*
* @param drawAngle
*/
public void setDrawAngle(float drawAngle) {
mDrawAngle = drawAngle;
}
/**
* メインの線の色を取得
*
* @return
*/
public int getPrimaryLineColor() {
return mFirstPaint.getColor();
}
/**
* メインの線の色を指定
*
* @param color
*/
public void setPrimaryLineColor(int color) {
mFirstPaint.setColor(color);
}
/**
* サブ(背景)の線の色を取得
*
* @return
*/
public int getSecondaryLineColor() {
return mSecondPaint.getColor();
}
/**
* サブ(背景)の線の色を指定
*
* @param color
*/
public void setSecondaryLineColor(int color) {
mSecondPaint.setColor(color);
}
/**
* 弧の線の太さを取得(px)
*
* @return
*/
public float getStrokeWidth() {
return mFirstPaint.getStrokeWidth();
}
/**
* 弧の線の太さを指定(px)
*
* @param width
*/
public void setStrokeWidth(float width) {
mFirstPaint.setStrokeWidth(width);
mSecondPaint.setStrokeWidth(width);
}
/**
* 状態保持用SavedStateクラス
*/
private static class SavedState extends BaseSavedState {
public float startAngle;
public float drawAngle;
public int primaryLineColor;
public int secondaryLineColor;
public float strokeWidth;
public SavedState(Parcel in) {
super(in);
startAngle = in.readFloat();
drawAngle = in.readFloat();
primaryLineColor = in.readInt();
secondaryLineColor = in.readInt();
strokeWidth = in.readFloat();
}
public SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(final Parcel out, final int flags) {
super.writeToParcel(out, flags);
out.writeFloat(startAngle);
out.writeFloat(drawAngle);
out.writeInt(primaryLineColor);
out.writeInt(secondaryLineColor);
out.writeFloat(strokeWidth);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment