【问题标题】:doesn't progress the radius, but should不增加半径,但应该
【发布时间】:2015-05-03 22:04:13
【问题描述】:

我知道这样做是错误的,但由于缺乏任何人的帮助,我才这样做。我在同一场比赛中绘制了 7 个不同半径的圆圈,每个圆圈在绘制后都会暂停 300 毫秒,所以理论上它应该看起来像圆圈在绘制时正在扩大,但由于某种原因,有人不能告诉我为什么或也许回答这个让我痛苦数周的问题。该应用程序在屏幕上随机显示绿色或红色圆圈。当它显示一个圆时,它实际上应该显示 7 个圆,而不是只显示最后一个半径为 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;
    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))));
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusNsix, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusNfive, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusNfour, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusNthree, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusNtwo, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusNone, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        canvas.drawCircle(randomWidthOne, randomHeightOne, radiusZero, newPaint);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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();
    }

}

【问题讨论】:

  • 请详细说明它到底在做什么,以及你想要它做什么?

标签: java android animation canvas geometry


【解决方案1】:

首先,你在做什么 - 绘制事物的错误方式。您不应该在 onDraw 方法中阻塞 UI 线程。 第二 - 圆圈显示在随机位置,因为您使用随机数生成器,canvas.drawCircle 的前两个参数不是宽度和高度(如果我正确理解您的代码)。

目前所有 UI 线程都在每个 Thread.sleep 上阻塞。如果你真的想为所有这些圆圈设置动画,你应该在每次调用 onDraw 方法时根据时间计算它们的大小。给你一个想法:

public SplashLaunch(Context context) {
    super(context);
    startTime = System.currentTimeMills();
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(x, y, calculateRadius(System.currentTimeMills() - startTime), paint);
}

还有一件事,您的代码只显示最后一个圆圈,因为您最后绘制它,您是否希望显示 7 个圆圈,您需要以相反的顺序绘制它。

【讨论】:

  • 我希望它最后绘制
  • System.currentTimeMilillis() 正在减去自身半径将始终为零
  • @PaulTrueman 您是否尝试在您的代码中实现它来说明这一点?
猜你喜欢
  • 2013-07-29
  • 2016-10-03
  • 1970-01-01
  • 2011-07-17
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
相关资源
最近更新 更多