Android Open Source - ExpertAndroid Circle View






From Project

Back to project page ExpertAndroid.

License

The source code is released under:

MIT License

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;
/*from  w w  w  .  j av  a 2s. c om*/
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;

public class CircleView extends AbstractBaseView implements View.OnClickListener {
    private static final String TAG = "CircleView";
    private static final int RADIUS_MULTIPLIER = 2;

    private int defRadius = 20;
    private int strokeWidth = 10;
    private int 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();
    }

    public void 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
    protected int hGetMaximumHeight() {
        return this.defRadius * RADIUS_MULTIPLIER;
    }

    @Override
    protected int hGetMaximumWidth() {
        return this.defRadius * RADIUS_MULTIPLIER;
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        this.defRadius *= 1.2;
        this.adjustMinimumHeight();
        this.requestLayout();
        this.invalidate();
    }

    private void adjustMinimumHeight() {
        this.setMinimumHeight(this.defRadius * RADIUS_MULTIPLIER);
        this.setMinimumWidth(this.defRadius * RADIUS_MULTIPLIER);
    }

    @Override
    public void 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
    protected void 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;
    }

    public static final class SavedState extends BaseSavedState {
        public static final 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) {
                return new 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) {
                return new SavedState[size];
            }
        };

        private int 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
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(this.defRadius);
        }
    }
}




Java Source Code List

com.androidbook.notebad.NoteEditor.java
com.androidbook.notebad.NotePadProvider.java
com.androidbook.notebad.NotePad.java
com.androidbook.notebad.NotesList.java
com.androidbook.notebad.TitleEditor.java
com.androidbook.parse.BaseActivity.java
com.androidbook.parse.BaseListActivity.java
com.androidbook.parse.CreateAMeaningActivity.java
com.androidbook.parse.CreateAWordActivity.java
com.androidbook.parse.Field.java
com.androidbook.parse.FormActivity.java
com.androidbook.parse.IReportBack.java
com.androidbook.parse.IValidator.java
com.androidbook.parse.IValueValidator.java
com.androidbook.parse.LoginActivity.java
com.androidbook.parse.ParseApplication.java
com.androidbook.parse.ParseObjectWrapperOld1.java
com.androidbook.parse.ParseObjectWrapper.java
com.androidbook.parse.ParseStarterProjectActivity.java
com.androidbook.parse.PasswordFieldRule.java
com.androidbook.parse.PasswordResetActivity.java
com.androidbook.parse.PasswordResetSuccessActivity.java
com.androidbook.parse.SignupActivity.java
com.androidbook.parse.SignupSuccessActivity.java
com.androidbook.parse.StringUtils.java
com.androidbook.parse.UserListActivity.java
com.androidbook.parse.WelcomeActivity.java
com.androidbook.parse.WordListActivity.java
com.androidbook.parse.WordListAdapter.java
com.androidbook.parse.WordMeaningListAdapter.java
com.androidbook.parse.WordMeaning.java
com.androidbook.parse.WordMeaningsListActivity.java
com.androidbook.parse.Word.java
com.iuriio.demos.expertandroid.ch10search.MainActivity.java
com.iuriio.demos.expertandroid.ch10search.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.MainActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SimpleSuggestionProvider.java
com.iuriio.demos.expertandroid.ch11searchprovider.SuggestUrlProvider.java
com.iuriio.demos.expertandroid.ch13parsesimple.BaseActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.LoginActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.MainActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseApp.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseObjectWrapper.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseStarterProjectActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.PasswordResetSuccessActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ResetPasswordActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.SignupActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.StringUtils.java
com.iuriio.demos.expertandroid.ch13parsesimple.WordListActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.Word.java
com.iuriio.demos.expertandroid.ch1circleview.AbstractBaseView.java
com.iuriio.demos.expertandroid.ch1circleview.CircleView.java
com.iuriio.demos.expertandroid.ch1circleview.MainActivity.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DatePickerFragment.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DurationControl.java
com.iuriio.demos.expertandroid.ch2durationcontrol.MainActivity.java
com.iuriio.demos.expertandroid.ch3flowlayout.FlowLayout.java
com.iuriio.demos.expertandroid.ch3flowlayout.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.ChildObject.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainObject.java
com.iuriio.demos.expertandroid.ch6forms.BaseActivity.java
com.iuriio.demos.expertandroid.ch6forms.Field.java
com.iuriio.demos.expertandroid.ch6forms.FormActivity.java
com.iuriio.demos.expertandroid.ch6forms.IValidator.java
com.iuriio.demos.expertandroid.ch6forms.IValueValidator.java
com.iuriio.demos.expertandroid.ch6forms.MainActivity.java
com.iuriio.demos.expertandroid.ch6forms.PasswordFieldRule.java
com.iuriio.demos.expertandroid.ch6forms.StringUtils.java
com.iuriio.demos.expertandroid.ch6forms.WelcomeActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AnimatedSimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20ControlledAnimatedTexturedCubeRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SingleTextureAbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.FrustumDimensions.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MainActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MyApplication.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES10Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES20Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.PolygonRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.RegularPolygon.java
com.iuriio.demos.expertandroid.ch9openglexperiments.Shape.java
com.iuriio.demos.expertandroid.ch9openglexperiments.SimpleTriangleRenderer.java
converters.FieldTransporter.java
converters.IFieldTransport.java
converters.IntegerFieldTransport.java
converters.ParseObjectEssentials.java
converters.StringFieldTransport.java
converters.User.java
converters.ValueField.java