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.
Related
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);
for example, if a 80dp square layout contains a smaller ImageView, the size of ImageView will not be scaled:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#FFFF00"
android:clipChildren="true">
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
but if the parent is smaller than the ImageView, eg:30dp:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#FFFF00"
android:clipChildren="true">
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
it would be scaled automatically:
but I want the ImageView cropped instead of scaled:
how can I get this crop effect in xml?
There is alot of techniques available.
Try to Add both attribute in your ImageView xml
android:scaleType="matrix"
OR
Use margin-right,left,up,down.
OR
Search Icon size in google and then edit this icon in photoshop give it a fix size of icon .
try adding following attribute in your ImageView xml
android:scaleType="matrix"
hope it helps :)
Image 1 is without rotation,I want image 3 when I rotate ImageView, but something like image 2 is displaying. How can I have the ImageView change its dimensions automatically?
My guess is that after rotation, the width and height of the ImageView is preserved, and I do not want that
P.S. I don't want to change the orientation of the layout
here is the picture(Rectangle is the phone, person is the imageview)
Here is my xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/page">
<ImageView
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>
</FrameLayout>
<!-- I tried all scaleTypes, and set height/width to fill_parent, wrap_content,and match_parent nothing worked-->
any help is appreciated. thanks!
Remove adjustViewBounds and use scaleType fitCenter. It should work. Try this:
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>
Try this:
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:adjustViewBounds="true"
android:rotation="90"
/>
Edit:
android:adjustViewBounds="true"
will set scaleType="fitCenter", so I think you should try scaleType or adjustBounds ,no both.
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>
I have an ImageView defined as:
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-80dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/tester"
android:adjustViewBounds="false" />
and I see this in Android Studio in the layout preview:
And notice the bounds of the ImageView. Is it possible to have the bounds be along the actual edges of the image I am displaying?
<ImageView
android:id="#+id/imageView"
android:src="#drawable/tester"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
Adding the scaleType to centerCrop should fix it for you. You can change the centerCrop to any other suggestions according to your need.
you have to set the scaletype to fitCenter
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-80dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/tester"
android:adjustViewBounds="false"
android:scaleType="fitCenter"
/>
set android:adjustViewBounds="true"
It will set the bounds of the imageView to match the content
I think, the image is too large and you re wraping it so the imageview layout will be larger than you wanted to. Crop the image seems like the only solution you can do. You can try to scale
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.