Android 简单实现自定义进度条

1.效果图

Android 简单实现自定义进度条_第1张图片Android 简单实现自定义进度条_第2张图片

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



    
        
        
        
            
            
        
    

<3>在布局控件中引用




    
    

    



<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) {

    }
}

 

你可能感兴趣的:(软件,Android,自定义控件)