5

可能重复:
Android,沿路径移动位图?

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

4

4 回答 4

9

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

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

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

calcBezier 计算直接取自Wikipedia的二次贝塞尔曲线。Bezier 曲线应该允许平滑缩放并且在图形中很常见(也用于 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);
}
}
于 2011-12-10T06:55:39.823 回答
5

它的灵感来自于 Monkeyless 的回答。我创建了 Animation 的一个子类,使用 PathMeasure 来计算平移。您可以使用 Path 创建一个新的 PathAnimation,并像使用任何其他动画一样使用它。

https://github.com/coocood/PathAnimation

public class PathAnimation extends Animation {
private PathMeasure measure;
private float[] pos = new float[2];
public PathAnimation(Path path) {
    measure = new PathMeasure(path, false);
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t){
    measure.getPosTan(measure.getLength() * interpolatedTime, pos,null);
    t.getMatrix().setTranslate(pos[0], pos[1]);
}
}
于 2012-07-11T02:10:01.920 回答
1

您可以逐帧制作动画。您可以逐步定位对象以创建曲线。这将是有限的重用,但你可以做到。

或者您可以编写自己的动画来创建 TweenAnimation 的子类,该子类可以沿曲线制作动画。如果您擅长数学并且可以理解贝塞尔曲线,那么这可能是一个直接的选择。一旦你有了那个类,你就可以轻松地在任何弯曲的路径上制作动画,但这需要更多的工作。

http://en.wikipedia.org/wiki/B%C3%A9zier_curve

这是一些Java代码:

http://www.math.ubc.ca/~cass/gfx/bezier.html

于 2011-07-27T19:27:28.573 回答
0

您可以像这样创建自己的类:

public class BezierTranslateAnimation extends TranslateAnimation {

private int mFromXType    = ABSOLUTE;
private int mToXType      = ABSOLUTE;
private int mFromYType    = ABSOLUTE;
private int mToYType      = ABSOLUTE;
private float mFromXValue = 0.0f;
private float mToXValue   = 0.0f;
private float mFromYValue = 0.0f;
private float mToYValue   = 0.0f;
private float mFromXDelta;
private float mToXDelta;
private float mFromYDelta;
private float mToYDelta;
private float mBezierXDelta;
private float mBezierYDelta;

public BezierTranslateAnimation(float fromXDelta, float toXDelta,float fromYDelta, float toYDelta, float bezierXDelta, float bezierYDelta) {
     super(fromXDelta, toXDelta, fromYDelta, toYDelta);

      mFromXValue = fromXDelta;
      mToXValue   = toXDelta;
      mFromYValue = fromYDelta;
      mToYValue   = toYDelta;
      mFromXType  = ABSOLUTE;
      mToXType    = ABSOLUTE;
      mFromYType  = ABSOLUTE;
      mToYType    = ABSOLUTE;
      mBezierXDelta = bezierXDelta;
      mBezierYDelta = bezierYDelta;

}



@Override
protected void  applyTransformation(float interpolatedTime, Transformation t) {

    float dx=0,dy=0;

    if (mFromXValue != mToXValue) {

        dx  = (float) ((1.0-interpolatedTime)*(1.0-interpolatedTime)*mFromXValue + 2.0*interpolatedTime*(1.0-interpolatedTime)*mBezierXDelta + interpolatedTime*interpolatedTime*mToXValue);
    }

    if (mFromYValue != mToYValue) {

        dy  = (float) ((1.0-interpolatedTime)*(1.0-interpolatedTime)*mFromYValue + 2.0*interpolatedTime*(1.0-interpolatedTime)*mBezierYDelta + interpolatedTime*interpolatedTime*mToYValue);
    }

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

  }

}

然后将其与此签名一起使用:

BezierTranslateAnimation(float fromXDelta, float toXDelta,float fromYDelta, float toYDelta, float bezierXDelta, float bezierYDelta);
于 2012-06-18T03:01:24.270 回答