问题描述
首先让我解释一下我的目标.我正在尝试制作一个 Animation
来改变 ArcShape
的属性.ArcShape 的
构造函数采用两个字段:startAngle
和 sweepAngle
.我想为 sweepAngle
设置动画,使其在屏幕上显示为一个不断缩小的圆圈.
First let me explain my goal. I am trying to make an Animation
that changes the properties of an ArcShape
. An ArcShape's
constructor takes two fields: startAngle
and sweepAngle
. I want to animate the sweepAngle
so that it appears on screen as a continuously shrinking circle.
你可以想象吃豆人来描绘这个动画.想象他的嘴是闭着的.这个动画类似于他越来越多地张开上颚,直到没有吃豆人.
You can picture this animation by imagining PacMan. Imagine his mouth is closed. This animation would be akin to him opening his upper jaw more and more until there was no more PacMan.
现在...我在实现这个时遇到了一些问题.首先,一旦创建了 ArcShape
,就没有内置的方法可以改变它的 sweepAngle
.这让我想到了我的第一个问题:有没有办法覆盖 ArcShape
并实现一些 setSweepAngle
方法?或者我是否必须为我希望显示的每个 sweepAngle
创建一个 new ArcShape
?
Now... I have a couple of issues with implementing this. First, once an ArcShape
is created, there are no built in methods of changing it's sweepAngle
. This brings me to my first question: Is there any way to override ArcShape
and implement some setSweepAngle
method? Or will I have to create a new ArcShape
for each sweepAngle
I wish to display?
现在进入第二个问题...假设我找到了第一个问题的解决方案,我该如何创建这个Animation
?这是我现在拥有的要点:
Now on to the second issue... Assuming I found a solution to the first issue, how could I create this Animation
? This is the gist of what I have now:
public class OpenPacman extends Animation {
public OpenPacman(float startAngle, float sweepAngle) {
mStartAngle = startAngle;
mSweepAngle = sweepAngle;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
/* This represents the current sweepAngle */
float currAngle = mStartAngle + ((mSweepAngle - mStartAngle) * interpolatedTime);
//Now I need to update the ArcShape's sweepAngle to currAngle. But HOW?
}
}
推荐答案
我找到了解决方案.我有一个扩展 View
的类我们称之为 Pacman
我在这个 Pacman
类中嵌套了我的自定义 Animation
.这允许我访问 Pacman
类的 成员变量
.
I have found a solution. I have a class that extends View
We'll call this Pacman
I nested my custom Animation
within this Pacman
class. This allowed me to access the member variables
of the Pacman
class.
public class Pacman extends View {
float mSweepAngle;
...
//include constructors
//override onMeasure
...
/* Here we override onDraw */
@Override
protected void onDraw(final Canvas canvas) {
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
RectF oval = new RectF(canvas.getClipBounds());
canvas.drawArc(oval, 0, mCurrAngle, true, p);
}
/* Here we define our nested custom animation */
public class OpenPacman extends Animation {
float mStartAngle;
float mSweepAngle;
public OpenPacman (int startAngle, int sweepAngle, long duration) {
mStartAngle = startAngle;
mSweepAngle = sweepAngle;
setDuration(duration);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float currAngle = mStartAngle + ((mSweepAngle - mStartAngle) * interpolatedTime);
Pacman.this.mCurrAngle = -currAngle; //negative for counterclockwise animation.
}
}
}
现在,当自定义动画更新容器类 mCurrAngle
时,会自动调用 onDraw
,从而绘制适当的 ArcShape
.
Now when the custom animation updates the container classes mCurrAngle
, onDraw
is automatically called, which draws the appropriate ArcShape
.
这篇关于ArcShape 的 Android 自定义动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!