2

I'm trying to take my small image from (x,y) location, and scale it up to full screen (by screen width) and move it to the center.

Here's a visual motivation for my mission: enter image description here

I have already managed to calculate the scale ratio to enlarge the image to full screen, but I'm struggling calculate the pivot points to make it go to the exactly center.

To ease up the problem I'm assuming the X is already centered for now on.. after I'll understand the Y I'll simply copy the same formula to the X properties.

So I tried to draw up geometrical illustration in order to come up with such generic formula and this is what I've got: enter image description here

and here's the code I've written according to the formula:

public static ScaleAnimation scale2fitAnimation(final View item2Scale, long duration) {

    Context context = item2Scale.getContext();

    int screenW = getScreenW(context);
    int screenH = getScreenH(context);

    int myW = item2Scale.getWidth();
    int myH = item2Scale.getHeight();

    int distanceFromTop = item2Scale.getBottom() - myH / 2;

    int dY_belowCenter = distanceFromTop - screenH / 2;

    float scaleX = (float) screenW / myW;
    float pivotY = 0.5f + (float) dY_belowCenter / (screenH / 2);

    ScaleAnimation scaleAnimation = new ScaleAnimation(
            1f, scaleX, //fromXscale -> toXscale
            1f, scaleX, //fromYscale -> toYscale
            Animation.RELATIVE_TO_SELF, 0.5f, //pivotX
            Animation.RELATIVE_TO_SELF, pivotY //pivotY
    );

    scaleAnimation.setFillAfter(true);
    scaleAnimation.setDuration(duration);

    //restore default shape later on..
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            item2Scale.clearAnimation();
        }
    }, 2000);


    return scaleAnimation;
}

public static int getScreenW(Context context) {
    return context.getResources().getDisplayMetrics().widthPixels;
}

public static int getScreenH(Context context) {
    return context.getResources().getDisplayMetrics().heightPixels;
}

and of course calling it is simple as:

imageView.startAnimation(scale2fitAnimation(imageView,500));

how ever the results are not good.. I can easily see in my eyes that the image doesn't go all the way to the center..

I can't find out whats wrong in my formula I've been rewritten it over and over and I can't find any mathematical or code mistake..

Can anyone please guide me up here to accomplish that or perhaps show me an easier way..

Thanks ahead in advanced!

P.S: I'm only working with square images..

Yarden Cohen
  • 584
  • 2
  • 4
  • 19
  • Aren't you looking for this: http://stackoverflow.com/a/28830564/4133919 maybe you dont have to compute the pivot and just use Animation.RELATIVE_TO_PARENT – Lubo Jul 03 '16 at 20:57
  • 1
    I tried the RELATIVE_TO_PARENT but couldn't find any way to make it go to the center.. if its easier that way I'd like to hear.. I couldn't find anything useful on the link you brought do you mind to point me what you saw there? – Yarden Cohen Jul 04 '16 at 09:51

0 Answers0