gpt4 book ai didi

android - 在 Canvas 上动画绘制圆/弧

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:30 29 4
gpt4 key购买 nike

更新:2013 年 11 月 20 日:此问题仍未解决。

我正在尝试在自定义 View 中以动画方式创建圆圈。我想制作圆周的动画 - 在动画开始时有一个圆弧,在动画结束时圆是完整的。

我按照这个答案成功地做到了这一点 - https://stackoverflow.com/a/11168363/2442638 - 只需添加一个重复的 Handler 来增加 sweepAngle 并调用 invalidate();

但是,这并不像我希望的那样工作,因为我无法设置完成圆圈的持续时间。

这是我当前的代码:

  Path mOuterCirclePath = new Path();
final RectF mOval = new RectF();
int mSweepAngle = 0;
int mStartAngle = -90;

@Override
protected void onDraw(Canvas canvas) {

mOval.set(0, 0, mBorderRect.right, mBorderRect.bottom); //mBorderRect is the outside border of my view
mOuterCirclePath.arcTo(mOval, 0, 0, true);
canvas.drawArc(mOval, -mStartAngle, mSweepAngle, false,
mOuterCirclePaint);
}

public void drawOuterCircle() {

startUpdateOuterCircle();

}

Runnable mCircleInvalidator = new Runnable() {
@Override
public void run() {
if (mSweepAngle <= 360) {
mSweepAngle++
}
invalidate();
mHandler.postDelayed(mCircleInvalidator, 20);
}
};

void startUpdateOuterCircle() {
mCircleInvalidator.run();
}

void stopUpdateOuterCircle() {
mHandler.removeCallbacks(mCircleInvalidator);
}

主要问题是:如何设置动画的持续时间?我希望它可以轻松更改,就像在动画师类中一样。

附言据我所知,我不能为此使用任何动画师,例如“ObjectAnimator”的 ViewPropertyAnimator。如果我错了,请纠正我!

最佳答案

要使用自定义对象动画器属性,您需要创建 getter 和 setter 方法,如 Stackoverflow 答案中所述:

Android Property Animation

在我的例子中,我有一个 CircleView 类和 sweepAngle 作为变量,如下所示:

public class CircleView extends View
{
public float sweepAngle = 0.0f; // start at 0 degrees

...

// ---------------------------------------------------
// getter and setter method to turn "sweepAngle"
// into a property for ObjectAnimator to use
// ---------------------------------------------------
public float getSweepAngle()
{
return sweepAngle;
}

public void setSweepAngle(float angle)
{
sweepAngle = angle;
}

// ---------------------------------------------------
// animation method to be called outside this class
// ---------------------------------------------------
public void animateArc(float fromAngle, float toAngle, long duration)
{
sweepAngle = fromAngle;

invalidate();

ObjectAnimator anim = ObjectAnimator.ofFloat(this, "sweepAngle", fromAngle, toAngle);
anim.setDuration(duration);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator)
{
// calling invalidate(); will trigger onDraw() to execute
invalidate();
}
});
anim.start();
}
}

注意事项:

以上具体例子是Hithredin建议使用ObjectAnimator的示范。

自定义穷人的实现

我建议您不要使用下面的这段代码,但我将其包括在内,以防您想知道我是如何实现自己的自定义实现的,我发现它偏离了 8 毫秒,并且只允许线性插值(在/ease out),它类似于上面的代码,但略有不同:

public void animateArc(float fromAngle, float toAngle, long duration)
{
sweepAngle = fromAngle;

invalidate();

// ---------------------------------------------------------------
// Note: might want to change use absolute value for totalAngle
// in case you want the animation to play backwards
// ---------------------------------------------------------------
float totalAngle = toAngle - fromAngle;

updateArc(totalAngle, duration);
}

public void updateArc(final float totalAngle, final long durationInMilliSeconds)
{
final long stepMilliseconds = 1;

handler.postDelayed(new Runnable()
{
@Override
public void run()
{
// ------------------------------------------------
// 17790.0 is a number I fine tuned and came out
// with on my Android phone to get the animation
// as close as possible to the animation
// duration specified
// ------------------------------------------------
double stepAngle = totalAngle / 1000.0 * (17790.0 / durationInMilliSeconds);

sweepAngle += stepAngle;
animationTime += stepMilliseconds;
invalidate();

if(animationTime < durationInMilliSeconds && sweepAngle < totalAngle)
{
updateArc(totalAngle, durationInMilliSeconds);
}
else
{
// --------------------------------------------------------
// time duration reached, stop incrementing/decrementing
// angle and reset animation time back to 0
// --------------------------------------------------------
animationTime = 0;
}
}
}, 0);
}

关于android - 在 Canvas 上动画绘制圆/弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20034627/

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