【问题标题】:Is there possibility to resize the image programmatic by click on button?是否有可能通过单击按钮以编程方式调整图像大小?
【发布时间】:2012-11-04 17:16:50
【问题描述】:

我最初显示的是一个空视图。当我单击“添加图像”按钮时,它会在网格中显示一组图像,然后我单击图像。我通过使用拖动层动态添加 ImageView 将图像放置在视图中。通过这种方式,我添加了更多图像。现在我想以编程方式调整每个图像的大小。可能吗?如果可能,请提供代码。

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用以下代码调整图像大小。如果您知道图像路径和所需的宽度和高度

    public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
                int targetHeight) {
            Bitmap bitMapImage = null;
            // First, get the dimensions of the image
            Options options = new Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
            double sampleSize = 0;
            // Only scale if we need to
            // (16384 buffer for img processing)
            Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
                    .abs(options.outWidth - targetWidth);
    
            if (options.outHeight * options.outWidth * 2 >= 1638) {
                // Load, scaling to smallest power of 2 that'll get it <= desired
                // dimensions
                sampleSize = scaleByHeight ? options.outHeight / targetHeight
                        : options.outWidth / targetWidth;
                sampleSize = (int) Math.pow(2d,
                        Math.floor(Math.log(sampleSize) / Math.log(2d)));
            }
    
            // Do the actual decoding
            options.inJustDecodeBounds = false;
            options.inTempStorage = new byte[128];
            while (true) {
                try {
                    options.inSampleSize = (int) sampleSize;
                    bitMapImage = BitmapFactory.decodeFile(filePath, options);
    
                    break;
                } catch (Exception ex) {
                    try {
                        sampleSize = sampleSize * 2;
                    } catch (Exception ex1) {
    
                    }
                }
            }
    
            return bitMapImage;
        }
    

    谢谢 迪帕克

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多