2

我正在使用KenBurnsView为我的图像添加动画。
是否可以以编程方式设置最大比例?小图像太缩放。

4

1 回答 1

0

是的你可以 。为此,您必须实现自己的 TransitionGenerator。就像给定的打击一样。在下面的代码中,您可以调用 SetMinRectFactor() 来设置最小缩放比例。

public class BackgroundTransitionGenerator implements TransitionGenerator 
{
  private static final int DEFAULT_TRANSITION_DURATION = 9000;
  private static final Interpolator DEFAULT_TRANSITION_INTERPOLATOR = 
  new LinearInterpolator();

private long transitionDuration;
private Interpolator transitionInterpolator;
private Transition lastTransition;
private RectF lastDrawableBounds;
private boolean forward;
private float MIN_RECT_FACTOR=1.0f;

public void SetMinRectFactor(float f)
{
    MIN_RECT_FACTOR=f;
}
public BackgroundTransitionGenerator() {
    transitionDuration = DEFAULT_TRANSITION_DURATION;
    transitionInterpolator = DEFAULT_TRANSITION_INTERPOLATOR;
}

@Override
public Transition generateNextTransition(RectF drawableBounds, RectF viewport) {
    float drawableRatio = getRectRatio(drawableBounds);
    float viewportRectRatio = getRectRatio(viewport);
    RectF startRect;
    RectF endRect;
    if (drawableRatio >= viewportRectRatio) {
        float w = drawableBounds.height() * viewportRectRatio;
        float h = drawableBounds.height();
        startRect = new RectF(0, 0, w, h);
        endRect =generateRandomRect(drawableBounds,viewport); //new RectF((drawableBounds.width() - w), drawableBounds.height(),drawableBounds.width(), h);
    } else {
        float w = drawableBounds.width();
        float h = drawableBounds.width() / viewportRectRatio;
        startRect = new RectF(0, 0, w, h);
        endRect =generateRandomRect(drawableBounds,viewport); //new RectF(0, drawableBounds.height() - h, w, drawableBounds.height());
    }

    if (!drawableBounds.equals(lastDrawableBounds) || !haveSameAspectRatio(lastTransition.getDestinyRect(), viewport)) {
        forward = false;
    }
    forward = !forward;

    if (forward) {
        lastTransition = new Transition(startRect, endRect, transitionDuration, transitionInterpolator);
    } else {
        lastTransition = new Transition(endRect, startRect, transitionDuration, transitionInterpolator);
    }

    lastDrawableBounds = new RectF(drawableBounds);
    return lastTransition;
}

private static boolean haveSameAspectRatio(RectF r1, RectF r2) {
    return (Math.abs(getRectRatio(r1) - getRectRatio(r2)) <= 0.01f);
}
private RectF generateRandomRect(RectF drawableBounds, RectF viewportRect) {
    float drawableRatio = getRectRatio(drawableBounds);
    float viewportRectRatio = getRectRatio(viewportRect);
    RectF maxCrop;

    if (drawableRatio > viewportRectRatio) {
        float r = (drawableBounds.height() / viewportRect.height()) * viewportRect.width();
        float b = drawableBounds.height();
        maxCrop = new RectF(0, 0, r, b);
    } else {
        float r = drawableBounds.width();
        float b = (drawableBounds.width() / viewportRect.width()) * viewportRect.height();
        maxCrop = new RectF(0, 0, r, b);
    }

    float factor = MIN_RECT_FACTOR ;

    float width = factor * maxCrop.width();
    float height = factor * maxCrop.height();
    int widthDiff = (int) (drawableBounds.width() - width);
    int heightDiff = (int) (drawableBounds.height() - height);
    int left = widthDiff;
    int top = heightDiff;
    return new RectF(left, top, left + width, top + height);
}

private static float getRectRatio(RectF rect) {
    return rect.width() / rect.height();
}

}

于 2018-11-29T17:39:00.180 回答