Android Custom ui--Battery

Source: Internet
Author: User

First Look,

Here's a look at the code:

[HTML]View Plaincopy
  1. /**
  2. *
  3. */
  4. Package Com.example.batterydemo;
  5. Import Android.content.Context;
  6. Import Android.graphics.Canvas;
  7. Import Android.graphics.Color;
  8. Import Android.graphics.Paint;
  9. Import Android.graphics.Paint.Style;
  10. Import Android.graphics.RectF;
  11. Import Android.util.AttributeSet;
  12. Import Android.view.View;
  13. /**
  14. * @author Kince
  15. * @category Custom View battery
  16. * @time 2014.2.13
  17. * @qq 543781062
  18. *
  19. */
  20. public class Batteryview extends View {
  21. /**
  22. * Brush Information
  23. */
  24. Private Paint Mbatterypaint;
  25. Private Paint Mpowerpaint;
  26. private float Mbatterystroke = 2f;
  27. /**
  28. * High Screen width
  29. */
  30. private int measurewidth;
  31. private int measureheigth;
  32. /**
  33. *
  34. * Battery Parameters
  35. */
  36. private float mbatteryheight = 30f;//battery height
  37. private float mbatterywidth = 60f;//battery width
  38. private float mcapheight = 15f;
  39. private float mcapwidth = 5f;
  40. /**
  41. *
  42. * Battery charge
  43. */
  44. private float mpowerpadding = 1;
  45. private float mpowerheight = Mbatteryheight-mbatterystroke
  46. -Mpowerpadding * 2; The height of the battery body
  47. private float mpowerwidth = mbatterywidth-mbatterystroke-mpowerpadding
  48. * Total width of 2;//battery body
  49. private float mPower = 0f;
  50. /**
  51. *
  52. * Rectangle
  53. */
  54. Private RECTF Mbatteryrect;
  55. Private RECTF Mcaprect;
  56. Private RECTF Mpowerrect;
  57. Public Batteryview (Context context) {
  58. Super (context);
  59. Initview ();
  60. }
  61. Public Batteryview (context context, AttributeSet Attrs) {
  62. Super (context, attrs);
  63. Initview ();
  64. }
  65. Public Batteryview (context context, AttributeSet attrs, int defstyleattr) {
  66. Super (context, attrs, defstyleattr);
  67. Initview ();
  68. }
  69. public void Initview () {
  70. /**
  71. * Set the battery brush
  72. */
  73. mbatterypaint = new Paint ();
  74. Mbatterypaint.setcolor (Color.gray);
  75. Mbatterypaint.setantialias (TRUE);
  76. Mbatterypaint.setstyle (Style.stroke);
  77. Mbatterypaint.setstrokewidth (Mbatterystroke);
  78. /**
  79. * Set the Power brush
  80. */
  81. mpowerpaint = new Paint ();
  82. Mpowerpaint.setcolor (color.red);
  83. Mpowerpaint.setantialias (TRUE);
  84. Mpowerpaint.setstyle (Style.fill);
  85. Mpowerpaint.setstrokewidth (Mbatterystroke);
  86. /**
  87. * Set the battery rectangle
  88. */
  89. mbatteryrect = new RECTF (mcapwidth, 0, Mbatterywidth, mbatteryheight);
  90. /**
  91. * Set the battery cover rectangle
  92. */
  93. mcaprect = new RECTF (0, (mbatteryheight-mcapheight)/2, mcapwidth,
  94. (mbatteryheight-mcapheight)/2 + mcapheight);
  95. /**
  96. * Set the charge rectangle
  97. */
  98. mpowerrect = new RECTF (mcapwidth + mbatterystroke/2 + mpowerpadding
  99. + Mpowerwidth * ((100f-mpower)/100f),//need to adjust the left position
  100. Mpowerpadding + MBATTERYSTROKE/2,//need to take into account the width of the brush
  101. Mbatterywidth-mpowerpadding * 2, MBATTERYSTROKE/2
  102. + mpowerpadding + mpowerheight);
  103. }
  104. @Override
  105. protected void OnDraw (canvas canvas) {
  106. Super.ondraw (canvas);
  107. Canvas.save ();
  108. Canvas.translate (MEASUREWIDTH/2, MEASUREHEIGTH/2);
  109. Canvas.drawroundrect (mbatteryrect, 2f, 2f, mbatterypaint); Drawing a battery contour takes into account the width of the brush
  110. Canvas.drawroundrect (mcaprect, 2f, 2f, mbatterypaint);//Draw the battery cover
  111. Canvas.drawrect (Mpowerrect, mpowerpaint);//Draw charge
  112. Canvas.restore ();
  113. }
  114. @Override
  115. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  116. measurewidth = measurespec.getsize (widthmeasurespec);
  117. measureheigth = measurespec.getsize (heightmeasurespec);
  118. Setmeasureddimension (Measurewidth, measureheigth);
  119. }
  120. /**]
  121. * @category set the battery charge
  122. * @param power
  123. */
  124. public void Setpower (float power) {
  125. mPower = power;
  126. if (MPower < 0) {
  127. mPower = 0;
  128. }
  129. mpowerrect = new RECTF (mcapwidth + mbatterystroke/2 + mpowerpadding
  130. + Mpowerwidth * ((100f-mpower)/100f),//need to adjust the left position
  131. Mpowerpadding + MBATTERYSTROKE/2,//need to take into account the width of the brush
  132. Mbatterywidth-mpowerpadding * 2, MBATTERYSTROKE/2
  133. + mpowerpadding + mpowerheight);
  134. Invalidate ();
  135. }
  136. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.