是否有可能通过点击按钮来调整图像的方案? [英] Is there possibility to resize the image programmatic by click on button?

查看:109
本文介绍了是否有可能通过点击按钮来调整图像的方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我初步显示出一个空的观点。当我点击添加图片按钮,它显示了一组图像在网格然后我点击的图像上。我把图像中的视图通过动态地添加ImageView的使用拖动层。就这样,我加入了更多的图像。现在我想以编程方式调整每一个形象。可能吗?请提供code,如果它是可能的。

I am initially showing an empty view. When I click on the "Add Image" button it shows a group of images in a grid then I click on an image. I am placing image in the view by adding the ImageView dynamically using Drag Layer. In this way I am adding more Images. Now I want to resize each of the image programmatically. Is it possible? Please provide the code if it is possible.

推荐答案

您可以使用下面的code,以调整图像大小。如果你知道图片的路径和需要的宽度和高度

You can use the following code to resize an image. if you know the image path and required width and height

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

谢谢 迪帕克

Thanks Deepak

这篇关于是否有可能通过点击按钮来调整图像的方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆