美文网首页
Android自定义View——圆形进度条

Android自定义View——圆形进度条

作者: lkmc2 | 来源:发表于2017-12-26 22:46 被阅读51次

近期学习到了Android圆形自定义View的开发,特此记录。

效果图.png

CustomProgressBar.java

package linchange.com.customprogressbar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * Created by lkmc2 on 2017/12/26.
 * 自定义圆形进度条
 */
public class CustomProgressBar extends View {

    public float progress = 0; //进度
    private RectF arcRect; //弧度矩阵
    private Paint mPaint; //画笔
    private int centerX; //屏幕中心横坐标
    private int centerY; //屏幕中心纵坐标
    private float radius = dpToPixel(100); //圆形半径

    public CustomProgressBar(Context context) {
        super(context);
        init(); //初始化参数
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(); //初始化参数
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(); //初始化参数
    }

    /**
     * 初始化参数
     */
    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //初始化画笔

        mPaint.setStrokeWidth(dpToPixel(15)); //设置画笔宽度
        mPaint.setStrokeCap(Paint.Cap.ROUND); //设置画笔边缘为圆形
        mPaint.setTextAlign(Paint.Align.CENTER); //设置画笔文字中心对齐
        mPaint.setTextSize(dpToPixel(40)); //设置画笔字体大小

        arcRect = new RectF(); //初始化弧度矩阵
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        centerX = getWidth() / 2; //获取屏幕中心横坐标
        centerY = getHeight() / 2; //屏幕中心纵坐标

        arcRect.set(centerX - radius, centerY -radius,
                centerX + radius, centerY + radius);
        mPaint.setColor(Color.parseColor("#E61C49")); //设置画笔颜色为深粉红色
        mPaint.setStyle(Paint.Style.STROKE); //设置画笔空心填充
        //绘制弧形
        canvas.drawArc(arcRect, 130, progress * 2.8f, false, mPaint);

        mPaint.setStyle(Paint.Style.FILL); //设置画笔实心填充
        mPaint.setColor(Color.BLACK); //设置画笔颜色为黑色

        String text = (int)progress + "%"; //获取进度提示文字
        //绘制文字
        canvas.drawText(text, centerX, centerY - (mPaint.ascent() - mPaint.descent()) / 2, mPaint);
    }

    /**
     * 将dp值转换成像素
     * @param dp dp值
     * @return 对应的像素
     */
    public static float dpToPixel(float dp) {
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); //获取屏幕矩阵
        return dp * metrics.density; //将dp值转换成对应的像素
    }

    /**
     * 获取进度
     * @return 当前进度
     */
    public float getProgress() {
        return progress;
    }

    /**
     * 设置进度
     * @param progress 新的进度
     */
    public void setProgress(float progress) {
        this.progress = progress;
        invalidate(); //重绘画面
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="linchange.com.customprogressbar.MainActivity">

    <!--圆形进度条-->
    <linchange.com.customprogressbar.CustomProgressBar
        android:id="@+id/customProgressBar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <!--动画启动按钮-->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:text="开始动画"
        android:textSize="18sp" />

</LinearLayout>

MainActivity.java

package linchange.com.customprogressbar;

import android.animation.ObjectAnimator;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private CustomProgressBar customProgressBar; //圆形进度条
    private Button button; //进度条提示文字

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

        //绑定视图
        customProgressBar = (CustomProgressBar) findViewById(R.id.customProgressBar);
        button = (Button) findViewById(R.id.button);

        //设置点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //初始化对象动画
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(customProgressBar, "progress", 0, 100);
                objectAnimator.setDuration(1000); //设置动画时长
                objectAnimator.setInterpolator(new FastOutLinearInInterpolator()); //设置插值器
                objectAnimator.start(); //启动动画
            }
        });

    }
}

相关文章

网友评论

      本文标题:Android自定义View——圆形进度条

      本文链接:https://www.haomeiwen.com/subject/gotygxtx.html