领透酷医云安卓客户端

MaterialProgressBar.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.linkdialy.mobile.kuyicloud.widget;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.PorterDuff;
  8. import android.graphics.PorterDuffXfermode;
  9. import android.graphics.RectF;
  10. import android.util.AttributeSet;
  11. import com.linkdialy.mobile.kuyicloud.utils.SizeUtils;
  12. public class MaterialProgressBar extends CustomView {
  13. final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";
  14. int backgroundColor = Color.parseColor("#1E88E5");
  15. private Context mContext;
  16. public MaterialProgressBar(Context context, AttributeSet attrs) {
  17. super(context, attrs);
  18. mContext = context;
  19. setAttributes(attrs);
  20. }
  21. protected void setAttributes(AttributeSet attrs) {
  22. setMinimumHeight(SizeUtils.convertDp2Px(mContext, 32));
  23. setMinimumWidth(SizeUtils.convertDp2Px(mContext, 32));
  24. int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML, "background", -1);
  25. if (bacgroundColor != -1) {
  26. setBackgroundColor(getResources().getColor(bacgroundColor));
  27. } else {
  28. int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
  29. if (background != -1)
  30. setBackgroundColor(background);
  31. else
  32. setBackgroundColor(Color.parseColor("#1E88E5"));
  33. }
  34. setMinimumHeight(SizeUtils.convertDp2Px(mContext, 3));
  35. }
  36. protected int makePressColor() {
  37. int r = (this.backgroundColor >> 16) & 0xFF;
  38. int g = (this.backgroundColor >> 8) & 0xFF;
  39. int b = (this.backgroundColor >> 0) & 0xFF;
  40. return Color.argb(128, r, g, b);
  41. }
  42. @Override
  43. protected void onDraw(Canvas canvas) {
  44. super.onDraw(canvas);
  45. if (firstAnimationOver == false)
  46. drawFirstAnimation(canvas);
  47. if (cont > 0)
  48. drawSecondAnimation(canvas);
  49. invalidate();
  50. }
  51. float radius1 = 0;
  52. float radius2 = 0;
  53. int cont = 0;
  54. boolean firstAnimationOver = false;
  55. private void drawFirstAnimation(Canvas canvas) {
  56. if (radius1 < getWidth() / 2) {
  57. Paint paint = new Paint();
  58. paint.setAntiAlias(true);
  59. paint.setColor(makePressColor());
  60. radius1 = (radius1 >= getWidth() / 2) ? (float) getWidth() / 2 : radius1 + 1;
  61. canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius1, paint);
  62. } else {
  63. Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
  64. Canvas temp = new Canvas(bitmap);
  65. Paint paint = new Paint();
  66. paint.setAntiAlias(true);
  67. paint.setColor(makePressColor());
  68. temp.drawCircle(getWidth() / 2, getHeight() / 2, getHeight() / 2, paint);
  69. Paint transparentPaint = new Paint();
  70. transparentPaint.setAntiAlias(true);
  71. transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
  72. transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
  73. if (cont >= 50) {
  74. radius2 = (radius2 >= getWidth() / 2) ? (float) getWidth() / 2 : radius2 + 1;
  75. } else {
  76. radius2 = (radius2 >= getWidth() / 2 - SizeUtils.convertDp2Px(mContext, 4)) ? (float) getWidth() / 2 - SizeUtils.convertDp2Px(mContext, 4) : radius2 + 1;
  77. }
  78. temp.drawCircle(getWidth() / 2, getHeight() / 2, radius2, transparentPaint);
  79. canvas.drawBitmap(bitmap, 0, 0, new Paint());
  80. if (radius2 >= getWidth() / 2 - SizeUtils.convertDp2Px(mContext, 4))
  81. cont++;
  82. if (radius2 >= getWidth() / 2)
  83. firstAnimationOver = true;
  84. }
  85. }
  86. int arcD = 1;
  87. int arcO = 0;
  88. float rotateAngle = 0;
  89. int limit = 0;
  90. private void drawSecondAnimation(Canvas canvas) {
  91. if (arcO == limit)
  92. arcD += 6;
  93. if (arcD >= 290 || arcO > limit) {
  94. arcO += 6;
  95. arcD -= 6;
  96. }
  97. if (arcO > limit + 290) {
  98. limit = arcO;
  99. arcO = limit;
  100. arcD = 1;
  101. }
  102. rotateAngle += 4;
  103. canvas.rotate(rotateAngle, getWidth() / 2, getHeight() / 2);
  104. Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
  105. Canvas temp = new Canvas(bitmap);
  106. Paint paint = new Paint();
  107. paint.setAntiAlias(true);
  108. paint.setColor(backgroundColor);
  109. temp.drawArc(new RectF(0, 0, getWidth(), getHeight()), arcO, arcD, true, paint);
  110. Paint transparentPaint = new Paint();
  111. transparentPaint.setAntiAlias(true);
  112. transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
  113. transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
  114. temp.drawCircle(getWidth() / 2, getHeight() / 2, (getWidth() / 2) - SizeUtils.convertDp2Px(mContext, 4), transparentPaint);
  115. canvas.drawBitmap(bitmap, 0, 0, new Paint());
  116. }
  117. public void setBackgroundColor(int color) {
  118. super.setBackgroundColor(getResources().getColor(android.R.color.transparent));
  119. if (isEnabled())
  120. beforeBackground = backgroundColor;
  121. this.backgroundColor = color;
  122. }
  123. }