Imageview not scaling picture - android

I have the following imageview with a image smaller than the RelativeLayout that contains this ImageView. I want the image scalled to fit the width of the imageView but I cannot make this works. For example I
have this relativeLayout with 350dp width:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#drawable/selector_button_grey"
android:clickable="true"
android:padding="5dp">
<ImageView
android:id="#+id/image_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#color/black"
android:src="#drawable/Nachos"
/>
<TextView
android:id="#+id/image_selector_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/image_selector"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textSize="18sp" />
</RelativeLayout>
As you can see in the picture the image is in the center. The problem with fitXY is that the width works, but doesn't change the height. I want somehitng like, fitX. I have also tried centerInside but it looks the same.

I would also use centerCropor similar. If that does not work for you, check this image and see if you can find anything interesting:
If this is nothing for you, consider to check out image customazations in Android:
https://developer.android.com/guide/topics/graphics/2d-graphics.html

Because your Relative layout and Imageview both have wrap content height so you have to make them match parent and make scaletype fitXY..
as follow code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#drawable/selector_button_grey"
android:clickable="true"
android:padding="5dp">
<ImageView
android:id="#+id/image_selector"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="#color/black"
android:src="#drawable/Nachos"
/>
<TextView
android:id="#+id/image_selector_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/image_selector"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textSize="18sp" />
</RelativeLayout>

You should change the android:scaleType attribute to your ImageView.
If you want to display the pictures in proportion, you can use centerCrop.
If you don't want to display the pictures in proportion, you can use fitXY.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#drawable/selector_button_grey"
android:clickable="true"
android:padding="5dp">
<ImageView
android:id="#+id/image_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#color/black"
android:scaleType="fitXY"
android:src="#drawable/Nachos" />
<TextView
android:id="#+id/image_selector_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/image_selector"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textSize="18sp"/>
</RelativeLayout>

Hope this helps!
In my case it is working fine.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#drawable/Nachos">
</RelativeLayout>

There is some padding set on the RelativeLayout they may be also a cause to ImageView not touching the sides in your layout RelativeLayout:
Remove this:
android:padding="5dp"
And if you real want some padding set on Top and Bottom but NOT on the Right and Left so if you want padding Top and Bottom ADD these:
android:paddingTop="5dp"
android:paddingBottom="5dp"
And if you do not want padding at all do not ADD them!

I have used another method. First, I obtaing the dimesions without loading from memory :
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(f), null, bitmapOptions);
BitmapFactory.decodeFile(f.getName(), bitmapOptions);
float scale = (bitmapOptions.outHeight * 1f) / (bitmapOptions.outWidth);
cache.put(image, value);
} catch (Throwable e) {
e.printStackTrace();
}
Then used that value scale
imageView.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(IMAGE_SIZE, Math.round(IMAGE_SIZE * imageData.getScale())));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
imageView.setCropToPadding(true);

Related

Imageview shows border around photo that does not exist in the source image

I'm trying to display a simple image in an imageview and it seems to be adding a border around the photo that isn't there in the source image. I placed the imageview in the parent view and set the image to it, and in the editor you can see a small sliver of the border but when I run it on a device or emulator more of a border appears.
I tried removing a pixel off the size of the photo in case there was a border but that didn't fix it. Below are the photos of the emulator and the editor to show the difference.
Editor:
Emulator:
Any help would be greatly appreciated, thank you.
EDIT: added xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_home_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="frolicindustries.strandstudybible.HomeScreen"
android:weightSum="1"
android:background="#android:color/background_light"
android:orientation="vertical"
android:baselineAligned="false">
<!--android:scaleType="fitCenter"-->
<!--android:cropToPadding="false"-->
<!--android:layout_weight="0.01"-->
<Button
android:text="Button"
android:layout_width="wrap_content"
android:id="#+id/About"
android:background="#drawable/aboutgradient"
android:layout_alignParentBottom="true"
android:layout_height="65sp"
android:layout_marginBottom="1dip"
android:layout_marginTop="1dip"
android:layout_alignRight="#+id/Extras"
android:layout_alignEnd="#+id/Extras"
android:layout_alignLeft="#+id/Bible"
android:layout_alignStart="#+id/Bible" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:id="#+id/Extras"
android:background="#drawable/extragradient"
android:layout_height="65sp"
android:layout_above="#+id/About"
android:layout_marginTop="1dip"
android:layout_alignRight="#+id/WTFW"
android:layout_alignEnd="#+id/WTFW"
android:layout_alignLeft="#+id/Bible"
android:layout_alignStart="#+id/Bible" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:id="#+id/Bible"
android:background="#drawable/biblegradient"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:elevation="0dp"
android:layout_height="65sp"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_above="#+id/WTFW" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:id="#+id/WTFW"
android:background="#drawable/wtfwgradient"
android:layout_height="65sp"
android:layout_above="#+id/Extras"
android:layout_marginTop="1dip"
android:layout_alignRight="#+id/Bible"
android:layout_alignEnd="#+id/Bible"
android:layout_alignLeft="#+id/Bible"
android:layout_alignStart="#+id/Bible" />
<ImageView
android:id="#+id/imageView2"
android:src="#drawable/logo"
android:background="#null"
tools:targetApi="lollipop"
android:layout_width="match_parent"
android:adjustViewBounds="false"
app:srcCompat="#drawable/logo"
android:layout_alignRight="#+id/Bible"
android:layout_alignEnd="#+id/Bible"
android:layout_height="155sp" />
<ImageView
android:layout_width="match_parent"
android:src="#drawable/title"
android:id="#+id/imageView3"
android:layout_weight="0.29"
android:layout_height="wrap_content"
android:layout_above="#+id/Bible"
android:layout_alignRight="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:cropToPadding="false"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_below="#+id/imageView2" />
</RelativeLayout>
Check this,
my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#000"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.charu.its2017huree.MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical">
<ImageView
android:background="#FFF"
android:src="#drawable/amanda"
android:id="#+id/my_image_view"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
I have set an imageView and i found out it seems there is something wrong with it's left and right edges. I have given match_parent to my imageView already.So I add a background color to my imageView to find out what's going on.
But that's not enough for me i want to see the boundaries as well.It's there on your android mobile!
Settings >Developer Options >(scroll bit down) under category Drawing there is a option Show layout boundaries > Tick that option .. mm You get some kind of grids in your mobile.It's normal it will be gone when you untick that option.
Let's jump back to image view and see how it looks now!
Now i can clearly see a background color which i add in behind of the image
Also image view width boundary is fine,it's match_parent.You can see that from your layout boundaries.
MM sounds like image didn't fit properly then i try something with scaleType like android:scaleType="fitXY" and run and see how is it now .. all-right it seems she has taken the full space .. I mean in my imageView :D
This way you can clearly identify what'w wrong with your imageView
what is your image saved as? i would suggest .png extension. I would get the border when my images are jpg or anything else.

How can get the right size images when setting some resource to imageview?

I hope to put some item into a gridview like following :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="40dp" >
<ImageView
android:id="#+id/mudImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/icon_m0_r"
android:visibility="visible" />
<TextView
android:id="#+id/mudTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="fsafdsf"
android:textColor="#color/white"
android:textSize="16dp" />
</LinearLayout>
While the 'mudImg' part got something wrong with the image's size on a true android machine.Actually It never works util I set a indeed num to 'layout_width' and 'layout_height' like '50dp' or something like this.
What I want is show the origin size image and if necessary expended to the size what the parent view left to it.
Now my solution is that calculating the image size with app screen params(height width) and parent view's margins and gridview's numColumns and setting the result by code (since can not do it in xml)
any better idea?
Try this for your ImageView:
<ImageView
android:id="#+id/mudImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/icon_m0_r"
android:visibility="visible" />
adjustViewBounds="true" tells the ImageView to set the height so that the image has the correct aspect ratio using the given width.

Loaded Bitmap doesn't scale to container size

I have a ListView Item that uses a Drawable as a default icon and am allowing an override if an icon is specified. When I load the Bitmap for the specified icon and set it to the ImageView that holds the default Drawable item, the size of the specified icon is far smaller.
Both the Drawable and the specified Icon are 128px x 128px so I "assumed" that they'd both be the same size upon loading.
Here's a screenshot of the problem.
Here is the XML for the ListItem:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dip" >
<ImageView
android:id="#+id/events1_lv1_listitem_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:contentDescription="#string/content_description_default_text"
android:src="#drawable/calendar" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/events1_lv1_listitem_headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/content_description_default_text"
android:gravity="center_vertical"
android:textSize="16sp" />
<TextView
android:id="#+id/events1_lv1_listitem_summary"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:contentDescription="#string/content_description_default_text"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
And here is the snippet where I load the bitmap into the list item icon:
File headerImageFile = new File(src);
Bitmap image = BitmapFactory.decodeStream(new FileInputStream(headerImageFile));
events1_lv1_listitem_icon.setImageBitmap(image);
Any help would be most appreciated!
Avoid wrap_content. Set your images size to dp, and set scaleType to "fitXY".
The following will do for you*
<ImageView
android:id="#+id/events1_lv1_listitem_icon"
android:layout_width="128dp"
android:layout_height="128dp"
android:scaleType="fitXY"
android:layout_marginRight="6dp"
android:contentDescription="#string/content_description_default_text"
android:src="#drawable/calendar" />
Couldn't see your screen shot.
If your icon is 128px * 128px, you can set the layout_width and layout_height to 128px, then set the scaleType to fitXY or centerCrop. Like this:
<ImageView
android:id="#+id/events1_lv1_listitem_icon"
android:layout_width="128px"
android:layout_height="128px"
android:scaleType="fitXY"
android:layout_marginRight="6dip"
android:contentDescription="#string/content_description_default_text"
android:src="#drawable/calendar" />
As to the meaning of scaleType, you can see this link.

ImageView without scaling

I have a ListView that shows rows
This is the xml:
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/white"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".30">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/face"
android:scaleType="centerCrop" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".70"
android:background="#color/white"
android:padding="4dp" >
// Other views...
</LinearLayout>
</LinearLayout>
…
and get this result:
this is the original image
need is the image without scaling, just like this:
I am using android: ScaleType = "centerCrop" I tested with android: adjustViewBounds = "true" and many other parameters, but never successfully
Ideas?
thanks in advance
Set your image to android:src instead of android:background.
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/face"
android:scaleType="centerCrop" />
All views can take a background image
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.
Change android:background="#drawable/face" to android:src="#drawable/face"
Scale type will only work forandroid:src
You can use android:src instead of android:background to set your drawable. Something like:
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/face"
android:scaleType="centerCrop" />
Also, Why not just crop the image using an image editor software like photoshop and use that image instead? Dynamically scaling and manipulating images should be avoided to prevent heavy use of system resources as far as possible.
I think, your problem is inserted weights in LinearLayout and FrameLayout. Please use RelativeLayout without weight. System can not crop your picture because system can not measure ImageView and can not compare him with your picture. In sum system thinks "This is picture and ImageView equal".
I have done so :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="#+id/image"
android:layout_height="50dp"
android:layout_width="match_parent"
android:src="#drawable/photo"
android:scaleType="centerCrop"
android:contentDescription="#string/app_name"
android:layout_alignParentTop="true"
/>
<TextView android:layout_height="150dp"
android:layout_width="match_parent"
android:text="#string/text"
android:layout_below="#id/image"
android:maxLength="350"
android:layout_marginTop="5dp"/>
</RelativeLayout>

How to programmatically get the height & width of one ImageView and set it to another ImageView in android

Here is the code
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<ImageView
android:id="#+id/first_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:src="#drawable/frame" />
<ImageView
android:id="#+id/second_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/first_imageview"
android:layout_alignLeft="#id/first_imageview"
android:scaleType="fitXY" />
</RelativeLayout>
Now i need to detect the height & width of first_imageview and then set it to second_image_view. How to do it?
you can set
android:layout_alignRight="#id/inside_imageview"
android:layout_alignBottom="#id/inside_imageview"
This will ensure that second image view has same size as first one.
Something like:
second_image_view.getLayoutParams().height = first_image_view.getHeight();
second_image_view.getLayoutParams().width = first_image_view.getWidth();
should work.
This line of code should do it:
secondImageView.setLayoutParams(new ViewGroup.LayoutParams(
firstImageView.getWidth(), firstImageView.getHeight()));
And if you wish to use xml to do so, you can follow Chitranshu answer.

Categories

Resources