Why does image has larger size? - android

I'm using such layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/logo"
android:orientation="horizontal" >
The drawable/logo.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/login_logo"
android:gravity="center" />
The drawable/login_logo.png has 280 pixels width, my Galaxy Tab screen width is 600px but in launched app I see the image takes almost all screen width and it is about 500px
Where did I make mistake?
SOLVED
By setting
<uses-sdk android:minSdkVersion="4" />
and putting image to drawable-hdpi

As you are saying it is for galaxy tab so it is coming under drawable-hdpi.So change the resolution of image with photoshop or something and put it inside drawable-hdpi.
and try to use android:layout_width="wrap_content". instead of android:layout_width="fil_parent"

You are setting your picture in background, and the android:background tag uses 9 patch images. So your background always stretches to the width and height of the view. Your layout width is set to fill_parent, so the image width is fill_parent too.
The solution could be to make a 9 patch image of the png you are using with .9.png extension.
Read here for drawing 9 patch images - developer.android.com/guide/developing/tools/draw9patch.html

I suggest that you have a good read of the documentation. If you have placed your image in the drawable-mdpi folder, then in high density screens the image will appear to be 1.5x in size. You will have to provide different resolutions of your image for different screen densities.

Related

Trying to get an ImageView to scale as the screen gets bigger

I'm currently new to Android development, and I'm was trying to get an image to scale for different screen sizes. I made a drawable-hdpi,-mdpi, and drawable-xdpi. In these folders I placed the image but at different sizes for each screen density ( Ex. for xdpi, I made the image bigger). I placed the imageView in the constraint layout and set the height and weight to wrap_content. I was expecting the image to be the same size as the corresponding drawable, but for higher screen densities it was still super small. Any thoughts? I also read about 9 patch images, but I cant just add a larger image to the higher density drawable folders ?
you should change the scaleType of imageview.
if you want to scale image with maintain aspect ratio you should use
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitcenter"/>
u can try another option like centerCrop and fitxy and see the result
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
....
Add android:scaleType="fitXY" to ImageView to adjust the width and Height of view

Correct image size at the source or hardocing the dp size in layout

When you create your xml layout and say you load imageview or a button with a background from a drawable image. Then do you design the orginal drawable image so that it's size will look what you want on screen when using "wrap content"? Or do you just grab whatever size the image is and resize the widget that is using the image by putting something like in the xml
android:width ="100dp" android:height="100dp"
This best way I can suggest you is to get you image designed in proper resolution and put it in appropriate drawable folder. Let's say you are targeted highest resolution is 1080x1920, then get your screen designed in this resolution and slice the image in actual resolution and paste it in "drawable-xxhdpi" folder. It'll work for your targeted screen size and for all other resolutions smaller than the targets resolution.
In situations like this, it's better to specify the dimensions of the view.
When you set a drawable as a background to a View, it automatically takes up the size of that View.
Here's an example:
<TextView
android:layout_width="400dp"
android:layout_height="100dp"
android:background="#drawable/background" />
The background will be the image named background.png/background.xml in your drawable folder, and no matter the size, it will take up the size of the TextView, which is 400dp long and 100dp tall.

How do I make a 320x480 ImageView fill the entire emulator screen (320x480 mdpi in size)?

I'm working with an emulator screen set at 320x480 MDPI. I created a background image in fireworks with the exact size of 320x480 pixels, placed it in the drawable MDPI folder, and set it as an ImageView in hopes it would fill the entire background of the emulator. The problem is the background only fills vertically, but seems to leave empty space equally on the left and right horizontally.
Any idea what I'm doing wrong?
Any reason why you don't just setBackground on your root view?
Either way, filling the background in Android is a troublesome business. I suggest reading this question: Handling different screen resolutions for background images in Android
<ImageView android:layout_width="fill_parent"
android:layout_height ="fill_parent"
android:scaleType="fillXY" />
Note that this will cause it to scale in the X direction to fill the space, which will slightly distort the image.
please don't use a ImageView, just set the android:background property of the root layout in your xml. Look at this example:
<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"
android:background="#drawable/your_image_here" >
.....
</RelativeLayout>
I hope this helps you. Best Regards

Why is wrap_content bigger than real pixel size?

I am trying to understand something. A weird thing that I see is that when I put wrap_content in the width and hight, the image is bigger than the the real px (pixel) size of the image which is inserted. Why is that so?
My code with wrap_content:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:src="#drawable/header" />
and thats my code with exact pixel size of the image:
<ImageView
android:id="#+id/imageView1"
android:layout_width="378px"
android:layout_height="155px"
android:adjustViewBounds="false"
android:src="#drawable/header" />
As you can see, thats the exact pixel size:
Why is that? I thought that wrap_content should wrap the view to the content size so why is it bigger on screen?
If you really need to use the image's pixels as-is, and use the screen actual pixels do the following:
1) Create a res/drawable-nodpi and move your fixed-pixel non-scaling images in there.
2) You then can use wrap_content as layout_width and layout_height, and image pixels will match the device pixels with no upscaling because of dpi.
From http://developer.android.com/guide/practices/screens_support.html, the definition of nodpi :
Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.
A very nice explaintaion for supporting the multiple screens is given at
http://developer.android.com/guide/practices/screens_support.html.
you need to put images in respective folders after scaling.
e.g. if you want 100*100 image then place
75*75 in drawable-ldpi for Low density devices
100*100 in drawable-mdpi for Medium density devices
150*150 in drawable-hdpi for High density devices
200*200 in drawable-xhdpi for Extra High density devices
wrap_content means that you want the image as it as its original size is. i.e if the size of the image is 200*200 then it will show 200*200 and if the image size is 400*400 it will show of the size 400*400.
So the reason you are getting a larger image then what you actually get when you hard code it with real pixels is because of the LARGE SIZE of the image. i.e image is actually large.
From my experience I can say that wrap_content always utilize maximum available size possible. So sometimes it stretch the image & sometimes reduce the size of the image. To use exact image use android:scaleType="fitXY"
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:scaleType="fitXY"
android:src="#drawable/header" />
Update after 1st 2 comments:
as per android docs :
You can specify width and height with exact measurements, though you probably won't want to do this often. More often, you will use one of these constants to set the width or height:
wrap_content tells your view to size itself to the dimensions required by its content
fill_parent (renamed match_parent in API Level 8) tells your view to become as big as its parent view group will allow.
In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or fill_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes. The accepted measurement types are defined in the Available Resources document.
I found the term size itself is important, it autometically resizes the images. thats why I told that from my experience I found sometimes it stretch the image & sometimes reduce the size of the image
You need to see where you put the image. If its in hdpi it will look bigger on screen then if its in hdpi when ising wrap_content. so, in order for it to be the exact size, put it in right library.

Best fit the larger image into android device?

I have a larger image of 1024 x 768. When the image is displayed in the android device as the background image then it gets compressed. Is there any way that the image can be displayed properly in all screen resolution?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "#drawable/titlepage">
</RelativeLayout>
This is the code that I am using for setting the background image. But since the image is quite large it appears to be compressed when viewed on emulator or device.
Any help will be just great.
You are talking about multiple screen support. You need to make 3 types of images, HDPI, MDPI and LDPI. They have all different aspect ratios as well as different sizes.
Check out my tutorial on this subject, should explain everything.
http://wonton-games.blogspot.com/2010/07/tutorial-multiple-screen-support.html
or a simpler method if you just want to keep your aspect ratio is in your xml file using imageView do this
<ImageView
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/background"
android:adjustViewBounds="true">
</ImageView>
What this does is keep the aspect ratio of the image and fit it to the screen. You will get bars on either side if the aspect ratio doesn't match but this is just a quick shortcut way. You might have to play with layout_width/height to make it fit properly.
What you require is to maintain the resolution of the image, that would be the width:height ratio of the image. i am afraid for a 1024*768 the ratio is 1.33 , such image is meant for desktops. usually mobile devices have smaller ratio (w:h). eg: for Samsung galxy S , it would be 0.5, for Xperia X10, HTC Incredible, it would be 0.562. Android device screen sizes may vary. You need to have a suitable version of your image for each mdpi, ldpi and hdpi versions. Even then there is no guarantee that the image aspect ratio will be maintained. Better still have your image to be a 9 patch image, where you can define which part of the image should be stretched and which should be not, here you can preerve important part of the image.
Make an xml drawable file for the bitmap. You can change the gravity to "fill" You will still have issues with screens that are a different aspect ratio, but you can experiment with clamping, tiling, etc to get the look you want.
http://developer.android.com/guide/topics/resources/drawable-resource.html#bitmap-element

Categories

Resources