Hardik Joshi
Hardik Joshi

Reputation: 9507

android image not maintain aspect ratio

Hello i am new in android. Can anybody help me to maintain aspect ratio of image. In my application i select image from SDCARD and display it in imageview by passing intent to next activity. But most of the times when image is big or small then image is not display properly in my imageview.

 BitmapFactory.Options options = new BitmapFactory.Options();  
                options.inTempStorage = new byte[16 * 1024];  
                options.inJustDecodeBounds = true;  
                bitmap = BitmapFactory.decodeStream(getContentResolver()  
                        .openInputStream(mImageCaptureUri));

                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);

                Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 120,
                        120, true);

                newbitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
                Intent photointent = new Intent(MethinkzActivity.this,
                        DisplayImage.class);
                photointent.putExtra("photo", bs.toByteArray());

                startActivity(photointent);

And at other activity i catch that by using following.

    bitmap = BitmapFactory.decodeByteArray(getIntent()
                .getByteArrayExtra("photo"), 0, getIntent()
                .getByteArrayExtra("photo").length);


        final double viewWidthToBitmapWidthRatio = (double) my_image
                .getWidth() / (double) bitmap.getWidth();
        my_image.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
        my_image.setImageBitmap(bitmap);

Please help me to find this.

Upvotes: 1

Views: 2075

Answers (3)

Khan
Khan

Reputation: 7605

you should have to use Scalling Property of ImageView for the ImageView use this property in your xml file use one the scale type for imageview

 android:adjustViewBounds="true"
 android:scaleType="fitXY" or  android:scaleType="centerCrop"

Upvotes: 2

Saurabh Verma
Saurabh Verma

Reputation: 6728

Displaying Bitmaps can be quite tricky at times.

One should follow this tutorial very closely: http://developer.android.com/training/displaying-bitmaps/index.html

Upvotes: 0

jeet
jeet

Reputation: 29199

set ScaleType property of ImageView to "CenterInside" in xml or activity.

my_image.setScaleType(ScaleType.CENTER_INSIDE);

See More on ScaleType

Upvotes: 0

Related Questions