Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.3 ImageView ScaleType.MATRIX

today I set up the new Android JB 4.3 on my Nexus 7 and i tried to run my application.

Everythings works like it should except one little thing about ImageViews with ScaleType.MATRIX.

Basically what i have in my application is a ImageView as background and accordingly to a ViewPager callbacks i move the focused part of the image updating the Matrix i gave to the imageView using setImageMatix( Matrix matrix ).

the problem seems to be that i can't update the matrix anymore, i just have to instantiate a new one a pass it to the ImageView.

i managed to work around it instantiating everytime a new Matrix but it seems awfully memory expensive compared to the old version.

is this a BUG? is there a way to udpate the Matrix? ( i by the way already tried to invalidate() the ImageView ecc. )

NOT WORKING

    private void updateMatrix( final int page, final double offset ) {          
        double pagePosition = page + offset;

        Matrix matrix = imageView.getImageMatrix();
        matrix.setScale( scale, scale );
        matrix.postTranslate( - (float) ( pagePosition * pageWidth ) , 0 );

        imageView.setImageMatrix( matrix );

        imageView.invalidate();
    }

WORKING

    private void updateMatrix( final int page, final double offset ) {          
        double pagePosition = page + offset;

        Matrix matrix = new Matrix();
        matrix.setScale( scale, scale );
        matrix.postTranslate( - (float) ( pagePosition * pageWidth ) , 0 );

        imageView.setImageMatrix( matrix );

        imageView.invalidate();
    }

EDIT:

in the first case the image is shown at the top left corner of the ImageView without any scale or translate applied to it, like if the matrix is back to identity.

like image 934
Mario Lenci Avatar asked Nov 19 '25 18:11

Mario Lenci


2 Answers

Just preserve your Matrix as field instead of retrieving it from ImageView and you'll be happy :)

like image 152
Dmitry Zaytsev Avatar answered Nov 21 '25 08:11

Dmitry Zaytsev


There may be a bug with ImageView scaling starting with 4.3. See my question and answer about this bug.

like image 41
kburbach Avatar answered Nov 21 '25 07:11

kburbach