Android 自定义 view(斜线进度条)– 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

Android 自定义 view(斜线进度条)

Android控件 sinvader 14881℃ 0评论

效果如下:

PercentText

 

代码:

自定义的 view 的代码:

  1. package cn.sumile.percentlayout;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Paint.FontMetricsInt;
  9. import android.graphics.Path;
  10. import android.graphics.Rect;
  11. import android.util.AttributeSet;
  12. import android.util.TypedValue;
  13. import android.view.View;
  14.  
  15. public class PercentTextLayout extends View {
  16. private String mText;
  17.  
  18. public String getText() {
  19. return mText;
  20. }
  21.  
  22. public void setText(String mText) {
  23. this.mText = mText;
  24. postInvalidate();
  25. }
  26.  
  27. public int getDefaultPercent() {
  28. return mDefaultPercent;
  29. }
  30.  
  31. public void setDefaultPercent(int mDefaultPercent) {
  32. this.mDefaultPercent = mDefaultPercent;
  33. postInvalidate();
  34. }
  35.  
  36. public int getBackColorDefault() {
  37. return mBackColorDefault;
  38. }
  39.  
  40. public void setBackColorDefault(int mBackColorDefault) {
  41. this.mBackColorDefault = mBackColorDefault;
  42. }
  43.  
  44. public int getBackColor() {
  45. return mBackColor;
  46. }
  47.  
  48. public void setBackColor(int mBackColor) {
  49. this.mBackColor = mBackColor;
  50. }
  51.  
  52. public int getTextColor() {
  53. return mTextColor;
  54. }
  55.  
  56. public void setTextColor(int mTextColor) {
  57. this.mTextColor = mTextColor;
  58. }
  59.  
  60. public int getTextSize() {
  61. return mTextSize;
  62. }
  63.  
  64. public void setTextSize(int mTextSize) {
  65. this.mTextSize = mTextSize;
  66. }
  67.  
  68. public int getTextAlignLeft() {
  69. return mTextAlignLeft;
  70. }
  71.  
  72. public void setTextAlignLeft(int mTextAlignLeft) {
  73. this.mTextAlignLeft = mTextAlignLeft;
  74. }
  75.  
  76. private int mDefaultPercent;
  77. private int mBackColorDefault;
  78. private int mBackColor;
  79. private int mTextColor;
  80. private int mTextSize;
  81. private Rect mBound;
  82. private int mTextAlignLeft;
  83.  
  84. public PercentTextLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  85. super(context, attrs, defStyleAttr);
  86. initView(context, attrs, defStyleAttr);
  87. }
  88.  
  89. /**
  90. * @param context
  91. * 上下文对象
  92. * @param attrs
  93. * 参数
  94. * @param defStyleAttr
  95. */
  96. private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
  97. TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PercentText, defStyleAttr, 0);
  98. int n = a.getIndexCount();
  99. for (int i = 0; i < n; i++) {
  100. int attr = a.getIndex(i);
  101. switch (attr) {
  102. case R.styleable.PercentText_backColor:
  103. mBackColor = a.getColor(attr, Color.GRAY);
  104. break;
  105. case R.styleable.PercentText_backColorDefault:
  106. mBackColorDefault = a.getColor(attr, Color.WHITE);
  107. break;
  108. case R.styleable.PercentText_defaultPercent:
  109. mDefaultPercent = a.getInt(attr, 50);
  110. break;
  111. case R.styleable.PercentText_text:
  112. mText = a.getString(attr);
  113. break;
  114. case R.styleable.PercentText_textSize:
  115. mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
  116. break;
  117. case R.styleable.PercentText_textColor:
  118. mTextColor = a.getColor(attr, Color.BLACK);
  119. break;
  120. case R.styleable.PercentText_alignLeft:
  121. mTextAlignLeft = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
  122. break;
  123. }
  124. }
  125. a.recycle();
  126. }
  127.  
  128. public PercentTextLayout(Context context, AttributeSet attrs) {
  129. this(context, attrs, 0);
  130. }
  131.  
  132. public PercentTextLayout(Context context) {
  133. this(context, null);
  134. }
  135.  
  136. @Override
  137. protected void onDraw(Canvas canvas) {
  138. // 绘制背景
  139. Paint paint = new Paint();
  140. paint.setColor(mBackColorDefault);
  141. canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
  142. // 测量字体
  143. mBound = new Rect();
  144. paint.getTextBounds(mText, 0, mText.length(), mBound);
  145. // 绘制已完成的进度
  146. Path path = new Path();
  147. path.moveTo(0, 0);
  148. if (mDefaultPercent < 0) {
  149. mDefaultPercent = 0;
  150. }
  151. if (mDefaultPercent > 100) {
  152. mDefaultPercent = 100;
  153. }
  154. if (mDefaultPercent >= 2 && mDefaultPercent <= 98) {
  155. // point2
  156. int p1_width = (mDefaultPercent + 2) * getWidth() / 100;
  157. path.lineTo(p1_width, 0);
  158. // point3
  159. int p3_width = (mDefaultPercent - 2) * getWidth() / 100;
  160. path.lineTo(p3_width, getHeight());
  161. } else if (mDefaultPercent < 2) {
  162. int p1_width = (mDefaultPercent) * getWidth() / 100;
  163. path.lineTo(p1_width, 0);
  164. } else if (mDefaultPercent > 98) {
  165. // point2
  166. path.lineTo(getWidth(), 0);
  167. // point3
  168. int p3_width = (mDefaultPercent) * getWidth() / 100;
  169. path.lineTo(p3_width, getHeight());
  170. }
  171. // point4
  172. path.lineTo(0, getHeight());
  173. path.close();
  174. paint.setColor(mBackColor);
  175. canvas.drawPath(path, paint);
  176. // 绘制文字
  177. paint.setColor(mTextColor);
  178. paint.setTextSize(mTextSize);
  179. paint.setStrokeWidth(3);
  180. FontMetricsInt fontMetrics = paint.getFontMetricsInt();
  181. // paint2.setTextSize(20);
  182. int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
  183. canvas.drawText(mText, mTextAlignLeft, baseline, paint);
  184. invalidate();
  185. }
  186. }

定义属性的代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <declare-styleable name="PercentText">
  5. <attr name="text" format="string"></attr>//要显示的文字
  6. <attr name="backColorDefault" format="color"></attr>//默认背景颜色,也就是没有完成的颜色
  7. <attr name="backColor" format="color"></attr>//完成后的颜色
  8. <attr name="defaultPercent" format="integer"></attr>//默认进度
  9. <attr name="textSize" format="dimension"></attr>//文字大小
  10. <attr name="textColor" format="color"></attr>//文字颜色
  11. <attr name="alignLeft" format="dimension"></attr>//文字相对于左面的边距
  12. </declare-styleable>
  13.  
  14. </resources>

注意事项

  • 在 xml 中使用时注意加上这一句,要加在头上(cn.sumile.percentlayout 这个替换为你的 package):
  1. xmlns:pt="http://schemas.android.com/apk/res/cn.sumile.percentlayout"
  • xmlns:pt 里面的 pt 字符可以替换为任意字符,但是这里写什么,下面就得怎么写:
  1. pt:defaultPercent="70"//这里前面就是用了 pt

xml 文件中进行使用

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. xmlns:pt="http://schemas.android.com/apk/res/cn.sumile.percentlayout"
  4. android:id="@+id/container"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="cn.sumile.percentlayout.MainActivity"
  8. tools:ignore="MergeRootFrame" >
  9.  
  10. <cn.sumile.percentlayout.PercentTextLayout
  11. android:id="@+id/ptl"
  12. android:layout_width="500dip"
  13. android:layout_height="50dip"
  14. android:layout_marginLeft="50dip"
  15. android:layout_marginRight="50dip"
  16. android:layout_marginTop="50dip"
  17. pt:alignLeft="20dip"
  18. pt:backColor="@android:color/holo_green_light"
  19. pt:backColorDefault="@android:color/darker_gray"
  20. pt:defaultPercent="70"
  21. pt:text="点个赞!"
  22. pt:textColor="@android:color/white"
  23. pt:textSize="20sp" />
  24.  
  25. </FrameLayout>
测试代码下载
¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » Android 自定义 view(斜线进度条)


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » Android 自定义 view(斜线进度条)

喜欢 (1)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址