将从图库中选取的图像调整为图像视图

时间:2016-02-05 14:46:19

标签: android image resize gallery

我的应用程序有一个带有ImageView的活动,我从手机图库中选择一张图片,并在此ImageView中设置为用户的个人资料图片。

我的问题是,一些图片在挑选时会使app停止原因太大,我想知道是否有人可以查看我的代码并帮助我如何调整这个选中的图片,所以在这个图片视图中设置后,怎么能用户在设置之前剪切了这张照片,下面是我拍摄照片的代码。如果有人做了必要的修改并给我代码,我会非常感激,因为我不太了解开发。谢谢。

{{1}}

4 个答案:

答案 0 :(得分:0)

不会重写你的代码,但这可能很有用

    Bitmap bitmap = BitmapFactory.decodeFile(foto.getFotoOrderFilePath());
    Double height = (double)bitmap.getHeight();
    Double scalingFactor = (960.0/height);
    int tempWidht = bitmap.getWidth();
    Double Dwidth = (tempWidht*scalingFactor);
    int width = Dwidth.intValue();
    Log.v("bitmap dimensions: ", String.valueOf(height) + " + " +String.valueOf(width) + " + " + String.valueOf(scalingFactor));
    bitmap = Utilities.scaleBitmap(bitmap, width, 960);

我用来缩放位图的代码的摘录。它将高度设置为960并进行缩放以相应地更改宽度。

编辑:

ScaleBitmap方法。

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
    Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Matrix m = new Matrix();
    m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
    canvas.drawBitmap(bitmap, m, new Paint());
    return output;
}

抱歉迟到的回复

答案 1 :(得分:0)

尝试这是像我在我的应用程序中使用的whtsapp一样的图像压缩https://www.built.io/blog/2013/03/improving-image-compression-what-weve-learned-from-whatsapp/

答案 2 :(得分:0)

您可以使用Picasso库。从here获取。 语法如下所示:

Picasso.with(getContext()).load(imagePath).into(imageView);

答案 3 :(得分:0)

您可以尝试使用此代码根据您的要求调整图片大小

public Bitmap decodeSampledBitmapFromResource(String Filepath,
                                                  int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(Filepath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(Filepath, options);
}

public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {

    // Raw height and width of image
    int inSampleSize = 1;
    final int height = options.outHeight;
    final int width = options.outWidth;


    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

点击这里 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html