转载请标明出处:http://blog.csdn.net/donkor_/article/details/53175903
前言:本文提供一个Android开发中可能会使用到电池控件view,有水平和垂直两个方向,同时根据电池电量更改电池中的电量颜色。首先看下效果图:
以下为实现过程:
1.在values目录下新建attrs,添加所需要的名字啊,包括可以更改的电池排列方向,电池颜色,电池电量。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.创建BatteryView文件继承View,在View的构造方法中,获取我们需要的自定义样式。重写onMesure,onDraw方法。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
3.在布局文件中声明我们的VIEW
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
4.在Activity中加入一个线程,使电池无限循环,营造出一个正在充电的状态,这样就大功告成。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
CSDN下载地址:http://download.csdn.net/detail/donkor_/9683749
public class BatteryView extends View {
/**
* 画笔信息
*/
private Paint mBatteryPaint;
private Paint mPowerPaint;
private float mBatteryStroke = 2f;
/**
* 屏幕高宽
*/
private int measureWidth;
private int measureHeigth;
/**
*
* 电池参数
*/
private float mBatteryHeight = 30f; // 电池的高度
private float mBatteryWidth = 60f; // 电池的宽度
private float mCapHeight = 15f;
private float mCapWidth = 5f;
/**
*
* 电池电量
*/
private float mPowerPadding = 1;
private float mPowerHeight = mBatteryHeight - mBatteryStroke
- mPowerPadding * 2; // 电池身体的高度
private float mPowerWidth = mBatteryWidth - mBatteryStroke - mPowerPadding
* 2;// 电池身体的总宽度
private float mPower = 0f;
/**
*
* 矩形
*/
private RectF mBatteryRect;
private RectF mCapRect;
private RectF mPowerRect;
public BatteryView(Context context) {
super(context);
initView();
}
public BatteryView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public BatteryView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
public void initView() {
/**
* 设置电池画笔
*/
mBatteryPaint = new Paint();
mBatteryPaint.setColor(Color.GRAY);
mBatteryPaint.setAntiAlias(true);
mBatteryPaint.setStyle(Style.STROKE);
mBatteryPaint.setStrokeWidth(mBatteryStroke);
/**
* 设置电量画笔
*/
mPowerPaint = new Paint();
mPowerPaint.setColor(Color.RED);
mPowerPaint.setAntiAlias(true);
mPowerPaint.setStyle(Style.FILL);
mPowerPaint.setStrokeWidth(mBatteryStroke);
/**
* 设置电池矩形
*/
mBatteryRect = new RectF(mCapWidth, 0, mBatteryWidth, mBatteryHeight);
/**
* 设置电池盖矩形
*/
mCapRect = new RectF(0, (mBatteryHeight - mCapHeight) / 2, mCapWidth,
(mBatteryHeight - mCapHeight) / 2 + mCapHeight);
/**
* 设置电量矩形
*/
mPowerRect = new RectF(mCapWidth + mBatteryStroke / 2 + mPowerPadding
+ mPowerWidth * ((100f - mPower) / 100f), // 需要调整左边的位置
mPowerPadding + mBatteryStroke / 2, // 需要考虑到 画笔的宽度
mBatteryWidth - mPowerPadding * 2, mBatteryStroke / 2
+ mPowerPadding + mPowerHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(measureWidth / 2, measureHeigth / 2);
canvas.drawRoundRect(mBatteryRect, 2f, 2f, mBatteryPaint); // 画电池轮廓需要考虑 画笔的宽度
canvas.drawRoundRect(mCapRect, 2f, 2f, mBatteryPaint);// 画电池盖
canvas.drawRect(mPowerRect, mPowerPaint);// 画电量
canvas.restore();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureWidth = MeasureSpec.getSize(widthMeasureSpec);
measureHeigth = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(measureWidth, measureHeigth);
}
/**]
* @category 设置电池电量
* @param power
*/
public void setPower(float power) {
mPower = power;
if (mPower < 0) {
mPower = 0;
}
mPowerRect = new RectF(mCapWidth + mBatteryStroke / 2 + mPowerPadding
+ mPowerWidth * ((100f - mPower) / 100f), // 需要调整左边的位置
mPowerPadding + mBatteryStroke / 2, // 需要考虑到 画笔的宽度
mBatteryWidth - mPowerPadding * 2, mBatteryStroke / 2
+ mPowerPadding + mPowerHeight);
invalidate();
}
}