带文字百分比进度横向进度条

//完整代码

(import package)


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 2017/1/20
 */
public class TextProgressView extends View {

    private float[] radii1;
    private float[] radii2;

public float getProgress() {
    return progress;
}

public void setProgress(float progress) {
    this.progress = progress;
    if (progress > maxProgress) {
        return;
    }
    invalidate();
}

public void setMaxProgress(float maxProgress) {
    this.maxProgress = maxProgress;
}

private float progress;
private float maxProgress;
private int mWidth, mHeight;

public TextProgressView(Context context) {
    this(context, null);
}

public TextProgressView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

public TextProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}


private void init() {
    backgroundPaint = new Paint();
    backgroundPaint.setAntiAlias(true);
    backgroundPaint.setColor(Color.parseColor("#7fDE3D41"));
    progressPaint = new Paint();
    progressPaint.setAntiAlias(true);
    progressPaint.setColor(Color.parseColor("#DE3D41"));
    textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(30);
    textPaint.setColor(Color.WHITE);
    radii1 = new float[]{
            10f, 10f,
            0f, 0f,
            0f, 0f,
            10f, 10f};
    radii2 = new float[]{
            0f, 0f,
            10f, 10f,
            10f, 10f,
            0f, 0f};
}

private Paint progressPaint;
private Paint backgroundPaint;
private Paint textPaint;

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float p = progress / maxProgress;

    /**绘制左边进度**/
    Path leftPath = new Path();
    RectF leftrectF = new RectF(
            0,
            mHeight / 2 - dipToPx(2),
            (mWidth - dipToPx(40)) * p,
            mHeight / 2 + dipToPx(2));

    leftPath.addRoundRect(leftrectF, radii1, Path.Direction.CW);
    canvas.drawPath(leftPath, progressPaint);

    /**绘制进度文字背景**/
    RectF progressrectF = new RectF(
            (mWidth - dipToPx(40)) * p,
            0,
            leftrectF.right + dipToPx(40),
            mHeight);
    canvas.drawRoundRect(
            progressrectF,
            dipToPx(10),
            dipToPx(10),
            progressPaint);

    /**绘制右边进度**/
    Path rightPath = new Path();
    RectF rightrectF = new RectF(
            progressrectF.right,
            mHeight / 2 - dipToPx(2),
            mWidth,
            mHeight / 2 + dipToPx(2));
    rightPath.addRoundRect(rightrectF, radii2, Path.Direction.CW);
    canvas.drawPath(rightPath, backgroundPaint);

    /**绘制进度文字**/
    String text = (int) progress + "%";
    Rect rect = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), rect);
    canvas.drawText(
            text,
            progressrectF.left + progressrectF.width() / 2 - rect.width() / 2,
            mHeight - rect.height() / 2,
            textPaint);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mWidth = getMeasuredWidth();
    mHeight = getMeasuredHeight();
}


private int dipToPx(int dip) {
    float scale = getContext().getResources().getDisplayMetrics().density;
    return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
}
复制代码

//效果图

转载于:https://juejin.im/post/5b04dff251882542836e61fa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值