Manish
Manish

Reputation: 1326

Image Scaling Android

I'm showing images from gallery to Imageview.

I'm using below code

 Bitmap background = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
                float originalWidth = bMap.getWidth(), originalHeight = bMap.getHeight();
                Canvas canvas = new Canvas(background);
                float scale = width/originalWidth;
                float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
                Matrix transformation = new Matrix();
                transformation.postTranslate(xTranslation, yTranslation);
                transformation.preScale(scale, scale);
                Paint paint = new Paint();
                paint.setFilterBitmap(true);
                canvas.drawBitmap(bMap, transformation, paint);
                imageView1.setImageBitmap(background);

Case 1: working.

Original Image

Output of above code.

Case 2: not working

Original Image.

Output of above code.

in case 2 why image is not getting scaled properly, to fill the imageview? Please let me know where I'm going wrong.

Upvotes: 1

Views: 173

Answers (3)

cybergen
cybergen

Reputation: 3157

Use the scaleType attribute on your ImageView in your layout xml file.

Example:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop" />

Then you can set your image on the ImageView using setImageUri(Uri uri) (I assume you have the Uri of the image to show).

Upvotes: 0

James Wyman
James Wyman

Reputation: 78

Are you wanting it to stretch? Use fitxy.

Upvotes: 0

Trash Can
Trash Can

Reputation: 6814

Android already provides a method to create scaled bitmap. check this out LINK

Upvotes: 1

Related Questions