android - 实现曲线动画的问题

标签 android android-animation

<分区>

Possible Duplicate:
Android, move bitmap along a path?

我想通过弯曲的路径移动图像。在 android 中有可能吗?我搜索了很多,但我只能找到关于缩放、旋转和平移动画的信息。所以任何人有任何想法请帮助。在 android 中是否可能?

最佳答案

下面是功能齐全的代码,它将沿着由三个点定义的弯曲路径进行动画处理。 Point 只是一个包含 x 值和 y 值的类(尽管您可以轻松地将它扩展到更多维度)。

所有 m 变量都取自 TranslateAnimation 并以类似的方式使用,因此如果某些地方没有意义,您应该能够相对轻松地将它与 TranslateAnimation 代码进行比较。

初始化中对 resolveSize 的调用意味着您可以使用任何动画类型(ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT)指定弧的起点、终点和半径,就像您对普通 TranslateAnimation 所做的那样。

calcBezier 计算直接取自 Wikipedia 的二次贝塞尔曲线.贝塞尔曲线应该允许平滑缩放并且在图形中很常见(也用于 Android 的 Path 类)。

实际的移动发生在 applyTransformation 中。 interpolatedTime 给出一个介于 0 和 1 之间的值,该值根据提供的插值器非线性增加。 dx 和 dy 是给定时间沿曲线的实际 x 点和 y 点。

这个类的唯一限制是 y 的最大变化总是发生在曲线的中心(参见初始化中对 middleX 的计算)。但是,如果您想要一条非对称曲线,则很容易修改,例如,沿着曲线给出一个特定点,在该点应该出现高点。

查看 TranslateAnimation 的 android 代码特别有帮助。请参阅:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/view/animation/TranslateAnimation.java#TranslateAnimation

public class ArcTranslate extends Animation {

private Point start;
private Point end;
private Point middle;
private final float mFromXValue;
private final float mToXValue;
private final float mYValue;
private final int mFromXType;
private final int mToXType;
private final int mYType;

/**
 * A translation along an arc defined by three points and a Bezier Curve
 *
 * @param duration - the time in ms it will take for the translation to complete
 * @param fromXType - One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
 * @param fromXValue - Change in X coordinate to apply at the start of the animation
 * @param toXType - One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
 * @param toXValue - Change in X coordinate to apply at the end of the animation
 * @param yType - One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
 * @param yValue - Change in Y coordinate to apply at the middle of the animation (the radius of the arc)
 */
public ArcTranslate(long duration, int fromXType, float fromXValue,
        int toXType, float toXValue, int yType, float yValue){
    setDuration(duration);

     mFromXValue = fromXValue;
     mToXValue = toXValue;
     mYValue = yValue;

     mFromXType = fromXType;
     mToXType = toXType;
     mYType = yType;

}

/** Calculate the position on a quadratic bezier curve given three points
 *  and the percentage of time passed.
 * from http://en.wikipedia.org/wiki/B%C3%A9zier_curve
 * @param interpolatedTime - the fraction of the duration that has passed where 0<=time<=1
 * @param p0 - a single dimension of the starting point
 * @param p1 - a single dimension of the middle point
 * @param p2 - a single dimension of the ending point
 */
private long calcBezier(float interpolatedTime, float p0, float p1, float p2){
    return Math.round((Math.pow((1 - interpolatedTime), 2) * p0)
           + (2 * (1 - interpolatedTime) * interpolatedTime * p1)
           + (Math.pow(interpolatedTime, 2) * p2));
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    float dx = calcBezier(interpolatedTime, start.x, middle.x, end.x);
    float dy = calcBezier(interpolatedTime, start.y, middle.y, end.y);

    t.getMatrix().setTranslate(dx, dy);
}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    float startX = resolveSize(mFromXType, mFromXValue, width, parentWidth);
    float endX = resolveSize(mToXType, mToXValue, width, parentWidth);
    float middleY = resolveSize(mYType, mYValue, width, parentWidth);
    float middleX = startX + ((endX-startX)/2);
    start = new Point(startX, 0);
    end = new Point(endX, 0);
    middle = new Point(middleX, middleY);
}
}

关于android - 实现曲线动画的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849554/

相关文章:

Android SDK 管理器无法在 Windows 8 x64 上获取存储库

java - 如何在运行时在 Android 中显示图像并将单选按钮作为列表中的单选组

fragment 外的 Android 动画 View 被剪裁

android - 增加 viewpager smoothscroll 持续时间

android - 如何避免让用户下载单独的 APK 进行帐户管理?

android - 改造 response.errorBody() 为空

android - 逐项叠加教程

android - ListView 和动画

android - 动画 : Move TextView into another container

android - FAB 没有动画 - 附上测试代码和截图