将图像调整为整个imageView,然后设置动画以显示隐藏的部分

时间:2016-06-03 08:42:02

标签: android android-animation android-kenburnsview

我想要获得的是从imageView加载图像时,它应该填充整个图像视图,类似于scaleType =“centerCrop”,但如果它是一个风景图像,它应该在中心裁剪它或者如果portriat裁剪到顶部。然后它应该慢慢平移以慢慢显示图像的裁剪部分。

喜欢这个例子。

enter image description here

我找到了一个可以帮助我解决这个难题的图书馆

KenburnsView

唯一的问题是文档没有提供关于TransitionGenerator的全面解释,这是我需要的,而且我太愚蠢了,无法自己解决。我尝试使用谷歌搜索,但只能找到this

如果您尝试过此库,可以指向正确的方向,或者如果您有其他类似功能的库,请告诉我。感谢。

1 个答案:

答案 0 :(得分:1)

我最近在APP中有同样的需求。

在这里阅读KenBurnsView的源代码之后 是可行的TransitionGenerator

public static class ScanTransitionGenerator implements TransitionGenerator {
    private static final int DEFAULT_TRANSITION_DURATION = 5000;
    private static final Interpolator DEFAULT_TRANSITION_INTERPOLATOR = new AccelerateDecelerateInterpolator();

    private long transitionDuration;
    private Interpolator transitionInterpolator;
    private Transition lastTransition;
    private RectF lastDrawableBounds;
    private boolean forward;

    public ScanTransitionGenerator() {
        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 = new RectF(drawableBounds.width() - w, 0, drawableBounds.width(), h);
        } else {
            float w = drawableBounds.width();
            float h = drawableBounds.width() / viewportRectRatio;
            startRect = new RectF(0, 0, w, h);
            endRect = 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 static float getRectRatio(RectF rect) {
        return rect.width() / rect.height();
    }
}