如何获得一个圈为动画显示只有一次 [英] How to get a circle to display only once for the animation

查看:124
本文介绍了如何获得一个圈为动画显示只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,显示多个圆圈和动画半径从0到300。我遇到的问题是,它显示多个圆圈缓慢增加,每个圆圈的半径。我想只有一个圆圈绘制,增加半径。这是我的code。

 公共类SplashLaunch扩展视图{
    处理器酷=新的处理程序();
    DrawingView伏;
    油漆newPaint =新的油漆();
    INT randomWidthOne = 0;
    INT randomHeightOne = 0;
    私人浮动radiusOne = 300;
    最终诠释redColorOne = Color.RED;
    最终诠释greenColorOne = Color.GREEN;
    私有静态诠释lastColorOne;
    ObjectAnimator radiusAnimator;
    私人最终随机theRandom =新的随机();
    公共SplashLaunch(上下文的背景下){
        超(上下文);
        // TODO自动生成构造函数存根
    }

    私人最终可运行circleUpdater =新的Runnable(){
        @覆盖
        公共无效的run(){
            lastColorOne = theRandom.nextInt(2)== 1? redColorOne:greenColorOne;
            newPaint.setColor(lastColorOne);
            startAnimation(100);
            cool.postDelayed(这一点,1000);
            无效();
        }
    };

    @覆盖
    保护无效onAttachedToWindow(){
        super.onAttachedToWindow();
        cool.post(circleUpdater);
    }
    保护无效onDetachedFromWindow(){
        super.onDetachedFromWindow();
        cool.removeCallbacks(circleUpdater);
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        // TODO自动生成方法存根
        super.onDraw(画布);

        如果(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))));
        }其他 {
            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);
    }




    公共无效setRadiusOne(浮点值){
        this.radiusOne =价值;
        无效();
    }


    公众诠释startAnimation(INT animationDuration){

        如果(radiusAnimator == NULL ||!radiusAnimator.isRunning()){

            //定义什么样的价值半径应该具有在特定的时间值
            关键帧kf0 = Keyframe.ofFloat(0F,0F);
            关键帧KF2 = Keyframe.ofFloat(0.5F,180F);
            关键帧KF1 = Keyframe.ofFloat(1F,360F);

            //如果传递半径,它会调用setRadius方法,所以请确保您有它!!!!!
            PropertyValues​​Holder pvhRotation = PropertyValues​​Holder.ofKeyframe(radiusOne,kf0,KF1,KF2);
            radiusAnimator = ObjectAnimator.ofPropertyValues​​Holder(这一点,pvhRotation);
            radiusAnimator.setInterpolator(新LinearInterpolator());
            radiusAnimator.setDuration(animationDuration);
            radiusAnimator.start();
        }
        其他 {
            Log.d(圆,我已经跑!);
        }
        返回animationDuration;
    }

    公共无效stopAnimation(){
        如果(radiusAnimator!​​= NULL){
            radiusAnimator.cancel();
            radiusAnimator = NULL;
        }
    }

    公共布尔getAnimationRunning(){
        返回radiusAnimator = NULL和放大器;!&安培; radiusAnimator.isRunning();
    }

}
 

解决方案

尝试使用

  canvas.drawColor(Color.WHITE);
 

有关super.onDraw(画布)后清零全景显示;

I have an app that displays multiple circles and animates the radius from 0 to 300. The problem I am having is that it displays multiple circles slowly increasing the radius with each circle. I want only one circle drawn that increases the radius. Here is my code.

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();
    }

}

解决方案

Try using

canvas.drawColor(Color.WHITE);

for clearing whole view display after super.onDraw(canvas);

这篇关于如何获得一个圈为动画显示只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆