I'm new in designing the layouts. I am getting the bitmap from the server and I'm setting the imageview height such that it look good on every density either ldpi, mdpi, hdpi, xhdpi.
stripImageView.getLayoutParams().height = (int) ((Util.screenWidth(this) * strip.getHeight() / strip.getWidth()) * getResources().getDisplayMetrics().density);
But This code not working for me. Some device images looking very small and Some device it look fine
Try just setting the dimensions in your layout using dp instead of px. Dp (or dip) stands for density independent pixels and will automatically scale based on density.
EDIT: another issue I see is that you're referencing both the screen width and height so your screen aspect ratio would also affect that calculation.
convert them to DIP:
stripImageView.getLayoutParams(). height =
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>,
getResources().getDisplayMetrics());
where HEIGHT is the height in px you want. You could base it off the strip height or something.
Related
I've been working on an app where it is important that drawables are always the same size relative to the display's width, e.g. always 1/8 of the screen height in pixels. I understand how density independent pixels work, but as their goal is to always represent the same physical size, it is pretty much the opposite of what i need. How can I achive this? I have tried using a .png-file with a very high resolution and downscale accordingly, but its always blurry and the quality loss is unbelievable.
Thank you for your help, best regards.
Had the exact same issue. Here's my solution:
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
Double newHeight = 0.125 * displayMetrics.heightPixels;
yourImageView.getLayoutParams().height = newHeight.intValue();
Alternatively, use a LinearLayout. Set your ImageView's weight to 1, and the LinearLayout's weightSum to 8.
if a Custom View of size 500w*600h in dp , remain same as px in (1152w*720h)px screens of android , then what will be view's size in dp and px on screen of (480w*600h)px screens. And how to calculate for different size of View.
The dp / px ratio is based on the density of the screen of the device.
I would encourage you to read the Android docs on the subject.
http://developer.android.com/guide/practices/screens_support.html
Each classification of screen density has a specific px multiplier associated with it i.e. mdpi = px * 1 and hdpi = px * 1.5
Here is a nice little calculator to help you make sense of it:
http://labs.rampinteractive.co.uk/android_dp_px_calculator/
Assuming that you specified your view sizes in dp(same as dip), you can use get an instance of DisplayMetrics to convert the dp to actual pixels for your current device.
A handy function you can add to a utility class for doing conversions:
private static float dipToPixels(Context context, int dip)
{
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, metrics);
}
The developers website simply states that getHeight() will return the bitmap's height, but can someone tell me that is in pixel unit or dp unit?
It's pixel. In Java code you usually work with pixels, e.g. a view's width and height.
After Hours of Experimenting I found that it actually return height in dp units.You can verify it by changing the device screen in emulator.
bitmap.getWidth() returns width in dp unit or densitiy.
To get dpi (density per inch) for a device, use
float dpi = context.getResources().getDisplayMetrics().density;
To convert dp to px
float px = dp * dpi;
and to convert px to dp
float dp = px/dpi;
If you read on how android deals with views this not clear at all. See "Supporting Multiple Screeens". After reading that document I have come to the conclusion that "it depends." (And I'm still guessing as I have not verified my analysis.) If the View was declared with size "wrap_content", "fill_parent", or "dp" then you get "dp", otherwise you get pixels. If you used "dp" then scaling to pixels is achieved by multiplying by
getResources().getDisplayMetrics().density
For 160 dpi screen, this returns 1.0; for 320 pi screen this returns 2.0.
Dividing by getResources()....density.
I have created the activity with a button. I need to specify the height and width of the button like below.
Button btn=new Button(this);
btn.setWidth(100);
btn.setHeight(100);
For multiple screen support is it a right way to specify the size of the view . Guide me to set the height and width to the view.
It really depends on how you want to determine the button size. Basically you have two options
Constants like FILL_PARENT or WRAP_CONTENT
A number
If you choose the 2nd way, you must also choose a strategy, eg
30% of the screen/parent width (so it shrinks/grows depending on the screen actual size)
1 inch on all screens
The former is trivial: just get the width of the target element (either via DisplayMetrics if it's the whole display, or with getWidth() if it's a View) and multiply it by your coefficient.
The latter requires you to know the density of the display, ie how big is a pixel on the device. For example by doubling the density, the pixel width will halve
DENSITY PIXEL SIZE
100 dpi 1/100 in
200 dpi 1/200 in
So if you want your button to be 1 inch wide on all possible devices, you can use
button.setWidth(1 * (int) metrics.xdpi); //1 inch * DisplayMetrics.xdpi
There is a little variant if you know the size in pixels on a MDPI device, ie a device where one DIP is one pixel on an approximately 160 dpi screen. In this case you can use the following
button.setWidth(100 * metrics.density);
This is the javadoc for DisplayMetrics.density:
The logical density of the display. This is a scaling factor for the
Density Independent Pixel unit, where one DIP is one pixel on an
approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
providing the baseline of the system's display. Thus on a 160dpi
screen this density value will be 1; on a 120 dpi screen it would be
.75; etc. This value does not exactly follow the real screen size (as
given by xdpi and ydpi, but rather is used to scale the size of the
overall UI in steps based on gross changes in the display dpi. For
example, a 240x320 screen will have a density of 1 even if its width
is 1.8", 1.3", etc. However, if the screen resolution is increased to
320x480 but the screen size remained 1.5"x2" then the density would be
increased (probably to 1.5).
#Karthick If your button is not used in xml of android layout then you can do below thing for set Button width and Height.
Use Below code to get known what is device height and width.
int public static int LDPI_BUTTONSIZE=100;
int device_height;
int device_width;
WindowManager wm=getWindowManager();;
DisplayMetrics dm=new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
device_width=dm.widthPixels;
device_height=dm.heightPixels;
From Above Code you will decide in which screen of your android device ,for Example you can refer this link of developer click here. in that Refer Table No 3.
Now if your device is LDPI then you can set Button value like this.
Button btn=new Button(this);
btn.setWidth(LDPI_BUTTONSIZE);
btn.setHeight(LDPI_BUTTONSIZE);
For this purpose you need to convert pixels to dp's. You can do this with TypedValue.
Example:
int buttonWidthInPixels = 100;
float buttonWIdthInDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
buttonWidthInPixels, getResources().getDisplayMetrics());
button.setWidth((int) buttonWidthInDp);
If instead you want to convert to SP (for text), use TypedValue.COMPLEX_UNIT_SP as the first parameter for applyDimension(). See this page on the Android dev guide.
I have a image under div id myimage. Now I want to resize it by using change. I want from select box if selected 700X7000 then image's size will be height 700px and width 700px. With out reloading the page.
Can any one help me how can I do this?
Set new new LayoutParams(200, 200) everytime.
screenDensity = getResources().getDisplayMetrics().density;
dimInDP = 200 * (screenDensity/(float)160)
imageTextView.setLayoutParams(new LinearLayout.LayoutParams(dimInDP ,dimInDP ));
EDIT : The Android system takes 160dpi as the reference density for density independent pixel calculation. For example, in your case you need dimension as 200dp. The system takes this as the numbersof pixels you want on a device of screen density 160dpi. So at runtime it scales the width relative to the reference density. Assume you run ur app on a screen density 120. The view width will be scaled to 150 physical pixels.