[关闭]
@wangwangheng 2014-12-08T15:32:29.000000Z 字数 4228 阅读 2230

仿iReader切换皮肤进度条

自定义View


效果图

本以为使用paint.setXfermode(new PorterDuffXfermode(Mode.XOR));可以轻松搞定,没想到我对PorterDuffXfermode(参考APIDemos代码,路径/APIDemos/Graphics/Xfermodes)的理解有问题,比较悲催了。最后使用了ClipRect的方式实现了这个东西!

定义属性文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="ProgressView">
  4. <attr name="text" format="string"/>
  5. <attr name="textSize" format="dimension"/>
  6. <attr name="color" format="color"/>
  7. <attr name="progress" format="integer"/>
  8. <attr name="maxProgress" format="integer"/>
  9. <attr name="crossColor" format="color"/>
  10. </declare-styleable>
  11. </resources>

实现代码:

  1. package com.example.testproject;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Rect;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. /**
  11. *
  12. * @author
  13. *
  14. */
  15. public class ProgressView extends View {
  16. /** 最大进度 **/
  17. private int maxProgress;
  18. /** 当前进度 **/
  19. private int progress;
  20. /** 当前显示的文字 **/
  21. private String text;
  22. /** 进度和没有交叉的时候的文字的颜色 **/
  23. private int color;
  24. /** 文字和进度交叉的时候的文字的颜色 **/
  25. private int crossColor;
  26. /** 画进度和没有交叉的时候的文字的Paint **/
  27. private Paint paint;
  28. /** 表示进度的Rect **/
  29. private Rect rect;
  30. public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
  31. super(context, attrs, defStyleAttr);
  32. init(context, attrs);
  33. }
  34. public ProgressView(Context context, AttributeSet attrs) {
  35. super(context, attrs);
  36. init(context, attrs);
  37. }
  38. public ProgressView(Context context) {
  39. super(context);
  40. init(context, null);
  41. }
  42. private void init(Context context, AttributeSet attrs){
  43. /** 得到XML属性 **/
  44. TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
  45. maxProgress = ta.getInt(R.styleable.ProgressView_maxProgress, 100);
  46. progress = ta.getInt(R.styleable.ProgressView_progress, 0);
  47. text = ta.getString(R.styleable.ProgressView_text);
  48. color = ta.getColor(R.styleable.ProgressView_color, Color.GREEN);
  49. crossColor = ta.getColor(R.styleable.ProgressView_crossColor, Color.WHITE);
  50. float textSize = ta.getDimensionPixelOffset(R.styleable.ProgressView_textSize, 20);
  51. ta.recycle();
  52. /** 设置默认的Paint属性 **/
  53. paint = new Paint();
  54. paint.setAntiAlias(true);
  55. paint.setFlags(Paint.ANTI_ALIAS_FLAG);
  56. paint.setStyle(Paint.Style.FILL);
  57. paint.setTextSize(textSize);
  58. paint.setColor(color);
  59. }
  60. @Override
  61. protected void onDraw(Canvas canvas) {
  62. /** 白色背景 **/
  63. canvas.drawColor(Color.WHITE);
  64. /** 恢复颜色 **/
  65. paint.setColor(color);
  66. /** 得到画文字的左上角顶点 **/
  67. int offsetX = (int) ((getWidth() - text.length() * paint.getTextSize()) / 2);
  68. int offsetY = (int) ((getHeight() - paint.getTextSize()) / 2);
  69. /** 画默认文字 **/
  70. canvas.drawText(text, offsetX, offsetY, paint);
  71. /** 画进度 **/
  72. if(rect == null){
  73. rect = new Rect();
  74. rect.left = 0;
  75. rect.top = 0;
  76. rect.bottom = getHeight();
  77. }
  78. rect.right = (int) (getWidth() * progress / (float)maxProgress);
  79. canvas.drawRect(rect, paint);
  80. /** 画交叉的时候的文字 **/
  81. canvas.save();
  82. canvas.clipRect(rect);
  83. paint.setColor(crossColor);
  84. canvas.drawText(text, offsetX, offsetY, paint);
  85. canvas.restore();
  86. }
  87. /**
  88. * 设置最大进度
  89. * @return
  90. */
  91. public int getMaxProgress() {
  92. return maxProgress;
  93. }
  94. /**
  95. * 得到最大进度
  96. * @param maxProgress
  97. */
  98. public void setMaxProgress(int maxProgress) {
  99. this.maxProgress = maxProgress;
  100. invalidate();
  101. }
  102. /**
  103. * 得到当前进度
  104. * @return
  105. */
  106. public int getProgress() {
  107. return progress;
  108. }
  109. /**
  110. * 设置当前进度
  111. * @param progress
  112. */
  113. public void setProgress(int progress) {
  114. this.progress = progress;
  115. invalidate();
  116. }
  117. /**
  118. * 得到显示的文字
  119. * @return
  120. */
  121. public String getText() {
  122. return text;
  123. }
  124. /**
  125. * 设置显示的文字
  126. * @param text
  127. */
  128. public void setText(String text) {
  129. this.text = text;
  130. invalidate();
  131. }
  132. /***
  133. * 设置提示文字的大小
  134. * @param textSize
  135. */
  136. public void setTextSize(int textSize) {
  137. paint.setTextSize(textSize);
  138. invalidate();
  139. }
  140. /***
  141. * 设置进度和没有交叉的时候的文字的颜色
  142. * @param color
  143. */
  144. public void setColor(int color) {
  145. this.color = color;
  146. paint.setColor(color);
  147. invalidate();
  148. }
  149. /**
  150. * 设置进度和文字交叉之后的文字颜色
  151. * @param color
  152. */
  153. public void setCrossColor(int color){
  154. crossColor = color;
  155. invalidate();
  156. }
  157. }

简单测试代码:

  1. package com.example.testproject;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.ViewGroup.LayoutParams;
  6. public class ProgressTextViewActivity extends Activity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,500);
  11. ProgressView view = new ProgressView(ProgressTextViewActivity.this);
  12. view.setText("正在下载...");
  13. view.setTextSize(40);
  14. view.setMaxProgress(10000);
  15. view.setProgress(5000);
  16. view.setColor(Color.parseColor("#FF336699"));
  17. view.setLayoutParams(params);
  18. view.setCrossColor(Color.WHITE);
  19. setContentView(view);
  20. }
  21. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注