change image size wih jquery - android

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.

Related

What is the resolution of default layout in Android?

I am just using simple default layout and not adding any of these layout versions; l-dpi-Layout,m-dpi-layout etc.. then what exactly the resolution of my default layout is?
Let say i add an image and define its width-height as 20dp X 20dp. This image is looking perfect in layout Preview screen, but would it look same in 1440X2560 screen or 1920X1080 screen.
My concerns are:
what exactly is the resolution of default layout is?
How would android know that the 20 dpi i am defining fits best for 1080X1920 resolution and 27dpi fits best 1440X2560 screen, when i am writing 20dpi in my default layout which perfectly fits in layout preview screen
The system assumes a target device with a density of 160dpi. When you define the size of yout image with dp unit, Android adapts the size according with the resolution of the device. For example, if you put an image of 100dp x 100dp in a 160dpi device it will take 100 x100 pixels, but in a device with a 640dpi screen it will take 400 x 400 pixel
By default when you create a template app, your main content layout has two parameters values as layout_width=match_parent and layout_height=match_parent. match_parent value means whatever the screen dpi size the mobile has, it stretches all the way to its size of width and height respectively which is calculated by android automatically when it runs its layout pass to position views (including padding). So in this context the default resolution of your layout is what your mobile has minus the size of statusBar and navigationBar.
Android translates the DPI value you enter for your width and height into number of pixels internally to size your views. You can also put the size of your view into number of pixels but its not recommended.

Android xml sp, dp , px issue

I know there is a lot of stuff related to how does android store images. and what does mhdpi xhdmi lhdpi etc means
But my question is a bit different. actually i am working on a screen whose height and weight in px is 720*1020 px. this is not complete HD, this is sort of SD screen. now i designed images for 720*1080 px with size equal to 520*370 px this is the image size. and i have stored the image in xhdmi
The Issue is
When i put the image on screen and give it
android:layout_height = "wrap_content"
android:layout_width = "wrap_content"
the image is displayed on the screen but it shrinks a to half the original size, so i tried giving the height and width equal to 520px and 370px respectively to the layout then i get the original size of image
My question is:
What size should my image contain so that if i wrap content the height and weight, it gets the original size.
In your case the image is resized based on the screen density of the display. Because you placed the image in a higher density folder than your phone is using it will be downscaled. The forth response from How to resize the bitmap image according to mdpi,hdpi and screen size of the mobile explains how the images scale depending on screen density.
You can consider the density on your device, the size you expect to be and increase the size with a multiplier so you can save it in xhdpi folder.

Image Height Appear Different on different density

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.

Image actual height differences

I have a bitmap with the dimensions of 537 * 233. This is displayed in my fragment.
I am calculating the height of this bitmap through code as well.
I came to know that simply using,
image.getHeight() will always return 0.
Then I found that putting the same in overridden method onGlobalLayout() will give the actual height.
I did that. See my SO post for link.
Now the height I am getting is 155.
I also tried getting the height with,
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.chart);
imgHeight = bd.getBitmap().getHeight();
Again the height I am getting is 155.
According to the above dimensions, the height should be 233.
I am seeing the same height in emulator too.
Why is the difference and/or what I consider to be its actual height ?
UPDATE:
ok, my chart was in drawable-hdpi and the density of my device is 160. So when I put the chart image in drawable folder, I got the correct height. But then if the chart height is fixed (233), why in some devices I am getting the chart height big enough to overlap my bottom timeline. Although I know a bit that this may be because of approximate values and not accurate values (density, resolution) that is causing the in-differences. But then, Any ideas how to fix that ?
First you know mdpi = 1, hdpi = 1.5 and xhdpi = 2.
Lets say you have an image in mdpi folder with width = 100px.
On mdpi device the image width will be 100x1 = 100px,
On hdpi device 100x1.5 = 150px,
On xhdpi device 100x2 = 200px.
if you dont have the image in hdpi or xhdpi folders the android system will scale them.
So when you have an Image in hdpi folder and you run the app on medium density device (mdpi), the android will scale down the image by 1.5. 233 / 1.5 = 155.
The same will happend if run the app to hdpi device you will get an image with ~310 width.
So, to avoid the scaling i suggest to put the image in drawable-nodpi folder (the images in this folder will not scaled by android system).
PS: if you put the image in drawable folder and run in mdpi device the image will not scaled because drawable folder = drawable-mdpi

Specify height width of the view to support multiple screen sizes?

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.

Categories

Resources