How to implement Pie Progress View in Android

I will be showing how to implement a Pie Progress View in an Android app. I’ve been asked asked to develop an Android app which contained a progress view in shape of a pie chart to show a countdown progress, so the view has to update every second.

Something like this:

screen-shot-2016-10-05-at-4-31-06-pm

To achieve this without reinventing the wheel, I fortunately found that David had created this drawable as you can see here.

So I will be showing how to implement his class, add it to an ImageView, change its color and update the progress.

First of all, we have to copy that class into our project. Once that is done, we will create an ImageView in the xml where we want to show the Pie Progress View:

<ImageView                  
android:id="@+id/time_progress"                  
android:layout_width="40dp"                  
android:layout_height="40dp"/>

In our activity’s or fragment’s onCreate we will create a new instance of the PieProgressDrawable class and set it’s color (in this case to white):

PieProgressDrawable pieProgressDrawable = new PieProgressDrawable();
pieProgressDrawable.setColor(ContextCompat.getColor(this, R.color.white));

Now we will set this drawable to our XML ImageView:

ImageView timeProgress = (ImageView) findViewById(R.id.time_progress);
timeProgress.setImageDrawable(pieProgressDrawable);

Now, let’s assume we have a CountDownTimer implemented which every few seconds calls a method passing a new progress to update the Pie Progress View:

    public void updateTime(int progress) {
        pieProgressDrawable.setLevel(progress);
        timeProgress.invalidate();
    }

pieProgressDrawable.setLevel(progress)  will set the drawable’s progress level. The level is then used to create a drawable with specific angles, colors and shapes to represent the Pie Drawable as you can see in PieProgressDrawable class method:

    @Override
    protected boolean onLevelChange(int level) {
        final float drawTo = START_ANGLE + ((float)360*level)/100f;
        boolean update = drawTo != mDrawTo;
        mDrawTo = drawTo;
        return update;
    }

Finally, in order to update the UI to show the new progress we need to redraw the drawable by calling: timeProgress.invalidate();

That’s it! Here’s the complete PieProgressDrawable class in case the gist is not available anymore:

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

/**
 * A PieProgressDrawable does this:
 * <a href="http://stackoverflow.com/questions/12458476/how-to-create-circular-progress-barpie-chart-like-indicator-android">Circular Progress Bar Android</a>
 */
public class PieProgressDrawable extends Drawable {

    Paint mPaint;
    RectF mBoundsF;
    RectF mInnerBoundsF;
    final float START_ANGLE = 0.f;
    float mDrawTo;

    public PieProgressDrawable() {
        super();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    /**
     * Set the border width.
     * @param widthDp in dip for the pie border
     */
    public void setBorderWidth(float widthDp, DisplayMetrics dm) {
        float borderWidth = widthDp * dm.density;
        mPaint.setStrokeWidth(borderWidth);
    }

    /**
     * @param color you want the pie to be drawn in
     */
    public void setColor(int color) {
        mPaint.setColor(color);
    }

    @Override
    public void draw(Canvas canvas) {
        // Rotate the canvas around the center of the pie by 90 degrees
        // counter clockwise so the pie stars at 12 o'clock.
        canvas.rotate(-90f, getBounds().centerX(), getBounds().centerY());
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawOval(mBoundsF, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawArc(mInnerBoundsF, START_ANGLE, mDrawTo, true, mPaint);

        // Draw inner oval and text on top of the pie (or add any other
        // decorations such as a stroke) here..
        // Don't forget to rotate the canvas back if you plan to add text!
        // ...
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mBoundsF = mInnerBoundsF = new RectF(bounds);
        final int halfBorder = (int) (mPaint.getStrokeWidth()/2f + 0.5f);
        mInnerBoundsF.inset(halfBorder, halfBorder);
    }

    @Override
    protected boolean onLevelChange(int level) {
        final float drawTo = START_ANGLE + ((float)360*level)/100f;
        boolean update = drawTo != mDrawTo;
        mDrawTo = drawTo;
        return update;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return mPaint.getAlpha();
    }
}

What’s new in Android N: Google I/O Keynote 2016

Screen Shot 2016-05-20 at 4.32.34 PM

At the very beginning of this year’s Google’s developer conference, the company announced its latest features and improved services: Android N, Daydream VR, Android Wear 2.0, Google Home and the new Allo and Duo apps were the stars of the opening keynote. 

So let’s focus on the latest news about the newest Android version, and what features we will find later this year in Android N’s final release: 

Performance

Google focused on improving two key areas: graphics and runtime.

Vulkan

Android N adds native support for this modern 3D graphics API, designed to give game developers direct control of the GPU to produce incredible graphics and compute performance.

Because it has a lower CPU usage than OpenGL, game developers are able to squeeze in more effects per frame while still maintaining a high frame rate.

Android Runtime

A new JIT Compiler was added to improve software performance, make installs faster, and reduce the amount of storage needed for apps on the device.

“The new JIT compiler allows installs to be 75% faster. And because it is more selective about which methods it compiles, it reduces the amount of storage by a 50%.” – Dave Burke at Google I/O 2016

JIT performance
The compiler in N performs 30% to 600% faster than major CPU benchmarks.

Security

Google stressed improvements in security through 3 key ways:

File-based encryption

Meaning that Android N encrypts at file level instead of block level, which allows to better isolate and protect individual users of the system.

Media framework hardening 

In N, key subsystems are split into individual SELinux protected processes.

“By improving the security of the media framework we improve the security of the entire device.” – Dave Burke at Google I/O 2016.

Seamless updates

We can say goodbye to the annoying ‘Android is upgrading message’ thanks to this new feature. When an update is available your phone will automatically download the new system image in background, so the next time you power up your phone it will seamlessly switch in to the new software image.

N automatically keeps your phone up to date with the latest version of the system software without you having to do anything.

Note: apparently, seamless updates won’t be able in current Nexus devices. Check the entire post from Android Police here.

Productivity

Multi-tasking and Multi-window

The window management framework was modified to display more than one app at the time.

Quick switch

It is now possible to switch to the previous app you’ve been using by double tapping the recents button.

Split screen

Android N adds native support to display more than one app at the same time. This feature, designed for tablets and phones, works by long tapping on the rec
ents button to enter multi window from where you can launch the second app.

splitscreen

Improved Notifications

Since more than half of the notifications in Android come from messaging apps, Google has decided to add ‘Direct Reply’ feature. This way, a user can quickly reply to a message without having to launch the app to send a response.

Screen Shot 2016-05-19 at 11.46.14 PM

It is also possible to change notifications settings, such as blocking or silencing it, by just long tapping on it.

“To wrap up: We’ve made it faster and more performant with a powerful new JIT Compiler and Vulkan 3D graphics. We are continuing to harden our security and provide the first truly seamless software updates system for mobile. And we are making users more productive with better multi tasking, and brand new multi window support, and improved notifications.” – Dave Burke, VP of Engineering at Google.

summaryPicture

Google expects to launch the final N release later this year, but in case you can’t wait, you can enter the new beta program announced at Keynote hereNote: only available for Nexus 6, Nexus 9, Nexus 5x, Nexus 6p, Nexus Player and Pixel C.

In case you missed the event, you can watch the entire Google I/O Keynote here.