android|动态设置Progress值和颜色

最近碰到一个需求,需要实时显示指南针信号的干扰情况,这个干扰值一直在变化,如果超过某个值就显示红色,如果中等的话,就是黄色,低的话就是绿色,所以仿了一个程序,来实现这个功能,用的是自定义的progress
1.效果图
android|动态设置Progress值和颜色
文章图片

2.主界面布局


3.主界面,线程和设置颜色和进度条的值
package com.example.administrator.testz; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { // 参考网址 https://github.com/Airsaid/ZProgressBar 详细信息请看这里,我可以看的这个例子修改来的程序 private int num = 0; private ZProgressBar mProgressBar; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mProgressBar = (ZProgressBar) findViewById(R.id.progressBar); textView = (TextView) findViewById(R.id.textView); mProgressBar.setMax(10); //设置最大进度 mProgressBar.setDefBackgroundColor(R.color.black); //设置背景颜色new Thread(new Runnable() { @Override public void run() { while (true) { try { //线程的作用就是起一个变化的作用,看的更加直观,模拟数值的变化 Thread.sleep(1000); runOnUiThread(new Runnable() {// 3788100123 @Override public void run() { //核心代码在这里 num = (int) (Math.random() * 10 + 1); mProgressBar.setAnimProgress(num); //设置进度 //设置进度颜色red-2870516 green -8795045yellow -994507 // 如果小于3 绿色如果3-6 为黄色如果大于6 为红色 if (num < 3) { mProgressBar.setProgressColor(-8795045); } else if (num < 6) { mProgressBar.setProgressColor(-994507); } else { mProgressBar.setProgressColor(-2870516); } textView.setText("" + num); } }); } catch (InterruptedException e) { e.printStackTrace(); }} } }).start(); }} /** * -13318089 green-8921493 true-8795045 * -1757165red-2870516 * -657588 yellow136,setProgressColor: -271779=35,setProgressColor: -2166956 * * * red-2870516 * green -8795045 * yellow -994507 */

4.自定义的progress
package com.example.administrator.testz; import android.animation.ValueAnimator; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.ClipDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.RoundRectShape; import android.util.AttributeSet; import android.view.Gravity; import android.widget.ProgressBar; /** * @author Airsaid * @github https://github.com/airsaid * @date 2017/5/19 * @desc 一个可动态配置颜色、弧度,带动画的自定义 ProgressBar。 */ public class ZProgressBar extends ProgressBar {private final Context mContext; /** 背景颜色(默认为灰色) */ private int mBackgroundColor= Color.LTGRAY; /** 进度条颜色(默认为红色) */ private int mProgressColor = Color.RED; /** 背景弧度(默认为 0) */ private float mRadius = 0f; /** 动画时长(默认 500 毫秒) */ private int mDuration = 500; public ZProgressBar(Context context) { this(context, null); }public ZProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); }public ZProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; initAttrs(attrs); createDrawable(); }private void initAttrs(AttributeSet attrs) { TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.ZProgressBar); mBackgroundColor = a.getColor(R.styleable.ZProgressBar_zpb_backgroundColor, mBackgroundColor); mProgressColor = a.getColor(R.styleable.ZProgressBar_zpb_progressColor, mProgressColor); mRadius = a.getDimension(R.styleable.ZProgressBar_zpb_radius, mRadius); mDuration = a.getInt(R.styleable.ZProgressBar_zpb_duration, mDuration); a.recycle(); }private void createDrawable(){ Drawable[] layers = new Drawable[2]; Drawable background = makeBackground(); Drawable progress = makeProgress(); ClipDrawable clip = new ClipDrawable(progress , Gravity.LEFT, ClipDrawable.HORIZONTAL); layers[0] = background; layers[1] = clip; LayerDrawable layer = new LayerDrawable(layers); layer.setId(0, android.R.id.background); layer.setId(1, android.R.id.progress); setProgressDrawable(layer); }/** * 设置带动画进度 * @param progress 进度 */ public void setAnimProgress(int progress){ startAnimator(progress); }private void startAnimator(int progress){ ValueAnimator animator = ValueAnimator.ofInt(0, progress); animator.setDuration(mDuration); animator.start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int value = https://www.it610.com/article/(int) valueAnimator.getAnimatedValue(); setProgress(value); } }); }/** * 设置默认背景颜色 * @param color 色值 */ public void setDefBackgroundColor(int color){ this.mBackgroundColor = color; createDrawable(); }/** * 设置进度条颜色 * @param color 色值 */ public void setProgressColor(int color){ this.mProgressColor = color; createDrawable(); }/** * 设置背景弧度 * @param radius 弧度 */ public void setRadius(float radius){ this.mRadius = radius; createDrawable(); }/** * 设置动画时长 * @param duration 时长 */ public void setDuration(int duration){ this.mDuration = duration; }/** * 生成背景样式 drawable * @return drawable */ private Drawable makeBackground(){ return createShape(mRadius, mBackgroundColor); }/** * 生成 Progress 样式 drawable * @return drawable */ private Drawable makeProgress(){ return createShape(mRadius, mProgressColor); }/** * 根据 radius 和 color 来创建 ShapeDrawable * @param radius 弧度 * @param color颜色 * @return drawable */ private Drawable createShape(float radius, int color){ ShapeDrawable shape = new ShapeDrawable(); // 设置弧度 radius = dp2px(radius); float[] outerRadii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius}; RoundRectShape roundShape = new RoundRectShape(outerRadii, null, null); shape.setShape(roundShape); // 设置颜色 shape.getPaint().setColor(color); return shape; }private int dp2px(float dpValue){ return (int)(dpValue * (mContext.getResources().getDisplayMetrics().density) + 0.5f); } }

5.进度条的属性attrs,在values里面,有一个attrs.xml文件,添加下面的代码,没有的,自己创建


【android|动态设置Progress值和颜色】end

    推荐阅读