If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.iuriio.demos.expertandroid.ch1circleview;
/*fromwww.java2s.com*/import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
publicclass CircleView extends AbstractBaseView implements View.OnClickListener {
privatestaticfinal String TAG = "CircleView";
privatestaticfinalint RADIUS_MULTIPLIER = 2;
privateint defRadius = 20;
privateint strokeWidth = 10;
privateint strokeColor = 0xFFFF8C00;
public CircleView(Context context) {
super(context);
this.initCircleView();
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.CircleView, defStyle, 0);
assert t != null;
this.strokeColor = t.getColor(R.styleable.CircleView_strokeColor, strokeColor);
this.strokeWidth = t.getInt(R.styleable.CircleView_strokeWidth, strokeWidth);
t.recycle();
this.initCircleView();
}
publicvoid initCircleView() {
// Set the minimum width and height.
this.setMinimumHeight(this.defRadius * RADIUS_MULTIPLIER);
this.setMinimumWidth(this.defRadius * RADIUS_MULTIPLIER);
this.setOnClickListener(this);
this.setClickable(true);
this.setSaveEnabled(true);
}
@Override
protectedint hGetMaximumHeight() {
return this.defRadius * RADIUS_MULTIPLIER;
}
@Override
protectedint hGetMaximumWidth() {
return this.defRadius * RADIUS_MULTIPLIER;
}
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
publicvoid onClick(View v) {
this.defRadius *= 1.2;
this.adjustMinimumHeight();
this.requestLayout();
this.invalidate();
}
privatevoid adjustMinimumHeight() {
this.setMinimumHeight(this.defRadius * RADIUS_MULTIPLIER);
this.setMinimumWidth(this.defRadius * RADIUS_MULTIPLIER);
}
@Override
publicvoid onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d(TAG, "OnDraw called");
int w = this.getWidth();
int h = this.getHeight();
int ox = w / 2;
int oy = h / 2;
int rad = Math.min(ox, oy) / 2;
canvas.drawCircle(ox, oy, rad, this.getBrush());
}
private Paint getBrush() {
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(this.strokeWidth);
paint.setColor(this.strokeColor);
paint.setStyle(Paint.Style.STROKE);
return paint;
}
@Override
protectedvoid onRestoreInstanceState(Parcelable p) {
if (!(p instanceof SavedState)) {
super.onRestoreInstanceState(p);
return;
}
SavedState ss = (SavedState) p;
super.onRestoreInstanceState(ss.getSuperState());
this.defRadius = ss.defRadius;
this.initCircleView();
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.defRadius = this.defRadius;
return ss;
}
publicstaticfinalclass SavedState extends BaseSavedState {
publicstaticfinal Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
/**
* Create a new instance of the Parcelable class, instantiating it
* from the given Parcel whose data had previously been written by
* {@link android.os.Parcelable#writeToParcel Parcelable.writeToParcel()}.
*
* @param source The Parcel to read the object's data from.
* @return Returns a new instance of the Parcelable class.
*/
@Override
public SavedState createFromParcel(Parcel source) {
returnnew SavedState(source);
}
/**
* Create a new array of the Parcelable class.
*
* @param size Size of the array.
* @return Returns an array of the Parcelable class, with every entry
* initialized to null.
*/
@Override
public SavedState[] newArray(int size) {
returnnew SavedState[size];
}
};
privateint defRadius;
/**
* Constructor called by derived classes when creating their SavedState objects
*
* @param superState The state of the superclass of this view
*/public SavedState(Parcelable superState) {
super(superState);
}
/**
* Constructor used when reading from a parcel. Reads the state of the superclass.
*
* @param source Source to restore state from.
*/public SavedState(Parcel source) {
super(source);
this.defRadius = source.readInt();
}
@Override
publicvoid writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(this.defRadius);
}
}
}