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>
Related
I still have problems with the correct view for the images in my Application. So on my first device (5,2 inches & 480 density) it looks good.
On the second device (5,5 inches & 420 density) the image doesn't fit and it shows white borders.
This is the ImageView in my layout:
<ImageView
android:id="#+id/iv_image"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/tv_topic" />
I placed all my Images in the drawable folder after reading this on a Android Blog:
There are commonly two ways to target all screen DPIs.
1. Easiest way - Make all images to Extra High or Extra Extra High DPI.
Android auto-scales the drawables if the device does not match the drawable DPI. If the only drawables are created in high density, lower DPI screens will down-scale a resource to fit in a layout.
So I implemented all Images in the highest possible resolution ONCE in the drawable folder. Is it necessary to place them all in the specific folders (drawable-ldpi, drawable-mdpi ...)? It would mean that there will be multiple copies of my Image with always the same size.
And yes I read the official documentation of supporting multiple screens a couple times. However I have some problems understanding it.
I advice you to use the layout_weight attribute to keep the constant ratio between the ImageView and the question layout.
Change your layout to something like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="3" />
</LinearLayout>
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" />
I am using an app that has portrait for phone and both portrait and landscape for tablets. However, i recently added a view for splash screen in landscape and both landscape and portrait mode in tablet are pulling mdpi images instead of xhdpi,not sure why this is happening, any clue?
Here's my xml for the same:
<?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:background="#drawable/login_bg"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/image"
android:layout_width="750dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_weight="0.36"
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:src="#drawable/splash_logo" />
</LinearLayout>
Sort of a silly fix but are you positive your tablet is xhdpi? I'm just saying this because a tablet I use to test on is actually ldpi when I just assumed it was xhdpi.
Another thing you can try is creating a new drawable folder drawable-xlarge, which is based on screen size and not pixel density.
I did an android application with splashscreen :
splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splashnine">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:textSize="16sp"
android:text="test" />
</RelativeLayout>
where splashnine.png is very clear with 1333x2000 but the picture is not clear when tha app is launched.
First, you should put different versions of this image to different drawable folders, such as drawable-hdpi, drawable-xhdpi, etc. If you just put the image to drawable folder, it's loaded as an mdpi image and then scaled up.
Second, when you set an image as a background, it's scaled up or down depending on the size of the view. It means that the quality of the image becomes worse because of resizing.
Maybe ,since you use "fill_parent" with layout size parameters, image would be resized according to your device screen dimensions. try to change fill_parent to wrap_content or give DP values.
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/