【问题标题】:Show ImageView with Circular growing animation in Android(Java)在 Android(Java) 中显示带有循环生长动画的 ImageView
【发布时间】:2021-08-18 11:22:05
【问题描述】:

我有一个圆形星形图像,想要实现如下所示的类似动画。

我尝试使用给定的链接 https://developer.android.com/training/animation/reveal-or-hide-view 使用“createCircularReveal”“真实或隐藏视图”,并尝试组合多个(“翻译”和“旋转”)动画,但不幸的是没有运气。

谁能帮我用 android(Java) 动画实现它?

谢谢

【问题讨论】:

    标签: android animation imageview android-animation android-imageview


    【解决方案1】:

    这是执行此操作的示例代码:

    ProgressImageView.java

    public class ProgressImageView extends AppCompatImageView {
    
        private final Path path = new Path();
        private float progress;
    
        public ProgressImageView(@NonNull Context context) {
            super(context);
        }
    
        public ProgressImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ProgressImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (progress >= 0f && progress < 1f) {
                final int width = getWidth();
                final int height = getHeight();
                final int xCenter = width / 2;
                final int yCenter = height / 2;
                path.rewind();
                path.moveTo(xCenter, yCenter);
                path.arcTo(0, 0, width, height, 270f /* start */, 360f * progress, false);
                path.close();
                canvas.clipPath(path);
            }
            super.onDraw(canvas);
        }
    
        public void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
            this.progress = progress;
            invalidate();
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec,
                                 int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            final int width = getMeasuredWidth();
            final int height = getMeasuredHeight();
            final int size = Math.min(width, height);
            setMeasuredDimension(size, size);
        }
    
    }
    

    动画:

    ImageView imageView = findViewById(R.id.progress_img);
    
    final Animator progressAnimator = ObjectAnimator.ofPropertyValuesHolder(
            imageView,
            PropertyValuesHolder.ofFloat("progress", 0f, 1f));
    progressAnimator.setInterpolator(new LinearInterpolator());
    progressAnimator.setDuration(3_000);
    
    final Animator scaleAnimator = ObjectAnimator.ofPropertyValuesHolder(
            imageView,
            PropertyValuesHolder.ofFloat(View.SCALE_X, 1f, 1.1f, 1f),
            PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f, 1.1f, 1f)
    );
    scaleAnimator.setInterpolator(new BounceInterpolator());
    scaleAnimator.setDuration(2_000);
    
    final AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playSequentially(progressAnimator, scaleAnimator);
    animatorSet.start();
    

    【讨论】:

    • 感谢上面的代码。我尝试过,但不幸的是,只有缩放动画有效。如果您分享上述 Android Studio 项目,我将不胜感激。
    • 首先检查图像剪辑是否在没有动画师的情况下工作,例如通过调用方法setProgress(0.5f)
    • 我只是检查了一下,它也不适用于直接调用。如果你喜欢,我可以分享我的 android 项目。
    • 是的,如果可以的话
    • 您可以通过以下链接查看。 drive.google.com/file/d/1y1HBeIwRT6a4ZDu7lJPo66X9Y4FiOK9v/… 确认画布路径,我尝试使用 Paint(蓝色)绘制此路径,确认使用 canvas.drawPath 的绘图画布运行良好,但不幸的是 canvas.clipPath 运行不正常。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2014-10-30
    • 2023-03-15
    • 2014-07-10
    相关资源
    最近更新 更多