Search code examples
androidimageviewtablelayoutscaletype

ImageView image not resizing with ImageView


I have a TableLayout with cells of ImageViews. What I am trying to do is create a zooming effect by incrementing the size of the ImageViews in the tabel. This works perfectly for zooming out( the picture resizes with the ImageView) but, the problem is with zooming in. The picture does not resize when the ImageView is made bigger.

Here is an example: enter image description here

I want the image to be the same size as the ImageView(each black square is an ImageView)

Here is my code for the zooming ( any tips on better implementation would be appreciated, don't want to use pinch zoom ).

zoomIndex ranges from -2, 2. (only allow them to zoom twice in each direction)
zoomInThreshold = 2, zoomOutThreshold = -2. 
defaultSize = 50, zoomIncrement = 15;


                if(zoomIndex < zoomInThreshold)
                    defaultSize += zoomIncrement;


                for(int i = 0; i < table.getChildCount(); ++i)
                {
                    TableRow row = (TableRow)table.getChildAt(i);
                    for(int r = 0; r < row.getChildCount(); ++r)
                    {
                        ImageView img = (ImageView)row.getChildAt(r);

                        if(zoomIndex  < zoomInThreshold)
                        {
                            TableRow.LayoutParams imgLayoutParams = new TableRow.LayoutParams(
                                    defaultSize,
                                    defaultSize
                            );
                            img.setLayoutParams(imgLayoutParams);

                            if(img.getDrawable() != null)
                            {
                                img.setAdjustViewBounds(true);
                                img.setMaxHeight(defaultSize);
                                img.setMaxWidth(defaultSize);
                                img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                            }
                        }
                    }
                }
                if(zoomIndex < zoomInThreshold)
                    zoomIndex++;

Solution

  • Figured out the problem, this problem only happens when I load my drawings from the xml files. All I had to do was add this line when loading the drawing

    img.setAdjustViewBounds(true);
    

    This is how it fits in:

                    if(drawableID != -1)
                    {
                        Bitmap pic = BitmapFactory.decodeResource(getResources(), drawableID);
                        Bitmap resizedBitmap = Bitmap.createScaledBitmap(pic, 50, 50, true);
                        im.setImageBitmap(resizedBitmap);
    
                        ic.setImage(im);
    
                        im.setTag(ic);
    
                        im.setAdjustViewBounds(true);
    
                        //rotate the icon
                        im.setScaleType(ImageView.ScaleType.MATRIX);
                        Matrix matrix = new Matrix();
                        matrix.set(im.getImageMatrix());
                        matrix.postRotate(rotAngle, pic.getWidth() / 2, pic.getHeight() / 2);
                        im.setImageMatrix(matrix);
    
                        im.setOnLongClickListener(longListen);//only set this listener if there is something there
                    }
    

    I only had img.setAdjustViewBounds(true); in my zoom buttons press.