gpt4 book ai didi

java - for循环不 "grow"循环。我需要 "grow"一个圆圈

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:41 25 4
gpt4 key购买 nike

我正在尝试编写一个Android应用程序,它可以不断地在随机位置绘制圆圈。这已经在我的代码中完成了。我的下一个目标是慢慢地制作这些圆圈的动画,使它们“生长”到屏幕上。基本上将圆的半径从 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();
}

}

最佳答案

调用this.invalidate()强制在下一帧重绘:

private int x = -1;
private int y = -1;
private int r = -1;
private int stepsLeft = 300;


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (x < 0) { // generate new circle
int[] xy = generateXY();
x = xy[0];
y = xy[1];
r = 0;
}
if (stepsLeft > 0) {
canvas.drawColor(Color.WHITE);
canvas.drawCircle(x, y, r, newPaint); // draw circle with new radius
r++; // increment radius
stepsLeft--; // decrement steps
this.invalidate(); // invalidate the view
}
}



private int[] generateXY() {
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))));
}
return new int[]{randomWidthOne, randomHeightOne};
}

关于java - for循环不 "grow"循环。我需要 "grow"一个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com