io.jawg.osmcontributor.ui.utils.views.customs.ImageProgressBar.java Source code

Java tutorial

Introduction

Here is the source code for io.jawg.osmcontributor.ui.utils.views.customs.ImageProgressBar.java

Source

/**
 * Copyright (C) 2016 eBusiness Information
 *
 * This file is part of OSM Contributor.
 *
 * OSM Contributor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OSM Contributor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OSM Contributor.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.jawg.osmcontributor.ui.utils.views.customs;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;

import com.facebook.drawee.drawable.ProgressBarDrawable;

import io.jawg.osmcontributor.R;

public class ImageProgressBar extends ProgressBarDrawable {
    private float level;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int color;
    private final RectF oval = new RectF();
    private float radius = 100;

    public ImageProgressBar(Context context) {
        this.radius = context.getResources().getDimension(R.dimen.progress_bar_radius);
        this.color = ContextCompat.getColor(context, R.color.colorPrimaryDark);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected boolean onLevelChange(int level) {
        this.level = level;
        invalidateSelf();
        return true;
    }

    @Override
    public void draw(Canvas canvas) {
        oval.set(canvas.getWidth() / 2 - radius, canvas.getHeight() / 2 - radius, canvas.getWidth() / 2 + radius,
                canvas.getHeight() / 2 + radius);

        drawCircle(canvas, level, color);
    }

    private void drawCircle(Canvas canvas, float level, int color) {
        paint.setColor(color);
        float angle;
        angle = 360 / 1f;
        angle = level * angle;
        canvas.drawArc(oval, 0, Math.round(angle), false, paint);
    }
}