1

我想要获得的是从 imageView 加载图像时,它应该填充整个图像视图,类似于 scaleType="centerCrop" 但不是在中心裁剪它,如果它是横向图像,它应该裁剪到左侧或裁剪到顶部如果纵向。然后它应该慢慢平移以缓慢显示图像的裁剪部分。

就像这个插图。

在此处输入图像描述

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

肯伯恩斯视图

唯一的问题是文档没有对TransitionGenerator我需要什么给出全面的解释,而且我自己也太笨了,无法弄清楚。我试着用谷歌搜索,但只能找到这个

如果您尝试过这个库,您能否指出正确的方向,或者如果您有其他具有类似功能的库,请告诉我。谢谢。

4

1 回答 1

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();
    }
}
于 2017-05-10T08:03:19.680 回答