Wednesday 17 May 2017

Circular seekbar in android

In this tutorial we are going to explain how to create circular seekbar view with progress update. There are a number of settings also available from sweep angle to rotation that can be set. This way the seekbar can grow in size with assigning dif-2 value. Also it is still possible to set the seekbar to a specific size by setting the container layouts width and height to specific dp values. This sizing approach gives the best of both worlds. To further adjust how the arc fits in its container a padding attribute can also be used.

                                                             


To understanding how to use the circular seekbar download the complete source code and try with a number of controls that can be used to adjust the attributes of the seekbar . This is by far the best way to get a good understanding of how to use the circular seekbar .



CircleDemo.java


public class CircleDemo extends Activity {

    ProgressCircle progressCircle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_demo);

        progressCircle = (ProgressCircle) findViewById(R.id.progress_circle);
        progressCircle.startAnimation();
    }

    public void animate(View view) {
        float val = new Random().nextFloat();

        progressCircle.startAnimation();
    }
}



ProgressCircle.java



public class ProgressCircle extends View {

    private final RectF mOval = new RectF();
    private float mSweepAngle = 0;
    private int startAngle = 90;
    private int angleGap = 4;
    private Bitmap icon;


    float mEndAngle = 1.0f;

    Paint progressPaint = new Paint();
    TextPaint textPaint = new TextPaint();
    Paint incompletePaint = new Paint();
    Paint percentagePaint = new Paint();

    private float strokeWidth = 30.0f;

    public ProgressCircle(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ProgressCircle,
                0, 0);

        int textColor;
        float textSize;

        int progressColor;
        int incompleteColor;

        try {
            textColor = a.getColor(R.styleable.ProgressCircle_android_textColor, Color.BLUE);
            textSize = a.getDimension(R.styleable.ProgressCircle_android_textSize, 100);

            strokeWidth = a.getDimension(R.styleable.ProgressCircle_strokeWidth, 30.0f);

            progressColor = a.getColor(R.styleable.ProgressCircle_progressColor, Color.RED);
            incompleteColor = a.getColor(R.styleable.ProgressCircle_incompleteProgressColor, Color.GRAY);
        } finally {
            a.recycle();
        }

        progressPaint.setColor(progressColor);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        Typeface tf = Typeface.create("Roboto Condensed Light", Typeface.BOLD);
        textPaint.setTypeface(tf);

        percentagePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        percentagePaint.setColor(textColor);
        percentagePaint.setTextSize(textSize / 3);

        incompletePaint.setColor(incompleteColor);
        incompletePaint.setStrokeWidth(strokeWidth);
        incompletePaint.setStyle(Paint.Style.STROKE);
        incompletePaint.setFlags(Paint.ANTI_ALIAS_FLAG);



    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float currentAngleGap = mSweepAngle == 1.0f || mSweepAngle == 0 ? 0 : angleGap;
        mOval.set(strokeWidth / 2, strokeWidth / 2, getWidth() - (strokeWidth / 2), getWidth() - (strokeWidth / 2));
        canvas.drawArc(mOval, -startAngle + currentAngleGap, (mSweepAngle * 360) - currentAngleGap, false,
                progressPaint);

        mOval.set(strokeWidth / 2, strokeWidth / 2, getWidth() - (strokeWidth / 2), getWidth() - (strokeWidth / 2));
        canvas.drawArc(mOval, mSweepAngle * 360- startAngle + currentAngleGap, 360 - (mSweepAngle * 360) - currentAngleGap, false,
                incompletePaint);

        drawText(canvas, textPaint, String.valueOf((int) (mSweepAngle * 100)), percentagePaint);

        if(icon != null)
            canvas.drawBitmap(icon, canvas.getWidth() / 2 - icon.getWidth() / 2, strokeWidth + (canvas.getHeight() / 15), null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    private void drawText(Canvas canvas, Paint paint, String text, Paint percentagePaint) {
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        Rect percentageBounds = new Rect();
        percentagePaint.getTextBounds("%", 0, 1, percentageBounds);
        int x = (canvas.getWidth() / 2) - (bounds.width() / 2) - (percentageBounds.width() / 2);
        int y = (canvas.getHeight() / 2) + (bounds.height() / 2);
        canvas.drawText(text, x, y, paint);

        canvas.drawText("%", x + bounds.width() + percentageBounds.width() / 2, y - bounds.height() + percentageBounds.height(), percentagePaint);
    }

    public void setTextColor(int color) {
        textPaint.setColor(color);
    }

    public void setProgressColor(int color) {
        progressPaint.setColor(color);
    }

    public void setIncompleteColor(int color) {
        incompletePaint.setColor(color);
    }

    public void setProgress(float progress) {
        if (progress > 1.0f || progress < 0) {
            throw new RuntimeException("Value must be between 0 and 1: " + progress);
        }

        mEndAngle = progress;

        this.invalidate();
    }

    public void startAnimation() {
        ValueAnimator anim = ValueAnimator.ofFloat(mSweepAngle, mEndAngle);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                ProgressCircle.this.mSweepAngle = (Float) valueAnimator.getAnimatedValue();
                ProgressCircle.this.invalidate();
            }
        });
        anim.setDuration(10000);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.start();

    }

}


Hope this tutorial will help you. Download complete project HERE


Here is the ouput screen : 


                                          

53 comments:

  1. Android Vertical Stepper Form Library
    It may display a transient feedback message after a step is saved. And today we’re going to see a cool library which help you to implement that stepper form in your applications. This Android library implements a vertical stepper form following Google Material Design guidelines.
    http://www.tellmehow.co/android-vertical-stepper-form-library/

    ReplyDelete
  2. Good one... i am very much interested to start my career as Android Developer, i think to start my learning in Android Training in Chennai can you suggest me what is the future strategies in Android across the world market.. Thanks Abdul..

    ReplyDelete
  3. Hi,
    Thanks for sharing the info about Android Plz keep sharing on...
    Thank you...

    ReplyDelete
  4. nice article... for android classes in Jaipur
    please visit Our Website

    ReplyDelete
  5. Thanks for the information and code.
    We are Netguru Solution India Pvt Ltd, one of the leading and developing Website Design and Mobile Application Development Company in Pune with a specialist management and expert group.
    Mobile Application Development Company In Pune
    Website Design Company In Pune

    ReplyDelete
  6. Wonderful post however I was wondering if you could write a litte more on this subject? I'd be very thankful if you could elaborate a little bit more. Cheers! iPhone application development

    ReplyDelete
  7. Thanks for providing Best Content.
    Kvch Delhi delivers Java Six Weeks Project Based Summer Training In Delhi for M.Tech, B.Tech CSE, MCA, BCA. The Skilled Development Company has been recently entered in the list of top ten JAVA summer training institute in Delhi. The core skills of the JAVA summer training are based-on the trainers and learning environment provided to the students.
    G-18, 2nd Floor, Preet Vihar, Delhi-110092
    Email: delhi.trg1@kvch.in
    Contact: +919212260630, +919212577708

    BEST 6 WEEKS JAVA INTERNSHIP COMPANIES IN DELHI
    BEST JAVA SIX WEEKS PROJECT BASED SUMMER TRAINING IN DELHI

    ReplyDelete
  8. good work! keep it up. it's really helpful coding for us.
    Android training in Chandigarh

    ReplyDelete
  9. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  10. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    Devops training in sholinganallur

    ReplyDelete
  11. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site
    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  12. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....

    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  13. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers


    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  14. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers


    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  15. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    Java training in Bangalore | Java training institute in Bangalore | Java course in Bangalore

    Java interview questions and answers

    Core Java interview questions and answers

    ReplyDelete
  16. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  17. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    python course in pune
    python course in chennai
    python course in Bangalore

    ReplyDelete
  18. I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.
    Python training in bangalore
    Python course in pune
    Python training in bangalore

    ReplyDelete
  19. Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.

    Best Devops Training in pune
    excel advanced excel training in bangalore

    ReplyDelete
  20. hank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me 

    angularjs-Training in velachery

    angularjs-Training in pune

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    ReplyDelete
  21. Woah this blog is wonderful i like studying your posts. Keep up the great work! You understand, lots of persons are hunting around for this info, you could help them greatly.
    Python Online certification training
    python Training institute in Chennai
    Python training institute in Bangalore

    ReplyDelete
  22. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.

    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune

    ReplyDelete
  23. Thank For Sharing Your Information The Information Shared Is Very Valuable Please Keep Updating Us Time Went On Just Reading The Article Python Online Course

    ReplyDelete
  24. Nice Post! Thank you for sharing very good post, it was so Nice to read and useful to improve my knowledge as updated one, keep blogging.
    Angular js Training in Electronic City

    ReplyDelete
  25. I got what i am seraching from last few days in your Blog. I hope you will share more info about it. Please keep sharing.
    Laptop Service center in Ameerpet
    Dell Service center in Ameerpet
    HP Service center in Ameerpet
    Lenovo Service center in Ameerpet

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Best Institute for Data Science in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Best Institute for Data Science

    ReplyDelete
  31. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Data Science institute in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Data Science institute in Mumbai

    ReplyDelete
  32. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live.thanks for the updation.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  33. I’ve beeen exploring for a little for any high quakity articles or weblog posts on this kind of space.

    BU Bhopal BA 3rd Year Result mark sheet

    ReplyDelete
  34. Infycle Technologies, the best software training institute in Chennai offers the best Data Science training in Chennai for tech professionals and freshers. In addition to Big Data, Infycle also offers other professional courses such as Python, Oracle, Java, Power BI, Digital Marketing, Big Data, etc., which will be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7502633633 to get more info and a free demo.

    ReplyDelete
  35. मैं आपके b a 3rd year time table घोषित ब्लॉग को बुकमार्क कर दूंगा और इसे नियमित रूप से यहीं पर फिर से देखूंगा।

    ReplyDelete
  36. This post is so interactive and informative.keep update more information...
    German Classes in Velachery
    German Classes in chennai

    ReplyDelete
  37. Allow expert Congress face apply. Spend size network hope sing civil floor step.news today live

    ReplyDelete