MyLikeView.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package com.xunao.effectdemo.view;
  2. import android.animation.ValueAnimator;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12. import android.view.animation.LinearInterpolator;
  13. import androidx.annotation.NonNull;
  14. import androidx.annotation.Nullable;
  15. /**
  16. * author : 程中强
  17. * e-mail : 740479946@qq.com
  18. * date : 2022/8/2615:10
  19. * desc :
  20. * version: 1.0
  21. */
  22. public class MyLikeView extends View {
  23. private Context context;
  24. private int ivResore;
  25. private int dotNum;
  26. private Paint mCircle;
  27. private Paint mBitmap;
  28. private int centerX;
  29. private int centerY;
  30. private Bitmap bitmap;
  31. private Float mBimt;
  32. private String canvasType;
  33. private Float circleR;
  34. private Float circleR0;
  35. private Float dotR;
  36. private int circleColor;
  37. private int[] dotColors;
  38. private int width;
  39. private int height;
  40. private Handler handler;
  41. public MyLikeView(@NonNull Context context) {
  42. this(context, (AttributeSet)null);
  43. }
  44. public MyLikeView(@NonNull Context context, @Nullable AttributeSet attrs) {
  45. this(context, attrs, -1);
  46. }
  47. public MyLikeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  48. super(context, attrs, defStyleAttr);
  49. this.dotNum = 7;
  50. this.mBimt = 0.0F;
  51. this.canvasType = "Bitmap";
  52. this.circleR = 0.0F;
  53. this.circleR0 = 0.0F;
  54. this.dotR = 10.0F;
  55. this.dotColors = new int[]{-2446854, -868533, -1852250, -13460755, -5117031, -9975379, -2200660};
  56. this.handler = new Handler() {
  57. public void handleMessage(Message msg) {
  58. super.handleMessage(msg);
  59. if (msg.what == 0) {
  60. MyLikeView.this.postInvalidate();
  61. }
  62. }
  63. };
  64. // TypedArray typedArray = context.obtainStyledAttributes(attrs, styleable.MyLikeView);
  65. // if (typedArray != null) {
  66. // this.ivResore = typedArray.getResourceId(styleable.MyLikeView_bitmap, mipmap.heart);
  67. // this.circleColor = typedArray.getColor(styleable.MyLikeView_circleColor, ContextCompat.getColor(context, color.colorPrimary));
  68. // this.dotNum = typedArray.getInteger(styleable.MyLikeView_dotNum, 7);
  69. // }
  70. //
  71. // typedArray.recycle();
  72. this.init(context);
  73. this.setOnClickListener(new OnClickListener() {
  74. public void onClick(View view) {
  75. MyLikeView.this.start();
  76. }
  77. });
  78. }
  79. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  80. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  81. if (this.bitmap != null) {
  82. this.width = this.bitmap.getWidth() * 4;
  83. this.height = this.bitmap.getWidth() * 4;
  84. this.setMeasuredDimension(this.width, this.height);
  85. }
  86. }
  87. private void init(Context context) {
  88. this.context = context;
  89. this.mCircle = new Paint();
  90. this.mCircle.setAntiAlias(true);
  91. this.mBitmap = new Paint();
  92. this.mBitmap.setAntiAlias(true);
  93. BitmapFactory.Options options = new BitmapFactory.Options();
  94. options.inScaled = false;
  95. this.bitmap = BitmapFactory.decodeResource(context.getResources(), this.ivResore, options);
  96. }
  97. protected void onDraw(Canvas canvas) {
  98. super.onDraw(canvas);
  99. this.centerX = this.getWidth() / 2;
  100. this.centerY = this.getHeight() / 2;
  101. String var2 = this.canvasType;
  102. byte var3 = -1;
  103. switch(var2.hashCode()) {
  104. case 68905:
  105. if (var2.equals("Dot")) {
  106. var3 = 2;
  107. }
  108. break;
  109. case 1990057295:
  110. if (var2.equals("Bitmap")) {
  111. var3 = 0;
  112. }
  113. break;
  114. case 2018617584:
  115. if (var2.equals("Circle")) {
  116. var3 = 1;
  117. }
  118. }
  119. switch(var3) {
  120. case 0:
  121. this.canvasHeart(canvas);
  122. break;
  123. case 1:
  124. this.canvasCircle(canvas);
  125. break;
  126. case 2:
  127. this.canvasDot(canvas);
  128. }
  129. }
  130. private void canvasDot(Canvas canvas) {
  131. this.canvasHeart(canvas);
  132. if (this.mBimt == 36.0F) {
  133. float angleA = 0.0F;
  134. float angleB = -0.15707964F;
  135. for(int i = 0; i < this.dotNum; ++i) {
  136. this.mCircle.setColor(this.dotColors[i]);
  137. canvas.drawCircle((float)((double)(this.circleR + 5.0F) * Math.sin((double)angleB)), (float)((double)(this.circleR + 5.0F) * Math.cos((double)angleB)), this.dotR, this.mCircle);
  138. angleA = (float)((double)angleA + 6.283185307179586D / (double)this.dotNum);
  139. canvas.drawCircle((float)((double)this.circleR * Math.sin((double)angleA)), (float)((double)this.circleR * Math.cos((double)angleA)), this.dotR - 2.0F, this.mCircle);
  140. angleB = (float)((double)angleB + 6.283185307179586D / (double)this.dotNum);
  141. }
  142. }
  143. }
  144. private void canvasCircle(Canvas canvas) {
  145. this.mCircle.setColor(this.circleColor);
  146. canvas.drawCircle((float)this.centerX, (float)this.centerY, this.circleR0, this.mCircle);
  147. }
  148. private void canvasHeart(Canvas canvas) {
  149. canvas.translate((float)this.centerX, (float)this.centerY);
  150. canvas.drawBitmap(this.bitmap, (float)(-this.bitmap.getWidth() / 2), (float)(-this.bitmap.getWidth() / 2), this.mBitmap);
  151. }
  152. public void start() {
  153. this.circleR = (float)this.bitmap.getWidth();
  154. this.circleR0 = 0.0F;
  155. this.dotR = 6.0F;
  156. ValueAnimator animator = ValueAnimator.ofFloat(new float[]{0.0F, 72.0F});
  157. animator.setDuration(500L);
  158. animator.setInterpolator(new LinearInterpolator());
  159. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  160. public void onAnimationUpdate(ValueAnimator animation) {
  161. Float value = (Float)animation.getAnimatedValue();
  162. if (value <= 36.0F) {
  163. MyLikeView.this.mBimt = value;
  164. MyLikeView.this.canvasType = "Circle";
  165. } else {
  166. MyLikeView.this.canvasType = "Dot";
  167. MyLikeView.this.circleR = MyLikeView.this.circleR + 1.0F;
  168. MyLikeView.this.dotR = MyLikeView.this.dotR - 0.2F;
  169. }
  170. MyLikeView.this.circleR0 = MyLikeView.this.circleR0 + 1.0F;
  171. if (value == 72.0F) {
  172. MyLikeView.this.mBimt = value / 2.0F;
  173. try {
  174. Thread.sleep(50L);
  175. (new Thread(MyLikeView.this.new progrssThread())).start();
  176. } catch (InterruptedException var4) {
  177. var4.printStackTrace();
  178. }
  179. }
  180. MyLikeView.this.invalidate();
  181. }
  182. });
  183. animator.start();
  184. }
  185. public void setIvResore(int ivResore) {
  186. this.ivResore = ivResore;
  187. }
  188. public void setCircleColor(int circleColor) {
  189. this.circleColor = circleColor;
  190. }
  191. public void setDotNum(int dotNum, int[] dotColors) {
  192. this.dotNum = dotNum;
  193. this.dotColors = dotColors;
  194. }
  195. public class progrssThread implements Runnable {
  196. public progrssThread() {
  197. }
  198. public void run() {
  199. while(MyLikeView.this.dotR > 0.0F) {
  200. try {
  201. Thread.sleep(9L);
  202. Message msg = Message.obtain();
  203. msg.what = 0;
  204. MyLikeView.this.dotR = MyLikeView.this.dotR - 0.3F;
  205. if (MyLikeView.this.circleR < 36.0F) {
  206. MyLikeView.this.circleR = MyLikeView.this.circleR + 1.0F;
  207. }
  208. MyLikeView.this.handler.sendMessage(msg);
  209. } catch (InterruptedException var2) {
  210. var2.printStackTrace();
  211. }
  212. }
  213. }
  214. }
  215. }