4

I am trying to apply a ScaleAnimation to a View, but I have a somewhat nontraditional requirement: I would like to scale only a very specific region of the View.

Using the existing ScaleAnimation class I can easily uniformly scale a view. I can also set a pivot point about which to scale a view. Below is an example of that:

smiley scaled from bottom

This is straightforward. But I wish to achieve the following, which results from scaling only a particular region of a view (or in this case a small horizontal rectangle in the middle of the smiley):

smiley with specific region scaled

I dug around the source code for ScaleAnimation, and the following function seems to be responsible for scaling:

protected void applyTransformation(float interpolatedTime, Transformation t) {
    float sx = 1.0f;
    float sy = 1.0f;

    if (mFromX != 1.0f || mToX != 1.0f) {
        sx = mFromX + ((mToX - mFromX) * interpolatedTime);
    }
    if (mFromY != 1.0f || mToY != 1.0f) {
        sy = mFromY + ((mToY - mFromY) * interpolatedTime);
    }

    if (mPivotX == 0 && mPivotY == 0) {
        t.getMatrix().setScale(sx, sy);
    } else {
        t.getMatrix().setScale(sx, sy, mPivotX, mPivotY);
    }
}

Since this function is simply applying a scale operation to a matrix, I was thinking there is some sort of matrix operation that I could use to write a custom scale animation, but my linear algebra know-how is lacking. The solution to this may be far simpler as it seems as though others would run into this issue, but I haven't been able to find any solutions. Thanks in advance.

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58

1 Answers1

1

A matrix transforms the entire bitmap. You can not scale regions with a single matrix.

yoah
  • 7,180
  • 2
  • 30
  • 30
  • So, this cannot be done with matrices. Perhaps there is still a way to do such a transformation in android? – Daniel Smith Apr 26 '13 at 06:37
  • 1
    It depends on what you want to do exactly. For you face example, you could use a 9-png where you define which area can stretch and which can't. Load the 9-png as a Drawable, fill the view with it, and instead of the scale animation, animate the view size. There are other ways, you can cut your image to 3 images where the middle is the middle thin slice, and then have scale animation on the middle, and more the top and bottom to match the size of the middle. It depends on what you need to do. I assume the smily example if just an example, not what you really need – yoah Apr 26 '13 at 07:31
  • Yeah I think I might try animating the view size! Going to add this to your answer because it seems like the right way to go about solving the problem at this point in time. – Daniel Smith Apr 26 '13 at 19:43