DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • How to Build a React Native Chat App for Android
  • Using Jetpack Compose With MVI Architecture
  • Implementing SOLID Principles in Android Development

Trending

  • How to Use Testcontainers With ScyllaDB
  • Enterprise-Grade Distributed JMeter Load Testing on Kubernetes: A Scalable, CI/CD-Driven DevOps Approach
  • Kung Fu Commands: Shifu Teaches Po the Command Pattern with Java Functional Interfaces
  • Designing AI Multi-Agent Systems in Java
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Make Your Progress Bar Smoother in Android

Make Your Progress Bar Smoother in Android

Want to smooth out that progress bar in Android? Here's how to get that done.

By 
Antoine Merle user avatar
Antoine Merle
·
Dec. 10, 13 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
30.8K Views

Join the DZone community and get the full member experience.

Join For Free

if you use the android gmail application, you probably noticed that the progress bar is a bit customized. i am not talking about the pull to refresh, but the indeterminate progress bar which appears just after. this indeterminate drawable is much smoother than the usual.

i will show you in this post a way to reproduce this smooth indeterminate horizontal progress bar. here is the result: the first progress bar uses the default indeterminate drawable while the others are all custom.


there is apparently a problem with the size of the video, so you might want to click on this link to see it on youtube.

how does the default animation work

first of all, you will need to use the horizontal progress bar style: widget.progressbar.horizontal for pre ics and widget.holo.progressbar.horizontal for post ics devices. this gives us two important parameters: indeterminateonly=false and indeterminatedrawable .

the default indeterminate drawable looks like this:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/progressbar_indeterminate_holo1" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo2" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo3" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo4" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo5" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo6" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo7" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate_holo8" android:duration="50" />
</animation-list>

it’s a simple animation drawable. that’s why we cannot have the smoothness we want. we just have to make a custom indeterminate drawable which dynamically draws lines instead of drawing bitmaps.

the custom indeterminate drawable

here is my original idea (don’t be impressed by my graphic skills):

basically we have some kind of exponential function we use to compute the length of each part of the drawable. in fact, the code is even simpler than that. we just have to loop and draw lines bigger than the previous one. you also have to take the offset in account, as this value will let you to have an animation.

note : my math skills are a bit poor, so i might be saying some stupid stuff, don’t hesitate to comment :)

here is the code i used.

int i = 0;
// we loop to draw lines until we reach the max width
while (prev < width) {
  float value = (float) math.expm1(++i + offset);
    canvas.drawline(prev, centery, prev + value - mseparatorwidth, centery, mpaint);
    prev = value + prev;
}

i used the expm1 function (which returns exp(x) – 1) but you can try any growing function. we could imagine a function which multiply by two the previous part each time, or some kind of fibonacci sequence.

here is the result for the code above:

i could stop here but i wanted to go further and give to our drawable the possibility to be fully customizable.

use interpolators!

i’ve always loved the relation between these curves and animations. it allows developers to radically change their animation in just one line. you can make it bounce, overshoot,… just by changing the interpolator. i used interpolators here to change the way the animation looks like.

a chart is better than words:

well. seeing this, code would be better than my chart. sorry about that.

basically, we set a number of sections. the interpolator will give us the ratio of each section’s length. the animation is done by an offset, incremented at each frame.

int width = mbounds.width() + mseparatorwidth;
int centery = mbounds.centery();
float xsectionwidth = 1f / msectionscount;

//line before the first section
int offset = (int) (math.abs(minterpolator.getinterpolation(mcurrentoffset) - minterpolator.getinterpolation(0)) * width);
if (offset > 0) {
    if (offset > mseparatorwidth) {
        canvas.drawline(0, centery, offset - mseparatorwidth, centery, mpaint);
    }
}

int prev;
int end;
int spacelength;
for (int i = 0; i < msectionscount; ++i) {
    float xoffset = xsectionwidth * i + mcurrentoffset;
    prev = (int) (minterpolator.getinterpolation(xoffset) * width);
    float ratiosectionwidth =
            math.abs(minterpolator.getinterpolation(xoffset) -
                    minterpolator.getinterpolation(math.min(xoffset + xsectionwidth, 1f)));

    //separator between each piece of line                
    int sectionwidth = (int) (width * ratiosectionwidth);
    if (sectionwidth + prev < width)
        spacelength = math.min(sectionwidth, mseparatorwidth);
    else
      spacelength = 0;
    int drawlength = sectionwidth > spacelength ? sectionwidth - spacelength : 0;
    end = prev + drawlength;

    if (end > prev) {
        canvas.drawline(prev, centery, end, centery, mpaint);
    }
}

here is the result with some basic interpolators:

sorry about the lag in the gif, a sample apk will be available soon, so stay tuned ;)

limitations : your interpolator must be a monotonic function!

library

i made a small lib available on github ! do not hesitate to fork it and make pull requests!


Android (robot)

Published at DZone with permission of Antoine Merle, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • How to Build a React Native Chat App for Android
  • Using Jetpack Compose With MVI Architecture
  • Implementing SOLID Principles in Android Development

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: