Reputation: 1302
What I'm trying to obtain is when loading an image from the imageView it should fill the whole imageview similar to scaleType="centerCrop" but instead of cropping it in the center it should crop to the left if it's a landscape image or crop to top if portriat. Then it should pan slowly to display the cropped part of the image slowly.
Like this illustration.
I found a library that might help me with this dilemma
The only problem is that documentation does not give a comprehensive explanation regarding the TransitionGenerator
which is what I need and I am too dumb to figure it out myself. I tried googling around but was able to find only this.
If you have tried this library can you point me to the right direction or if you have other library with a similar feature please let me know. Thanks.
Upvotes: 1
Views: 405
Reputation: 43
I have the same need in the APP I recently work on.
After reading the source code of KenBurnsView, here
is a workable 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();
}
}
Upvotes: 1