【问题标题】:Fit image to entire imageView then animate to show the hidden part使图像适合整个 imageView 然后动画显示隐藏部分
【发布时间】:2016-06-03 08:42:01
【问题描述】:

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

喜欢这个插图。

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

KenburnsView

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

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

【问题讨论】:

    标签: android android-animation android-kenburnsview


    【解决方案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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-21
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多