java - 如何让一个圆圈在动画中只显示一次

标签 java android animation view android-animation

我有一个应用程序,可以显示多个圆圈,并将半径从 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();
    }

}

最佳答案

尝试使用

canvas.drawColor(Color.WHITE);

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

关于java - 如何让一个圆圈在动画中只显示一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912436/

相关文章:

java - 斜杠上的 Gson MalformedJsonException

java - Android MaterialCardView 切角样式

java - 创建 Alarmobject 时应用程序崩溃 - Alarmmanager

javascript - 在 Canvas 上平滑地将对象从 A 移动到 B

ios - Spritebuilder 如何以编程方式更改 CCNode 的动画(CCB 文件)?

javascript - 使用线性渐变(纯 CSS)实现动画跷跷板效果

java - 从文本文件中选择

java - 带有 MySQL 的 JDBC ResultSet 忽略 ResultSet 类型

java - 与 org.iq80.snappy 存在潜在冲突

android - strings.xml 中的双引号是做什么用的?