I'm using KenBurnsView to add animation to my image.
Is it possible to set the max scale programmatically ? Small images are too zooming.
Asked
Active
Viewed 273 times
2

enfix
- 6,680
- 12
- 55
- 80
-
1I don't know, but now I want to find an excuse to use a `KenBurnsView`. – Kevin Krumwiede Mar 26 '17 at 21:40
1 Answers
0
Yes, you can . For this purpose you have to implement your own TransitionGenerator. Like given blow .In following code you can call SetMinRectFactor() to set minimum zoom sacale.
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();
}
}

Arslan Aslam
- 121
- 1
- 10