Image deforming when setting on ImageView

In the app I'm delevoping, the user is able to get a photo from gallery or to take one directly from the camera, to set it as profile picture.

Then, this photo is shown in the profile page, where is placed in a way like this:

Image deforming when setting on ImageView

My testing phone is a HTC Sensation with KitKat 4.4.4. Here the pic shows Ok, but in my personal device, Galaxy S5 with 5.0, the pic doesn't show well. It fit's right the entire space, but appears deformed. If the image is in landscape, it get's narrowed, and if is in portrait, it get's broad.

This is the code I use for this proposal, in this case, taking the pic from the camera:

/*Get's data from onActivityResult*/ Bitmap srcBmp = (Bitmap) data.getExtras().get("data"); Bitmap landBmp = null; /*If it is square or portrait*/ if (srcBmp.getWidth() <= srcBmp.getHeight()) { landBmp = Bitmap.createBitmap( srcBmp, 0, srcBmp.getHeight()/4, srcBmp.getWidth(), srcBmp.getHeight() /2 ); /*If is on landscape*/ } else { landBmp = srcBmp.copy(srcBmp.getConfig(), true); } /*Get screen dimensions*/ Display display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int screenWidth = size.x; int screenHeight = size.y; /*Bitmap scaling*/ int picOriginalWidth = srcBmp.getWidth(); int picOriginalHeight = srcBmp.getHeight(); Bitmap fullbitmap = Bitmap.createScaledBitmap(landBmp, screenWidth, (originalHeight*screenWidth)/originalWidth, true); //After this I save the pic on memory, this works fine so isn't relevant ...

So, maybe some of the operations need a correction to get well fitted, or there is a better way to do this?


Some things modificated in the above text. The problem is not that the Image doesn't fit the ImageView space. In fact, this is well done. The problem is that the image is getting resized and deformed.

-------------Problems Reply------------

Try this:

public final class FitWidthImageView extends ImageView {

public FitWidthImageView(Context context) {

public FitWidthImageView(Context context, AttributeSet attrs) {
super(context, attrs);

public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable();
if (drawable == null) {
setMeasuredDimension(0, 0);
} else {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
setMeasuredDimension(width, height);
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

It´s just an implementation of the ImageView widget that fits the screen width.

Using centerCrop and adjustViewBounds does the job.

android:layout_alignParentTop="true" />

Category:android Views:7 Time:2018-09-14
Tags: android

Related post

  • how to change the background or the image source when a clickable imageview in android is clicked? 2011-07-01

    I set android:clickable="true" to an imageview in my android app. I want to change the background or the image source when it is clicked, how could i do that? --------------Solutions------------- Try this: myImageView.setOnClickListener(MyOnClickHand

  • objc_msgsend when setting cell.imageView.image 2010-03-08

    this code works fine until I start scrolling: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWi

  • android how to download an 1mb image file and set to ImageView 2011-06-13

    I am trying to download an 1mb image file and then save to bitmap to populate an imageview. but shows error 06-13 13:39:48.149: ERROR/AndroidRuntime(1782): java.lang.OutOfMemoryError: bitmap size exceeds VM budget How can I download a large image? --

  • check which image has been set to imageview 2012-01-27

    I have used the animated images on image view concept. It is working fine.Now whenever when I touch that image view I want to check which image is placed on that image view. How should I do this. I have used the following code to make the images anim

  • NullPointerException when setting bitmap in imageview 2011-03-14

    Hi all i'm getting a NPE when setting an ImageView a bitmap. The bitmap has been decoded from a bytearray and is not null or empty. any ideas? thanks public class LoadPic extends Activity{ private static final String TAG = "Loadpic"; private ImageVie

  • SC.ImageView throws error when setting value 2011-04-17

    When setting the value property of a subclassed SproutCore image view (SC.ImageView) a runtime error is thrown: In Safari and Firefox: TypeError: Result of expression 'elem.setAttribute' [undefined] is not a function. In Chrome: Uncaught TypeError: O

  • How to check does image set in ImageView 2010-09-22

    How can i check does image set in ImageView or no, exists standard methods? ImageView imgV; imgV = (ImageView)findViewById(; if(imgV != set) ///!!!!! imgV.setImageBitmap(mBitmap); else imgV.setImageBitmap(null); --------------Solutions

  • jquery vertical align when page loads with image's display set to none? 2012-03-17

    I am using JQuery Cycle to produce a slideshow of images. The plugin uses display:none to hide the images that aren't being shown yet, with the first image automatically having display:block on page load. I have this bit of JQuery code to vertically

  • How can you prevent Google Chrome from hitting the server when setting background image css? 2012-04-15

    When you set a background image (you could possibly include using an img tag as well - I haven't tested that though) in Google Chrome and the server isn't asking for it to be cached, Google Chrome will hit the server asking for the image again (even

  • Images appear blurry/noisy when set as Desktop Wallpaper 2014-02-24

    This is my first post on the EightForums, and I hope you can be of assistance. For the past few days, any images that I try to set as my desktop wallpaper appear blurry, noisy, and distorted when set as my desktop wallpaper. Any older images on my co

  • Image "moves" when varying heightForRowAtIndexPath in UITableViewCell 2010-03-11

    I have a table view with varying height, as defined in the heightForRowAtIndexPath. For some very odd reason, the image is "indented" to the right based on the height; if the height is low enough, the image is stuck to the left side of the cell, but

  • Setting an ImageView resource as a Drawable 2010-05-02

    I am trying to create a drawable in code and change the color based on some criteria. When I try and set the Drawable as the background of the ImageView it displays but won't let me set any padding. I realized I need to set the ImageView image via th

  • bad bitmap error when setting Uri 2010-09-10

    I would like to make an ImageView display an image on a website. So I create a new ImageView and execute imgView.setImageURI(uri); When I launch the app the image does not appear and I get the error, "resolveUri failed on bad Bitmap Uri (uri)". Any i

  • Change source image for image view when pressed 2010-11-16

    I have a scroll view with lots of image buttons. I want to change the image for an image button when it's pressed. The thing is that I want the image to remain until another image button is pressed. That's why I couldn't use a selector. What is the b

  • How to keep background image size when software keyboard show 2011-05-11

    When the software keyboard shows, it resizes my layout and thus squeezes the background image together. My question is basically a duplicate of this question, which is explained in a very good way: Android: Soft Keyboard resizes background image Howe

  • Monodroid will not deploy when using an ImageView 2011-06-07

    I have this in my Main.axml; <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" android:layout_height="fill_parent" android:layout_width="fill_parent" > <Button android:id=

  • iPhone UITableView image background when table is empty 2012-01-18

    I would like to show an image background when my UITableView is empty. Currently I tried adding a UIImageView to my view controller that contains a table, but XCode doesn't allow it. Is there a good way of doing it? --------------Solutions-----------

  • How to set the imageView property in a UITableCellView to show only a background color? 2012-03-05

    I have a custom class inheriting from UITableViewCell class that shows either an image (left to the title) or a generic dark-colored square if the image is not available). The following code shows a dark square on a light-colored cell background: ima

  • How to replace the image of UIImageView with another imageView 2012-04-13

    In my code first I am setting the image and later on when clicked on imageView I want to add shadow to existing image. How to do it? --------------Solutions------------- I assume you have an UIView with an image - lets say it is called myImageView -(

  • CSS: Why do I have a red underline on image links when hovered? 2009-10-31

    On my web site, I have the following CSS: a:hover{color:red;border-bottom:1px solid} This displays a red underline on text links when hovered, which is desired. However, this also displays a red underline on image links when hovered, which is NOT des

  • getting image size when image zoomed by user 2010-08-16

    how can i get the image size when it's zoomed by user, i write these on my project : - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return imageView; } -(void) didLoad{ UIImageView *tempImageView = [[UIImageView alloc] initWithIm

  • Image resized when page displayed inside of jQuery Dialog and iFrame 2011-01-31

    I am having weird display issues using the jQuery dialog with an iframe in Firefox and IE8. I have tested the page itself outside of the iframe and the problems are not reproduced, only when inside of the jQuery dialog. Issue image actual size: 300x2

  • Setting an UIImageView's frame position relative to parent view in Interface Builder is different when set in code? 2011-03-15

    I have a view controller and a view. I then have a UIImageView created through IB and position using the IB Inspector. Setting the UIImageView's frame to x and y coordinatates to 0 results in the image being placed top left of the view, below the sta

  • NinePatchDrawable problem when set as a ListView item background 2011-06-27

    I've got a ListView which gets populated by items programmatically using ListAdapter. As a layout for a single item, I've got an XML defined. Then, I set up some NinePatchDrawable's in my code and dynamically pop them as a background for a LinearLayo

  • image lost when using ems to resize 2011-11-29

    I'm running into an issue with my template that seems strange to me: when I use ems to define image height/width, it will not display, while setting the image size using pixels works fine. Using ems to define padding and margins have no effect, eithe

Copyright (C), All Rights Reserved.

processed in 0.114 (s). 11 q(s)