簡體   English   中英

將從圖庫中選取的圖像調整為圖像視圖的大小

[英]Resize a Image Picked from Gallery to Image View

我的應用程序有一個ImageView活動,我從電話庫中選擇一張圖片,並在此ImageView中設置為用戶的個人資料圖片。

我的問題是,某些圖片在拾取后會使應用程序停止原因太大,我想知道是否有人可以查看我的代碼並幫助我如何調整此拾取圖片的大小,因此在此圖像視圖中設置后,用戶如何裁剪該圖片設置之前的圖片,下面是我的圖片圖片代碼。 如果有人進行必要的更改並給我代碼,我將非常感激,因為我對開發尚不甚了解。 謝謝。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.User);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

    }

不會重寫您的代碼,但這可能很有用

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

我用來縮小位圖的代碼摘錄。 它將hight設置為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;
}

回復晚了非常抱歉

嘗試使用像我在我的應用程序中使用過的whtsapp這樣的圖像壓縮https://www.built.io/blog/2013/03/improving-image-compression-what-weve-learned-from-whatsapp/

您可以使用Picasso庫。 這里得到它。 語法如下所示:

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

您可以根據需要嘗試使用此代碼來調整圖像大小

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM