问题描述
首先让我解释一下我的目标。我试图让一个动画
,改变了 ArcShape
的属性。一个 ArcShape的
构造函数有两个字段:由startAngle
和 sweepAngle
。我想动画 sweepAngle
使其显示在屏幕上连续缩小圈子。
您可以通过想象吃豆图片此动画。想象一下他的嘴巴闭合。这个动画将类似于他打开他的上颚越来越多,直到有没有更多的吃豆。
现在......我有几个实现此问题。首先,一旦 ArcShape
创建,有没有内置的改变方法,它是 sweepAngle
。这使我想起我的第一个问题:有没有什么方法可以覆盖 ArcShape
和实施一些 setSweepAngle
的方法?或者将我必须创建一个新ArcShape
每个 sweepAngle
我要显示?
现在就到了第二个问题......假设我找到了解决的第一个问题,我怎么能创造这个动画
?这是我现在的要点:
公共类OpenPacman扩展动画{
公共OpenPacman(浮startAngle开始,浮动sweepAngle){
mStartAngle = startAngle开始;
mSweepAngle = sweepAngle;
}
@覆盖
保护无效applyTransformation(浮动interpolatedTime,变换T){
/ *此重新presents当前sweepAngle * /
浮currAngle = mStartAngle +((mSweepAngle - mStartAngle)* interpolatedTime);
//现在我需要更新ArcShape的sweepAngle到currAngle。但如何?
}
}
我已经找到了解决办法。我有一个扩展的类查看
我们将这个吃豆子
我嵌套的我自定义的动画
在此吃豆子
类。这让我访问成员变量
。吃豆子
类
公共类吃豆子扩展视图{
浮动mSweepAngle;
...
//包括构造
//覆盖onMeasure
...
/ *在这里,我们覆盖的OnDraw * /
@覆盖
保护无效的OnDraw(最后的画布油画){
涂料P =新的油漆(Paint.ANTI_ALIAS_FLAG);
RectF椭圆=新RectF(canvas.getClipBounds());
canvas.drawArc(椭圆形,0,mCurrAngle,真实,P);
}
/ *在这里我们定义嵌套自定义动画* /
公共类OpenPacman扩展动画{
浮动mStartAngle;
浮动mSweepAngle;
公共OpenPacman(INT startAngle开始,诠释sweepAngle,持续时间长){
mStartAngle = startAngle开始;
mSweepAngle = sweepAngle;
setDuration(持续时间);
setInterpolator(新LinearInterpolator());
}
@覆盖
保护无效applyTransformation(浮动interpolatedTime,变换T){
浮currAngle = mStartAngle +((mSweepAngle - mStartAngle)* interpolatedTime);
Pacman.this.mCurrAngle = -currAngle; //负逆时针动画。
}
}
}
现在,当自定义动画更新容器类 mCurrAngle
,的OnDraw
被自动调用,吸引相应的 ArcShape
。
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.
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?
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?
}
}
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.
}
}
}
Now when the custom animation updates the container classes mCurrAngle
, onDraw
is automatically called, which draws the appropriate ArcShape
.
这篇关于Android的自定义动画为ArcShape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!