for loop不会"成长"圆圈。我需要"成长"一个圆圈

时间:2015-05-05 06:23:48

标签: java android animation android-canvas geometry

我正在尝试编写一个Android应用程序,它可以在任意位置上一遍又一遍地绘制Circles。这已经在我的代码中完成了。我的下一个目标是慢慢为这些圈子制作动画,使它们成长为#34;到屏幕上。基本上将圆的半径从0增加到300非常快我通过创建这样的for循环来做到这一点。

for(int i = 0;i< 300; i++){
            canvas.drawCircle(randomWidthOne, randomHeightOne,i, newPaint);
        }

不幸的是,这并没有达到预期的效果。相反,它只显示最终半径为300的圆圈。这是我绘制圆圈的类的代码。如果课堂上有任何干扰我想要完成的任务,请告诉我。

public class SplashLaunch extends View{
    Handler cool = new Handler();
    DrawingView v;
    Paint newPaint = new Paint();
    int randomWidthOne = 0;
    int randomHeightOne = 0;
    private float radiusNsix = 10;
    private float radiusNfive = 25;
    private float radiusNfour = 50;
    private float radiusNthree = 100;
    private float radiusNtwo = 150;
    private float radiusNone = 200;
    private float radiusZero = 250;
    private float radiusOne = 300;
    final int redColorOne = Color.RED;
    final int greenColorOne = Color.GREEN;
    private static int lastColorOne;
    double startTime = System.currentTimeMillis();
    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); 
            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);
        canvas.drawColor(Color.WHITE);
        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))));
        }
        for(int i = 0;i< 300; i++){
            canvas.drawCircle(randomWidthOne, randomHeightOne,i, 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();
    }

}

1 个答案:

答案 0 :(得分:0)