在自定义圆形进度条类中使用到attrs.xml,出现多个自定义类引用多个属性,运行时attrs.xml报了错误,其实就是你的attrs.xml中有同名的属性(比如颜色的name),修改一下属性名称就可以了:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--圆弧进度条-->
<declare-styleable name="TasksCompletedView">
<attr name="radius" format="dimension"/>
<attr name="strokeWidth" format="dimension"/>
<attr name="circleColor" format="color"/>
<attr name="ringColor" format="color"/>
<attr name="ringBgColor" format="color"/>
</declare-styleable>
<!--圆弧进度条2-->
<declare-styleable name="circleProgressBar">
<attr name="circleWidth" format="dimension" />
<attr name="betaAngle" format="integer" />
<attr name="firstColor" format="color" />
<attr name="secondColor" format="color" />
</declare-styleable>
<!--圆弧进度条3-->
<declare-styleable name="CircleProgressView">
<!--画笔宽度-->
<attr name="progress_width" format="dimension" />
<!--画笔颜色-->
<attr name="progress_color" format="color" />
<!--加载进度起始位置-->
<attr name="location_start" format="enum">
<enum name="left" value="1" />
<enum name="top" value="2" />
<enum name="right" value="3" />
<enum name="bottom" value="4" />
</attr>
</declare-styleable>
<!--圆弧进度条4-->
<declare-styleable name="CircularProgressView">
<attr name="ringWidth" format="dimension" />
<attr name="ringColors" format="color" />
<attr name="progressTitle" format="string" />
</declare-styleable>
</resources>
自定义圆形进度条类:
package com.apeng.utils;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
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.view.View;
import android.view.animation.LinearInterpolator;
import com.apeng.ffmpegandroiddemo.R;
/**
* 普通环形进度条
*/
public class CircleProgressBar3 extends View {
private int mCurrent;//当前进度
private Paint mBgPaint;//背景弧线paint
private Paint mProgressPaint;//进度Paint
private float mProgressWidth;//进度条宽度
private int mProgressColor = Color.RED;//进度条颜色
private int locationStart;//起始位置
private float startAngle;//开始角度
private ValueAnimator mAnimator;
private int mMaxStepNum;//默认最大步数(最大值)
public static String GOAL_STEP;
public CircleProgressBar3(Context context) {
this(context, null);
}
public CircleProgressBar3(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleProgressBar3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
locationStart = typedArray.getInt(R.styleable.CircleProgressView_location_start, 1);
mProgressWidth = typedArray.getDimension(R.styleable.CircleProgressView_progress_width, dp2px(context, 4));
mProgressColor = typedArray.getColor(R.styleable.CircleProgressView_progress_color, mProgressColor);
typedArray.recycle();
//背景圆弧
mBgPaint = new Paint();
mBgPaint.setAntiAlias(true);
mBgPaint.setStrokeWidth(mProgressWidth);
mBgPaint.setStyle(Paint.Style.STROKE);
mBgPaint.setColor(Color.parseColor("#eaecf0"));
mBgPaint.setStrokeCap(Paint.Cap.ROUND);
//进度圆弧
mProgressPaint = new Paint();
mProgressPaint.setAntiAlias(true);
mProgressPaint.setStyle(Paint.Style.STROKE);
mProgressPaint.setStrokeWidth(mProgressWidth);
mProgressPaint.setColor(mProgressColor);
mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
//进度条起始角度
if (locationStart == 1) {//左
startAngle = -180;
} else if (locationStart == 2) {//上
startAngle = -90;
} else if (locationStart == 3) {//右
startAngle = 0;
} else if (locationStart == 4) {//下
startAngle = 90;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width < height ? width : height;
setMeasuredDimension(size, size);
}
/**
* oval // 绘制范围
* startAngle // 开始角度
* sweepAngle // 扫过角度
* useCenter // 是否使用中心
*/
@Override
protected void onDraw(Canvas canvas) {
//绘制背景圆弧
RectF rectF = new RectF(mProgressWidth / 2, mProgressWidth / 2, getWidth() - mProgressWidth / 2, getHeight() - mProgressWidth / 2);
canvas.drawArc(rectF, 0, 360, false, mBgPaint);
//绘制当前进度
float sweepAngle = 360 * mCurrent / mMaxStepNum;
canvas.drawArc(rectF, startAngle, sweepAngle, false, mProgressPaint);
}
public int getCurrent() {
return mCurrent;
}
/**
* 设置进度
*
* @param current
*/
public void setCurrent(int current) {
mCurrent = current;
invalidate();
}
/**
* @param stepNum
*/
public void setMaxStepNum(int stepNum) {
mMaxStepNum = stepNum;
GOAL_STEP = String.valueOf(mMaxStepNum);
}
private int tCurrent = -1;
/**
* 动画效果
*
* @param current 精度条进度:0-100
* @param duration 动画时间
*/
public void startAnimProgress(int current, int duration) {
mAnimator = ValueAnimator.ofInt(0, current);
mAnimator.setDuration(duration);
mAnimator.setInterpolator(new LinearInterpolator());
mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int current = (int) animation.getAnimatedValue();
if (tCurrent != current) {
tCurrent = current;
setCurrent(current);
if (mOnAnimProgressListener != null)
mOnAnimProgressListener.valueUpdate(current);
}
}
});
mAnimator.start();
}
public interface OnAnimProgressListener {
void valueUpdate(int progress);
}
private OnAnimProgressListener mOnAnimProgressListener;
/**
* 监听进度条进度
*
* @param onAnimProgressListener
*/
public void setOnAnimProgressListener(OnAnimProgressListener onAnimProgressListener) {
mOnAnimProgressListener = onAnimProgressListener;
}
public void destroy() {
if (mAnimator != null) {
mAnimator.cancel();
}
}
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lh2="http://schemas.android.com/apk/res/com.apeng.ffmpegandroiddemo"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/_6FA0F8"
tools:ignore="ResAuto">
<RelativeLayout
android:id="@+id/rl"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.apeng.utils.CircleProgressBar3
android:id="@+id/color_progress_view"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="0" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_below="@+id/rl"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_margin="12dp"
android:text="开始" />
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_margin="12dp"
android:text="重置" />
</LinearLayout>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#778899"
android:max="50" />
</RelativeLayout>
主函数代码:
package com.apeng.ffmpegandroiddemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import com.apeng.utils.CircleProgressBar3;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import java.util.List;
/**
* @author sgf
* https://github.com/Hellobird/CircleSeekBar-For-Android
*/
public class CircleProgressBar3Activity extends AppCompatActivity implements View.OnClickListener{
private CircleProgressBar3 circle_progress;
private TextView tv_progress;
private Button btn_start;
private Button btn_reset;
private SeekBar seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circleprogressbar3);
getPermissions();
initView();
}
private void initView() {
//自定义圆形进度条
btn_start = (Button) findViewById(R.id.btn_start);
btn_reset = (Button) findViewById(R.id.btn_reset);
circle_progress = (CircleProgressBar3) findViewById(R.id.color_progress_view);
tv_progress = (TextView) findViewById(R.id.tv_progress);
circle_progress.setMaxStepNum(200);//设置最大值
btn_start.setOnClickListener(this);
btn_reset.setOnClickListener(this);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setMax(200);//手动控制progress
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (fromUser)
{
circle_progress.setCurrent(progress);
// color_progress_view.setProgress(progress); //不使用动画
// circleProgressBar.setProgress(progress, true); // 使用数字过渡动画
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (circle_progress != null) {
circle_progress.destroy();
}
}
@Override
protected void onResume() {
super.onResume();
}
/**
* @dec 一句代码搞定权限问题
* @author apeng
* @date 2018/10/31 10:54
*/
public void getPermissions() {
XXPermissions.with(this)
.constantRequest() //可设置被拒绝后继续申请,直到用户授权或者永久拒绝
.permission("android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE")
.request(new OnPermission() {
@Override
public void hasPermission(List<String> granted, boolean isAll) {
}
@Override
public void noPermission(List<String> denied, boolean quick) {
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_start:
//开锁执行动画效果
circle_progress.startAnimProgress(200, 1200);
//监听进度条进度
circle_progress.setOnAnimProgressListener(new CircleProgressBar3.OnAnimProgressListener() {
@Override
public void valueUpdate(int progress) {
tv_progress.setText(String.valueOf(progress));
}
});
break;
case R.id.btn_reset:
circle_progress.setCurrent(0);
tv_progress.setText("0");
break;
}
}
}
上面是一个完整的案例,复制即可实现效果。