There are many images that appear in the ListView.
My list_view_item.xml (image in ivEventImage):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/tvText"
android:autoLink="web"
android:linksClickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/ivEventImage"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#null"
android:layout_width="match_parent"
android:layout_height="match_parent" />
They have different sizes - 640 x 480, 1280 x 720, 1920x1080, 1080x1920 and others. In addition to different sizes, different orientation - portrait or landscape.
And all images in different aspect ratio.
How to make the display of images in ImageView, BUT with an aspect ratio of 3:2 and if the image height not fit to show only the middle part of the image. But not to break ratio.
Black square - ivEventImage.
Left - 1280x720, right - 1080x1920 (for example).
Use centerCrop instead of fitXY for android:scaleType as below...
android:scaleType="centerCrop"
So, ImageView xml will be...
<ImageView
android:id="#+id/ivEventImage"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#null"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Related
Problem: trying to give multi device support screen for a simple layout but in Nexus 6 its creating the problem,means not showing fit.
It's side is cutting down where as per as docs for nexus-6 resolution is 1440*2560 and image should be in drawable-xxxhdpi .so i keep a image with resolution of 1440*2560.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/imv" />
</RelativeLayout>
if i ll give match_parent so obvious it will stretch the image.
Please help me to get out of it.
Here is screen shots of screen -
http://s10.postimg.org/43od5j5h5/screen_shot_layout.png
Set the width of the Image to fill_parent (you can set margins if you want a bit smaller imageView or you could also just set a fixed width)
Use android:adjustViewBounds to keep aspect ratio
Set height to wrap_content (it will take whatever it takes to maintain aspect ratio)
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/icon"
android:id="#+id/imv"
android:scaleType="fitCenter" />
If you want to fit the image on the imageview, replace android:src with android:background in the ImageView in the xml code.
Try to use these lines of code in the layout
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:scaleType="fitXY"
android:id="#+id/imv" />
I have a background image which should be scaled on the height, but keep its aspect ratio. I tried it with this code:
<ImageView
android:id="#+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg" />
This works on phones in portrait mode, but when I test it on a tablet (default landscape), it scales the width and cuts the image on top and bottom. Is this because of the xml code or because of the landscape?
Try something like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/background_image"/>
<!-- Your other views -->
</RelativeLayout>
In my app, I am using an image. I do not want the width of image to exceed the width of the screen in any screen size of any density. How to achieve that and what will be issues as I am using only one image for all android densities instead of images of different resolutions?
try this, but you will need a resized image for each resolution
hdpi, ldpi, mdpi and xdpi resolutions
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/title_bar"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/YourPicSource" />
</RelativeLayout>
I want to resize an image to fit to screen that it fills the complete width. The hight should be set in aspect ratio.
For example the image dimentions are widthxheight 10x5 and the phone is 400 width. the image should be displayed in 200x400.
I played around with some settings
android:scaleType="center" should be the correct setting according to documentation but seems to have no effect,
android:scaleType="centerCrop" fits the hight and makes the width bigger than the screen.
heres my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageViewFrage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="1dp"
android:scaleType="centerCrop"
android:src="#drawable/bg_striped_img" />
</LinearLayout>
</LinearLayout>
thanks alot
You should try android:scaleType="centerInside".
According to the documentation (http://developer.android.com/reference/android/widget/ImageView.ScaleType.html):
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view.
ok wont work. that sucks. found this soli
http://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/
I have the following android layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTopLevel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/orbitImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:scaleType="fitStart"
android:src="#drawable/earthorbit"
android:padding="0dp"
android:background="#android:color/white"
/>
<TextView
android:id="#+id/positionDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:layout_below="#id/orbitImage"
android:text="should appear below image"
/>
</RelativeLayout>
The image scales down quite a bit, but since the aspect ratio is maintained, it only takes up the top half of the screen.
The problem is that the image view itself has the original height, which takes the entire screen and the text never shows up.
(So the original image is about 620 X 430, the scaled image is about 250 X 200, but the image view is about 250 X 430)
Is there a way to get the size of the image view to exactly match the scaled size of the image?
It looks similar to this problem: Android ImageView size not scaling with source image, to which to the solution was to add android:adjustViewBounds="true" to the ImageView
Since the sizes are fixed by the image file in resources, then there is no reason why you should not be able to fix the height on your ImageView: android:layout_height="200dp"