如何让动画只显示一次圆圈

Posted

技术标签:

【中文标题】如何让动画只显示一次圆圈【英文标题】:How to get a circle to display only once for the animation 【发布时间】:2015-04-28 07:00:27 【问题描述】:

我有一个应用程序,它显示多个圆圈并将半径从 0 设置为 300。我遇到的问题是它显示多个圆圈,每个圆圈的半径逐渐增加。我只想要一个增加半径的圆圈。这是我的代码。

public class SplashLaunch extends View
    Handler cool = new Handler();
    DrawingView v;
    Paint newPaint = new Paint();
    int randomWidthOne = 0;
    int randomHeightOne = 0;
    private float radiusOne = 300;
    final int redColorOne = Color.RED;
    final int greenColorOne = Color.GREEN;
    private static int lastColorOne;
    ObjectAnimator radiusAnimator;
    private final Random theRandom = new Random();
    public SplashLaunch(Context context) 
        super(context);
        // TODO Auto-generated constructor stub
    

    private final Runnable circleUpdater = new Runnable() 
        @Override 
        public void run() 
            lastColorOne = theRandom.nextInt(2) == 1 ? redColorOne : greenColorOne;
            newPaint.setColor(lastColorOne); 
            startAnimation(100);
            cool.postDelayed(this, 1000);
            invalidate();
        
    ;

    @Override
    protected void onAttachedToWindow()
        super.onAttachedToWindow();
        cool.post(circleUpdater);
    
    protected void onDetachedFromWindow()
        super.onDetachedFromWindow();
        cool.removeCallbacks(circleUpdater);
    

    @Override
    protected void onDraw(Canvas canvas) 
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        if(theRandom == null)
            randomWidthOne =(int) (theRandom.nextInt((int) Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
            randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
        else 
            randomWidthOne =(int) (theRandom.nextInt((int) Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
            randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
        

        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusOne, newPaint);
    




    public void setRadiusOne(float value)
        this.radiusOne = value;
        invalidate();
    


    public int startAnimation(int animationDuration) 

        if (radiusAnimator == null || !radiusAnimator.isRunning()) 

            // Define what value the radius is supposed to have at specific time values
            Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
            Keyframe kf2 = Keyframe.ofFloat(0.5f, 180f);
            Keyframe kf1 = Keyframe.ofFloat(1f, 360f);

            // If you pass in the radius, it will be calling setRadius method, so make sure you have it!!!!!
            PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("radiusOne", kf0, kf1, kf2);
            radiusAnimator = ObjectAnimator.ofPropertyValuesHolder(this, pvhRotation);
            radiusAnimator.setInterpolator(new LinearInterpolator());
            radiusAnimator.setDuration(animationDuration);
            radiusAnimator.start();
        
        else 
            Log.d("Circle", "I am already running!");
        
        return animationDuration;
    

    public void stopAnimation() 
        if (radiusAnimator != null) 
            radiusAnimator.cancel();
            radiusAnimator = null;
        
    

    public boolean getAnimationRunning() 
        return radiusAnimator != null && radiusAnimator.isRunning();
    


【问题讨论】:

如果您的应用最小 sdk 版本 >= HoneyComb,请使用 developer.android.com/reference/android/animation/Animator.html 或其任何子类。无需实现处理程序。如果您使用处理程序,您可能会遇到内存泄漏。 我不明白。 ^ 这对我一点帮助都没有 【参考方案1】:

尝试使用

canvas.drawColor(Color.WHITE);

用于在 super.onDraw(canvas) 之后清除整个视图显示;

【讨论】:

圆圈是在屏幕上随机绘制的,所以这会在顶部绘制一个半径为 x 的圆圈,然后在屏幕底部绘制另一个半径大于 x 的圆圈 奇怪的是实际上什么也没做

以上是关于如何让动画只显示一次圆圈的主要内容,如果未能解决你的问题,请参考以下文章

iOS 16 SwiftUI 列表(List)footer 中 ProgressView “转圈圈”动画只显示一次的解决

iOS 16 SwiftUI 列表(List)footer 中 ProgressView “转圈圈”动画只显示一次的解决

iOS 16 SwiftUI 列表(List)footer 中 ProgressView “转圈圈”动画只显示一次的解决

如何让 JQuery 在旋转动画的循环内等待

如何围绕一个角色制作动画

WPF DataTrigger 动画只触发一次