ImageView图像未使用ImageView调整大小

时间:2014-06-27 13:09:44

标签: android imageview tablelayout scaletype

我有一个TableLayout,单元格为ImageView s。我想要做的是通过增加表格中ImageView的大小来创建缩放效果。这适用于缩小(图片使用ImageView调整大小),但问题在于放大。当ImageView变大时,图片不会调整大小。

这是一个例子: enter image description here

我希望图像与ImageView的大小相同(每个黑色方块都是ImageView)

这是我的缩放代码(有关更好实现的任何提示都会受到赞赏,不想使用缩放缩放)。

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++;

1 个答案:

答案 0 :(得分:0)

想出问题,这个问题只发生在我从xml文件加载图纸时。我只需要在加载绘图时添加此行

img.setAdjustViewBounds(true);

这就是它适用的方式:

                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
                }

按下我的缩放按钮时只有img.setAdjustViewBounds(true);

相关问题