Android 简单实现自定义进度条

1.效果图

progress小于90的时候为绿色,大于90的时候变为红色。

2.步骤:

<1>创建一个自定义view

public class MyProgressBar extends View {
    private Paint mPaint = null ;
    private int max=100;
    private int progress=0;
    private int type=1;
    private int NORMAL_TYPE=1;
    private int ALERT_TYPE=2;
    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyProgressBar); //与属性名称一致
        max = array.getInteger(R.styleable.MyProgressBar_max, 100);//第一个是传递参数,第二个是默认值
        progress = array.getInteger(R.styleable.MyProgressBar_progress, 0);
        type=array.getInt(R.styleable.MyProgressBar_type, 1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Point left_top=new Point(0,0);
        Point right_bottom=new Point(getWidth(),getHeight());
        double rate=(double)progress/(double)max;
        drawProgressBar(canvas, left_top, right_bottom, rate);
    }
    public void setProgress(int progress){
        this.progress=progress;
        invalidate();//使得onDraw重绘
    }
    private void drawProgressBar(Canvas canvas,Point left_top,Point right_bottom,double rate){
        int width=1;
        int rad=10;
        mPaint.setColor(Color.BLACK);//画边框
        mPaint.setStyle(Paint.Style.STROKE);//设置空心
        mPaint.setStrokeWidth(width);
        RectF rectF = new RectF(left_top.x,left_top.y,right_bottom.x,right_bottom.y);
        canvas.drawRoundRect(rectF, rad, rad, mPaint);

        mPaint.setColor(Color.GREEN);//画progress bar

        if (type==ALERT_TYPE) {
            if (rate>0.9)
            mPaint.setColor(Color.RED);
        }
        mPaint.setStyle(Paint.Style.FILL);
        int x_end=(int)(right_bottom.x*rate);
        RectF rectF2 = new RectF(left_top.x+width,left_top.y+width,x_end-width,right_bottom.y-width);
        canvas.drawRoundRect(rectF2, rad, rad, mPaint);
    }
}

<2>添加属性文件

在values文件夹中添加控件属性文件attrs

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyProgressBar">
        <attr name="progress" format="integer" />
        <attr name="max" format="integer" />
        <attr name="type">
            <enum name="NORMAL_TYPE" value="1" />
            <enum name="ALERT_TYPE" value="2" />
        </attr>
    </declare-styleable>
</resources>

<3>在布局控件中引用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:my_attrs="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true"
        />
    <com.innovpower.mycustomprogressbar.CustomView.MyProgressBar
        android:layout_width="150dp"
        android:layout_height="25dp"
        android:layout_below="@+id/textView"
        android:layout_marginTop="10dp"
        android:id="@+id/myProgressBar"
        android:layout_centerHorizontal="true"
        my_attrs:max="100"
        my_attrs:progress="0"
        my_attrs:type="ALERT_TYPE"


        />

    <SeekBar
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:max="100"
        android:layout_below="@+id/myProgressBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:progress="0"
        android:indeterminate="false" />


</RelativeLayout>

<4>在程序中使用该控件

package com.innovpower.mycustomprogressbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

import com.innovpower.mycustomprogressbar.CustomView.MyProgressBar;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
    private TextView textView;
    private MyProgressBar myProgressBar;
    private SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView);
        myProgressBar=(MyProgressBar)findViewById(R.id.myProgressBar);
        seekBar=(SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textView.setText(progress+"");
        myProgressBar.setProgress(progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值