ImageView, why different size? - java

First thing, I know many people have asked same thing, but this one is nearly same but I have few more questions.
I have an image of 48px x 48px, so if I set this image in ImageView with
layout_width="wrap_content"
layout_height="wrap_content"
then image looks bigger but if I use following fixed size, it gets smaller
layout_width="48dp"
layout_height="48dp"
I think it's now showing properly(not pixelated ?) with 48dp, but why it got bigger if I use wrap_content ? And if the image size through wrap_content is correct then whats the point of using dp(if it make image smaller) ?

48dp (or 48dip) stands for "Density Independent Pixel". This means that the physical size of the image as displayed on the screen of an Android should be the same regardless of screen density and size. In order to support this, Android adjusts the actual pixel size displayed based on screen density. Your findings of different sizes will vary from device to device.
The conversion of dp units to screen pixels is: pixels = dps * (density / 160). For example, on 240 dpi screen, 1dp would equal 1.5 physical pixels. Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

This probably happens because you don't explicitly set the scaleType. Set your scaleType in XML as follows and you should be fine:
android:scaleType="center"

Try this
<ImageView
android:id="#+id/seprator2"
layout_width="48dp"
layout_height="48dp"
android:scaleType="fitXY"
android:src="#drawable/your_image" />
And if still it shows small then your image must contains alpha around it.

If the image is larger than the view, then you could scale it using:
android:scaleType="center"

Related

ImageView Scaling for No Apparent Reason

I'm using Android Studio, and have created an ImageView in activity_mail.xml. The layout width and height are set to "match_parent". The scaleType is set to "center" and the source image is 70x70 pixels. When I run the app in the emulator the Image appears on the screen larger than 70x70 pixels. I mean it takes up more than 70x70 pixels on the screen of the phone. I don't know why it's doing this. The app is exclusively in Landscape mode, that might be relevant. Forgive me if I have included extraneous details, I genuinely don't know what information would be relevant to include. I intend to make it so that the image takes up 70x70 pixels on the screen of the phone. What is causing the unintended result, and how could I fix it?
change ScaleType, maybe fit_center instead of just center + android:adjustViewBounds="true” will fit your purposes... another way is to create ImageView with wrap_content sizes placed in some container (e.g. RelativeLayout) with match_parent sizes
also check this visual guide for ScaleType
note that 70px image will be big on devices with HD resolution and significantly smaller on those with e.g. full HD. you should have few versions of your image in proper density buckets (mdpi, hdpi etc.) or just download proper size if your image comes frome some API, so then you can say that you have image with 70dp dimension, not 70px

Why is the dimen resource inconsistent and how to get the correct value?

I was writing an Android app. I basically created the following dimen resource:
<dimen name="button_text_size">11pt</dimen>
And I use it in both my xml and Java code to create a Button and a TextView which text size is 11pt. Here is how I did this:
XML
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:textSize="#dimen/button_text_size"
android:text="secret!"
android:layout_margin="#dimen/button_margin"
android:background="secret!"
android:layout_weight="1"
android:onClick="secret!"/>
Java code
TextView text = new TextView (this);
text.setLayoutParams (new LinearLayout.LayoutParams (0, ViewGroup.LayoutParams.WRAP_CONTENT, 3));
text.setText (secret);
text.setTextSize (getResources().getDimension (R.dimen.button_text_size));
In theory, the text of the two views should be the same size. But the text view's text size appears to be larger! I don't know why this is happening. I guess it is because of some problems with the units but what is actually happening? How to get the correct values?
try this
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.button_text_size));
and it better to use sp( "scaled pixel") as text size as per android documentation
http://developer.android.com/intl/es/reference/android/widget/Button.html
Use dp or dip instead of pt
<dimen name="button_text_size">11dp</dimen>
dp/dip: Density-independent Pixels - An abstract unit that is based on the
physical density of the screen. These units are relative to a 160 dpi
(dots per inch) screen, on which 1dp is roughly equal to 1px. When
running on a higher density screen, the number of pixels used to draw
1dp is scaled up by a factor appropriate for the screen's dpi.
Likewise, when on a lower density screen, the number of pixels used
for 1dp is scaled down.
On the other hand
pt:Points - 1/72 of an inch based on the physical size of the screen.
px: Pixels - Corresponds to actual pixels on the screen. This unit of
measure is not recommended because the actual representation can vary
across devices;
So pt and px will vary screen to screen while dp**/dip will manage according to the screen.**..if you want consistent size across different screen than go for dp/dip
For more on DIMENSIONS refer
Therefore change from 11pt to 11dp in XML this will help
But dynamically it considered as floating value in terms of px...so go for solution provide by pavan as in
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.button_text_size));
Either go for one of the below values.
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,getResources().getDimension(R.dimen.button_text_size));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,getResources().getDimension(R.dimen.button_text_size));

Correct calculation from ldpi dp values for mdpi and so on

I'm having a gridview for which I determine the cell size like this (in the xml file):
android:columnWidth="#dimen/gridview_cell_dimen"
As you can see I am getting the dp dimension from my dimens folders (values-ldpi, values-hdpi,...).
My question now is - how to determine the correct dp sizes?
For ldpi I simply use 20dp in this case, which perfectly fits the size from my smartphone. What would be the correct forumla to see which size would be correctly suitable for bigger screen resolutions like mdpi, hdpi, xhdpi, xxdpi?
Is there a rule of thumb which says: "Increase the size by 120%?" for example? So that I can say that the best resolution for mdpi would be: 24dp ? and for hdpi: 28dp in my case?
Thanks in advance!
using dp as the unit is independent of the density of the screen, so you can stick to one value for all screen sizes.
From the Documentation:
Density-independent pixel (dp) A virtual pixel unit that you should
use when defining UI layout, to express layout dimensions or position
in a density-independent way. The density-independent pixel is
equivalent to one physical pixel on a 160 dpi screen, which is the
baseline density assumed by the system for a "medium" density screen.
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
The conversion of dp units to screen pixels is simple: px = dp * (dpi
/ 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels. You should always use dp units when defining your
application's UI, to ensure proper display of your UI on screens with
different densities.

Splash screen which fills the whole samrtphone screen, but not crop

I want to have a splash screen, something like a full picture, which doesn't crop in heigth or width on different smartphone screens.
Now I achieved a splash screen with android:scaleType="fitXY", but now the image is cropped on top or bottom or if the devices screen size changes to another aspect ratio it is cropped on the left and right.
What do I have to do? I've already read the android developer article Supporting Multiple Screens, but I don't get it how to achieve this.
A simple picture in the middle of the screen is just simple to get, but a picture which fills the screen is hard to get. Can you help me pls?
you should use center_crop per this purpose. From the doc
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
larger than the corresponding dimension of the view (minus padding).
There is no way to create one single asset and expect it to do not be cropper and to do not create black areas when the application is deployed in different screen sizes.
The android platform is designed to work dynamically with multiple screen sizes that any manufacture can change at any time, including new resolutions that you haven't thought about it yet.
Android can specify minimums for screen hight/width categories in which your resources will fall, but those are generics.
In order to use them, you will have to specify qualifiers in your drawables and create a different splash screen for every qualifier, as for example if you use drawable-w420dp, all the resources there will be used when the screen has a minimum width of 420dp (notice that are not pixels)
So you have two options:
You can use one single splash image and design margins of that image flexible enough in order to cope with the image being cropped in certain cases. You can play with different scaleTypes in your ImageView and take as a reference this website http://etcodehome.blogspot.co.uk/2011/05/android-imageview-scaletype-samples.html even though as commented before, "center-crop" will be your best shot.
You can programatically use a specific image for a specific resolution.
2.1 Put in the assets directory, all the splash images that you want for all the specific resolutions or aspect ratios that you want to use
2.1 Get the screen size of the device with Get screen dimensions in pixels
2.2 Now you can load from the assets the image that you want dynamically
Use the below code
android:layout_width="match_parent"
android:layout_height="match_parent"
which will fill the entire screen.
Try Using Width and Height of image to "match_parent"

Android: Scale fixed Layout down

i want to scale my android application, basically i developed it for 800x480 display with a AbsoluteLayout. This is necessary for my Application, i can't change it.
Now the Problem:
When i start my app in the Emulator the buttons disappear and the images are extremly large. I thought android would scale applications with a fixed size down by default, but it does not work. I already tried to manipulate the manifest but this did not work.
I use a ImageView component for graphics.
Targeting Android 2.1
Cheers
Felix
It is definitely not ideal to use AbsoluteLayout. But, if you want to just push through with it, you should switch the units of all your co-ordinates and sizes away from px (pixels) to dp (density independent pixels). You will have to scale all of your existing co-ordinates by a factor of 2/3 to start, since 1 dp = 1.5px at the density that your layout targets (hdpi).
You will need to explicitly specify the sizes of all your images and layouts. If, for example, you had a button that was 30px wide and 120px tall, then it will become 20dp wide and 80dp tall.
Of course, the images won't look great on smaller (mdpi) screens, since they will be scaled to 2/3 size. Also, some devices are fixed to landscape mode, where you will definitely encounter layout problems. So it's not pretty, but it may get you over the finish line, depending on your requirements.

Categories

Resources