From 6d2d0275fbf1e2931251ffb0341317b4c7dea2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=AE=8F=E5=AE=87?= Date: Mon, 14 Dec 2020 20:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=BD=97=E7=9B=98?= =?UTF-8?q?=E7=9A=84=E6=97=8B=E8=BD=AC=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../small/map/SmallMapDirectionView.java | 29 +++++--- .../module/small/map/SmallMapService.java | 5 +- .../animation/DirectionRotateAnimation.java | 66 +++++++++++++++++++ 3 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/animation/DirectionRotateAnimation.java diff --git a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java index 14a2a62ad1..0e5764968c 100644 --- a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java +++ b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java @@ -3,14 +3,13 @@ package com.mogo.module.small.map; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; -import android.view.animation.Animation; -import android.view.animation.DecelerateInterpolator; -import android.view.animation.RotateAnimation; +import android.view.animation.LinearInterpolator; import android.widget.ImageView; import androidx.annotation.Nullable; import com.mogo.module.common.view.RoundLayout; +import com.mogo.module.small.map.animation.DirectionRotateAnimation; /** * 小地图的方向View @@ -21,6 +20,8 @@ import com.mogo.module.common.view.RoundLayout; public class SmallMapDirectionView extends RoundLayout { private ImageView mIvMapBorder; + private DirectionRotateAnimation mRotateAnimation; + private int lastAngle = 0; public SmallMapDirectionView(Context context) { this(context, null); @@ -38,6 +39,7 @@ public class SmallMapDirectionView extends RoundLayout { private void initView(Context context) { LayoutInflater.from(context).inflate(R.layout.module_small_map_view, this); mIvMapBorder = findViewById(R.id.ivMapBorder); + mRotateAnimation = new DirectionRotateAnimation(context, null); } @@ -47,18 +49,25 @@ public class SmallMapDirectionView extends RoundLayout { * @param angle 角度 0 - 359度旋转,相对于自身中心位置 */ public void changeAngle(int angle) { - Animation mRotateAnimation = new RotateAnimation( - 0, angle, - Animation.RELATIVE_TO_SELF, 0.5f, - Animation.RELATIVE_TO_SELF, 0.5f); + int tempAngle = angle; + if (angle <= 180) { + tempAngle = angle; + } else { + tempAngle = -(360 - tempAngle); + } + + mRotateAnimation.setFromDegrees(lastAngle); + mRotateAnimation.setToDegrees(tempAngle); + //设置线性插值,可以解决旋转一圈后卡顿问题 - mRotateAnimation.setInterpolator(new DecelerateInterpolator()); + mRotateAnimation.setInterpolator(new LinearInterpolator()); //设置旋转一圈时间 - mRotateAnimation.setDuration(1000); + mRotateAnimation.setDuration(300); //控件动画结束时是否保持动画最后的状态 mRotateAnimation.setFillAfter(true); - mIvMapBorder.startAnimation(mRotateAnimation); + // 刷新最后一次角度 + lastAngle = tempAngle; } } diff --git a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapService.java b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapService.java index 881efae6c5..de26ba6776 100644 --- a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapService.java +++ b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapService.java @@ -15,6 +15,8 @@ import com.mogo.module.common.machinevision.IMachineVisionInterface; import com.mogo.module.common.wm.WindowManagerView; import com.mogo.utils.logger.Logger; +import java.util.Random; + /** * @author donghongyu * @date 12/10/20 1:35 PM @@ -90,7 +92,8 @@ public class SmallMapService extends Service { mSmallMapDirectionView.postDelayed(new Runnable() { @Override public void run() { - mSmallMapDirectionView.changeAngle(-60); + Random random = new Random(); + mSmallMapDirectionView.changeAngle(random.nextInt(360)); } }, 1000); diff --git a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/animation/DirectionRotateAnimation.java b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/animation/DirectionRotateAnimation.java new file mode 100644 index 0000000000..ba84112955 --- /dev/null +++ b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/animation/DirectionRotateAnimation.java @@ -0,0 +1,66 @@ +package com.mogo.module.small.map.animation; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.animation.Animation; +import android.view.animation.Transformation; + +/** + * 方向罗盘旋转动画 + * @author donghongyu + * @date 12/14/20 7:46 PM + */ +public class DirectionRotateAnimation extends Animation { + private float mFromDegrees; + private float mToDegrees; + + private int mPivotXType = Animation.RELATIVE_TO_SELF; + private int mPivotYType = Animation.RELATIVE_TO_SELF; + private float mPivotXValue = 0.5f; + private float mPivotYValue = 0.5f; + + private float mPivotX; + private float mPivotY; + + + public DirectionRotateAnimation(Context context, AttributeSet attrs) { + super(context, attrs); + initializePivotPoint(); + } + + public void setFromDegrees(float fromDegrees) { + mFromDegrees = fromDegrees; + } + + public void setToDegrees(float toDegrees) { + mToDegrees = toDegrees; + } + + private void initializePivotPoint() { + if (mPivotXType == ABSOLUTE) { + mPivotX = mPivotXValue; + } + if (mPivotYType == ABSOLUTE) { + mPivotY = mPivotYValue; + } + } + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime); + float scale = getScaleFactor(); + + if (mPivotX == 0.0f && mPivotY == 0.0f) { + t.getMatrix().setRotate(degrees); + } else { + t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale); + } + } + + @Override + public void initialize(int width, int height, int parentWidth, int parentHeight) { + super.initialize(width, height, parentWidth, parentHeight); + mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth); + mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight); + } +}