【问题标题】:ImageView image not resizing with ImageViewImageView 图像未使用 ImageView 调整大小
【发布时间】:2014-06-27 22:43:09
【问题描述】:

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

这是一个例子:

我希望图像与 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++;

【问题讨论】:

  • 你应该为此提供赏金:)
  • 我只能在 2 天内开始赏金

标签: android imageview tablelayout scaletype


【解决方案1】:

解决了这个问题,这个问题只有在我从 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);

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    相关资源
    最近更新 更多