2

I'm using a pinch and zoom function on an ImageView that calls view.setImageMatrix(matrix). The problem is that I also want to call onSizeChanged for this ImageView and this function never seems to get called after the image is zoomed or reduced in size. Is the onDraw() and onSizeChanged() ever called after setImageMatrix is set? I want to be able to access this method:

class MyImageView extends ImageView { 

@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
    super.onSizeChanged(xNew, yNew, xOld, yOld);
    parent = (View) this.getParent();
    ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) parent.getLayoutParams();
    params.height = yNew;
    params.width = xNew;
    parent.setLayoutParams(params);
    if (parent.getParent() != null) {
        if (parent.getParent() instanceof ScrollView) {
            ((ScrollView) parent.getParent()).smoothScrollBy(xNew,yNew);
            ((ScrollView) parent.getParent()).setSmoothScrollingEnabled(true);
            ((ScrollView) parent.getParent()).fullScroll(View.FOCUS_DOWN);

        }
    }

}
}
Community
  • 1
  • 1
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • 1
    My understanding is that the image matrix affects the drawing of the image within the view, while onSizeChanged() is called when the size of the view itself changes. I don't think the image matrix has anything to do with onSizeChanged(). – kris larson Apr 11 '15 at 00:20
  • Is there a way to detect when the actual image size has changed? – Kristy Welsh Apr 11 '15 at 17:14
  • I'm looking at your code... Are you trying to get a ScrollView to show scroll bars that indicate the position of the zoom viewport within the image? – kris larson Apr 11 '15 at 20:53
  • @krislarson What I'm actually trying to do is to get the view to scroll as the image increases in size. – Kristy Welsh Apr 12 '15 at 15:02
  • In your onTouch() with the zoom operations, maybe instead of changing the image matrix, you could force a layout on the ImageView and change the bounds of the ImageView in an onLayout() method somewhere... – kris larson Apr 12 '15 at 20:53
  • I did a zoom image view feature for the app I'm working on now, and by changing the drawing matrix, I was able to zoom (pinch) the image in and out (setScale) and pan (drag) the zoomed image around to see different areas (setTranslate). The display view size matched the screen size and never changed during the zoom/drag operations, so I never had to put the view inside a ScrollView or worry about scrolling. The view also became the clip window for the image. – kris larson Apr 12 '15 at 21:15

1 Answers1

0

onSizeChanged() is not called in your case as changing the matrix of the ImageView doesn't change the size of the View.

But you can fix your problem by calling your statements inside the setMatrix(matrix) like so:

@Override
protected void setMatrix(Matrix matrix)
{
    super.setMatrix(Matrix matrix);
    parent = (View) this.getParent();
    ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) parent.getLayoutParams();
    params.height = yNew;
    params.width = xNew;
    parent.setLayoutParams(params);
    if (parent.getParent() != null) {
        if (parent.getParent() instanceof ScrollView) {
            ((ScrollView) parent.getParent()).smoothScrollBy(xNew,yNew);
            ((ScrollView) parent.getParent()).setSmoothScrollingEnabled(true);
            ((ScrollView) parent.getParent()).fullScroll(View.FOCUS_DOWN);
}

Or you can call all of this using the ImageView object without extending the class. No need to hook into onSizeChanged(...).

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89