First Look,
Here's a look at the code:
[HTML]View Plaincopy
- /**
- *
- */
- Package Com.example.batterydemo;
- Import Android.content.Context;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Paint;
- Import Android.graphics.Paint.Style;
- Import Android.graphics.RectF;
- Import Android.util.AttributeSet;
- Import Android.view.View;
- /**
- * @author Kince
- * @category Custom View battery
- * @time 2014.2.13
- * @qq 543781062
- *
- */
- public class Batteryview extends View {
- /**
- * Brush Information
- */
- Private Paint Mbatterypaint;
- Private Paint Mpowerpaint;
- private float Mbatterystroke = 2f;
- /**
- * High Screen width
- */
- private int measurewidth;
- private int measureheigth;
- /**
- *
- * Battery Parameters
- */
- private float mbatteryheight = 30f;//battery height
- private float mbatterywidth = 60f;//battery width
- private float mcapheight = 15f;
- private float mcapwidth = 5f;
- /**
- *
- * Battery charge
- */
- private float mpowerpadding = 1;
- private float mpowerheight = Mbatteryheight-mbatterystroke
- -Mpowerpadding * 2; The height of the battery body
- private float mpowerwidth = mbatterywidth-mbatterystroke-mpowerpadding
- * Total width of 2;//battery body
- private float mPower = 0f;
- /**
- *
- * Rectangle
- */
- 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 () {
- /**
- * Set the battery brush
- */
- mbatterypaint = new Paint ();
- Mbatterypaint.setcolor (Color.gray);
- Mbatterypaint.setantialias (TRUE);
- Mbatterypaint.setstyle (Style.stroke);
- Mbatterypaint.setstrokewidth (Mbatterystroke);
- /**
- * Set the Power brush
- */
- mpowerpaint = new Paint ();
- Mpowerpaint.setcolor (color.red);
- Mpowerpaint.setantialias (TRUE);
- Mpowerpaint.setstyle (Style.fill);
- Mpowerpaint.setstrokewidth (Mbatterystroke);
- /**
- * Set the battery rectangle
- */
- mbatteryrect = new RECTF (mcapwidth, 0, Mbatterywidth, mbatteryheight);
- /**
- * Set the battery cover rectangle
- */
- mcaprect = new RECTF (0, (mbatteryheight-mcapheight)/2, mcapwidth,
- (mbatteryheight-mcapheight)/2 + mcapheight);
- /**
- * Set the charge rectangle
- */
- mpowerrect = new RECTF (mcapwidth + mbatterystroke/2 + mpowerpadding
- + Mpowerwidth * ((100f-mpower)/100f),//need to adjust the left position
- Mpowerpadding + MBATTERYSTROKE/2,//need to take into account the width of the brush
- 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); Drawing a battery contour takes into account the width of the brush
- Canvas.drawroundrect (mcaprect, 2f, 2f, mbatterypaint);//Draw the battery cover
- Canvas.drawrect (Mpowerrect, mpowerpaint);//Draw charge
- Canvas.restore ();
- }
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- measurewidth = measurespec.getsize (widthmeasurespec);
- measureheigth = measurespec.getsize (heightmeasurespec);
- Setmeasureddimension (Measurewidth, measureheigth);
- }
- /**]
- * @category set the battery charge
- * @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),//need to adjust the left position
- Mpowerpadding + MBATTERYSTROKE/2,//need to take into account the width of the brush
- Mbatterywidth-mpowerpadding * 2, MBATTERYSTROKE/2
- + mpowerpadding + mpowerheight);
- Invalidate ();
- }
- }
The code has been commented out more clearly, we look at it, do not know how to look at the API.
Android Custom ui--Battery