一个简单的自定义控件
public class MyProgressBar extends View {
private int pressure;
private int max;
public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyProgressBar(Context context) {
super(context);
}
public void setPressure(int pressure) {
this.pressure = pressure;
postInvalidate();
}
public void setMax(int max) {
this.max = max;
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
if(pressure < 50){
paint.setColor(Color.GREEN);
}
else if(pressure < 80){
paint.setColor(0xffff8800);
}
else {
paint.setColor(Color.RED);
}
canvas.drawRect(10, 10 + max - pressure, 30, 10 + max, paint);
}
}