Browse Source

烟花消失优化

LAPTOP-K69FCNBP\crius 2 years ago
parent
commit
dfa4fdc2a7
1 changed files with 23 additions and 27 deletions
  1. 23 27
      app/src/main/java/com/xunao/effectdemo/view/FireworkView.java

+ 23 - 27
app/src/main/java/com/xunao/effectdemo/view/FireworkView.java

@@ -6,6 +6,7 @@ import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.util.TypedValue;
 import android.view.View;
 import android.view.animation.DecelerateInterpolator;
@@ -20,9 +21,10 @@ import androidx.annotation.Nullable;
  */
 
 public class FireworkView extends View{
+    String TAG = "FireworkView";
 
     /**
-     * 圆最大半径(心形)
+     * 圆最大半径
      */
     private float mRadius;
     /**
@@ -39,7 +41,7 @@ public class FireworkView extends View{
     private static final int[] dotColors = {0xffdaa9fa, 0xfff2bf4b, 0xffe3bca6, 0xff329aed,
             0xffb1eb99, 0xff67c9ad, 0xffde6bac};
     /**
-     * 4.圆环减消失、心形放大、周围环绕十四圆点
+     * 4.圆环减消失、周围环绕十四圆点
      */
     private static final int RING_DOT__HEART_VIEW = 3;
     /**
@@ -71,8 +73,8 @@ public class FireworkView extends View{
 
     public FireworkView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
-        mRadius = dp2px(20);
-        mCycleTime = 600;
+        mRadius = dp2px(30);
+        mCycleTime = 1800;
         mCenterX = mRadius;
         mCenterY = mRadius;
         mPaint = new Paint();
@@ -94,14 +96,11 @@ public class FireworkView extends View{
         }
     }
 
-    //绘制圆点、圆环、心形
+    //绘制圆点
     private void drawDotWithRing(Canvas canvas, int radius) {
         mPaint.setAntiAlias(true);
 
-        mPaint.setStyle(Paint.Style.STROKE);
         mCurrentPercent = (1f - mCurrentPercent > 1f ? 1f : 1f - mCurrentPercent) * 1f;
-        //用于计算圆环宽度,最小0,与动画进度负相关
-//        mPaint.setStrokeWidth(2 * mRadius * mCurrentPercent);
 
         float innerR = radius - mRadius * mCurrentPercent + dotR;
         double angleB = -Math.PI / 20;
@@ -109,20 +108,18 @@ public class FireworkView extends View{
         offL += dotR / 14;
         rDotL = innerR + offL;
 
-        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
+        mPaint.setStyle(Paint.Style.FILL);
         for (int i = 0; i < 14; i++) {
             mPaint.setColor(dotColors[i % 7]);
             float cx = (float) (rDotL * Math.sin(angleB));
             float cy = (float) (rDotL * Math.cos(angleB));
             if(i % 3 == 0){
-                mPaint.setStrokeWidth(dotR);
                 canvas.drawCircle(cx, cy, dotR, mPaint);
             }else if(i % 3 == 1){
-                mPaint.setStrokeWidth(dotR);
-                canvas.drawText("/", cx, cy, mPaint);
+                mPaint.setTextSize(60);
+                canvas.drawText("÷", cx, cy, mPaint);
             }else{
-                mPaint.setStrokeWidth(dotR);
-                canvas.drawRect(cx - 1, cy - 1, cx + 1, cy + 1, mPaint);
+                canvas.drawRect(cx - 20, cy - 20, cx + 20, cy + 20, mPaint);
             }
 
             angleB += 2 * Math.PI / 14;
@@ -131,14 +128,13 @@ public class FireworkView extends View{
 
     }
 
-    //绘制圆点、心形
+    //绘制圆点
     private void drawDot(Canvas canvas) {
         mPaint.setAntiAlias(true);
-        mPaint.setStyle(Paint.Style.FILL);
 
         double angleB = -Math.PI / 20;
         float dotRL;
-        if (rDotL < 2.6 * mRadius) {//限制圆点的扩散范围
+        if (rDotL < 2.6 * mRadius) {  // 限制圆点的扩散范围
             rDotL += dotR / 14;
         }
         if (!isMax && mCurrentRadius <= 1.1 * mRadius) {
@@ -151,21 +147,20 @@ public class FireworkView extends View{
 
         dotRL = (dotR * (1 - mCurrentPercent) * 4) > dotR ? dotR :
                 (dotR * (1 - mCurrentPercent) * 3);
-        mPaint.setAlpha((int) (255 * (1 - mCurrentPercent)));//圆点逐渐透明
-        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
+        mPaint.setStyle(Paint.Style.FILL);
         for (int i = 0; i < 14; i++) {
             mPaint.setColor(dotColors[i % 7]);
+            // 逐渐透明,setColor自带Alpha值,所以要放在setColor之后才会生效
+            mPaint.setAlpha((int) (255 * (1 - mCurrentPercent)));
             float cx = (float) (rDotL * Math.sin(angleB));
             float cy = (float) (rDotL * Math.cos(angleB));
             if(i % 3 == 0){
-                mPaint.setStrokeWidth(dotRL);
                 canvas.drawCircle(cx, cy, dotRL, mPaint);
             }else if(i % 3 == 1){
-                mPaint.setStrokeWidth(dotRL);
-                canvas.drawText("/", cx, cy, mPaint);
+                mPaint.setTextSize(60);
+                canvas.drawText("÷", cx, cy, mPaint);
             }else{
-                mPaint.setStrokeWidth(dotRL);
-                canvas.drawRect(cx - 1, cy - 1, cx + 1, cy + 1, mPaint);
+                canvas.drawRect(cx - 20, cy - 20, cx + 20, cy + 20, mPaint);
             }
 
             angleB += 2 * Math.PI / 14;
@@ -190,7 +185,7 @@ public class FireworkView extends View{
     }
 
     /**
-     * 点赞的变化效果
+     * 烟花的变化效果
      */
     public void like() {
         if (animatorTime != null && animatorTime.isRunning()) {
@@ -199,14 +194,14 @@ public class FireworkView extends View{
         resetState();
         animatorTime = ValueAnimator.ofInt(0, 1200);
         animatorTime.setDuration(mCycleTime);
-        animatorTime.setInterpolator(new DecelerateInterpolator());//需要随时间匀速变化
+        animatorTime.setInterpolator(new DecelerateInterpolator());  // 减速变化
         animatorTime.start();
         animatorTime.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
             @Override
             public void onAnimationUpdate(ValueAnimator animation) {
                 int animatedValue = (int) animation.getAnimatedValue();
 
-                    if (animatedValue <= 480) {
+                if (animatedValue <= 480) {
                     float percent = calcPercent(1f, 480f, animatedValue);//内环半径增大直至消亡
                     mCurrentPercent = percent;
                     mCurrentRadius = (int) (2 * mRadius);//外环半径不再改变
@@ -239,6 +234,7 @@ public class FireworkView extends View{
         return (current - start) / (end - start);
     }
 
+    // 转换为dp单位
     private float dp2px(int value) {
         return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                 getResources().getDisplayMetrics());