Luciano
Luciano

Reputation: 2796

how to scale a image to imageview in onCreate when i need to use scaleType matrix

I'm trying to create a ImageView with zoom functions. I got the zoom function ready, but now I want the image to scale to the ImageView when starting the Activity. Normally I would use the ScaleType in the xml layout, but it needs to be Matrix for the zoom function. How can I do this?

I tried the solution from this question: Android image view matrix scale + translate

Matrix m = imageView.getImageMatrix();
RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight());
m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
imageView.setImageMatrix(m);

But the problem is I can't use getWidth() and getHeight() because its to early (it only returns 0). Is there a work around? Or how can I fix this?

Upvotes: 1

Views: 3846

Answers (3)

anguila
anguila

Reputation: 148

I had the same problem with matrix, zoom and imageview and the easiest solution I found is to render the image twice, one with scaleType fitcenter and Matrix.

After many (really many..) tempts with setRectToRect and the solution mentioned before (and so on..), I finally find a solution (at least for my case, similar to the post):

In the imageview in your activity xml set fitcenter:

    <ImageView
     ...
    android:scaleType="fitCenter"

Then in your java file define a init function on focus changed

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        init();
    }
}

private void init() {
    maxZoom = 10;
    minZoom = 1f;
    height = viewHeight;
    width = viewWidth;
    **Matrix m = imageSWT.getImageMatrix();
    imageSWT.setScaleType(ScaleType.MATRIX);
    matrix.set(m);**

}

So I'm using the property of fitcenter and then I get his matrix, after that I switch to Matrix mode (for the zoom functionality) and I save the fitcentered-matrix into the matrix-zoom object for keeping the changes when the zoom is used.

I hope this solution could help someone ;)

Upvotes: 1

Sam Judd
Sam Judd

Reputation: 7387

The only way to be sure of a views width and height is to subclass the view and override onSizeChanged() and use the given width/height or onLayout() and used getMeasuredHeight() and getMeasuredWidth().

This means you're matrix won't be setup until way after onCreate() (until the view is about to be rendered for the first time), but at least the code you've posted could go entirely into the subclassed view.

Upvotes: 0

aromero
aromero

Reputation: 25761

You need to a add an observer to the ImageView, that will be called when the object is layouted:

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
       int width = imageView.getWidth();
       int height = imageView.getHeight();
       // do your stuff
    }
});

Upvotes: 1

Related Questions