dfe*_*r88 11 java animation android
首先让我解释一下我的目标.我试图Animation改变一个属性ArcShape.一个ArcShape's构造函数有两个领域:startAngle和sweepAngle.我想要制作动画sweepAngle,使其在屏幕上显示为一个不断缩小的圆圈.
您可以通过想象PacMan来拍摄这个动画.想象一下,他的嘴闭上了.这个动画将类似于他越来越多地打开他的上颌,直到没有更多的PacMan.
现在......我有一些实施这个问题.首先,一旦ArcShape创建了一个,就没有内置的方法来改变它sweepAngle.这让我想到了第一个问题:有没有办法覆盖ArcShape并实现某些setSweepAngle方法?或者我是否必须new ArcShape为每个sweepAngle我希望展示的内容创建一个?
现在转到第二个问题......假设我找到了第一个问题的解决方案,我怎么能创建它Animation呢?这是我现在所拥有的要点:
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?
}
}
Run Code Online (Sandbox Code Playgroud)
dfe*_*r88 16
我找到了解决方案.我有一个扩展的类View我们称之为Pacman嵌套我Animation在这个Pacman类中的自定义.这让我可以访问member variables该Pacman课程.
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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当自定义动画更新容器类时mCurrAngle,onDraw会自动调用,从而绘制相应的ArcShape.
| 归档时间: |
|
| 查看次数: |
7265 次 |
| 最近记录: |