I am trying to create a simple android app in Java but I have hit a bump in the road.
I have an Array List that contains many Bitmaps and I am drawing them to the canvas but I am using the measurement of pixels and that doesn't come out right on most devices.
So is there any way to find out the size of my Graphic in dp and scale it to the screen density/size with dp and position it with dp or dip.
My Graphics ( Squares ) :
Graphic size in ldpi = 50px
Graphic size in mdpi = 75px
Graphic size in hdpi = 100px
Everyting else in the code is fine apart from it comes out looking weird?
Any help would be appreciated.
James.
From the android developer doc:
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
see this for more info.
/**
* Convert dp in pixels
* #param dp
* #return
*/
public int getPx(int dp){
float scale = getResources().getDisplayMetrics().density;
return((int) (dp * scale + 0.5f));
}
Related
I have an application running on android on which I have to draw a grid. I would like the cells of the grid to be of the same size on all devices, and I've found this method to do that:
float scale = getResources().getDisplayMetrics().density;
float SIZE = DESIRED_DP_VALUE * scale + 0.5f;
DESIRED_DP_VALUE is a value I set for the cells dimension.
I've tried the app on two smartphones and a tablet: the tablet and one of the smartphones have the cells of the same size, while the other smartphone doesn't.
This is the method I use to draw the grid:
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
int count=0;
while(count<=row){
float coordinate=count*SIZE;
canvas.drawLine(coordinate,0,coordinate,column*SIZE,whitePaint);
count++;
}
count=0;
while(count<=column){
float coordinate=count*SIZE;
canvas.drawLine(0,coordinate,row*SIZE,coordinate,whitePaint);
count++;
}
}
The grid is draw correctly on each device.
Where could the problem be?
I think you are mixing some concepts.
The size in pixels is that, how many dots are counting per with or height.
Every device has its own screen size measured in inches and the amount of pixels or dots able to draw in width and height.
The relationship between dimension and pixels is the density.
So, what do you want to do?
Draw an square which shows the same physical size on the screen or a square with the same amount of pixels?
If you want to draw based on inches, take it that way.
For drawing an one inch square, for a device with a density of 300 pixels per inch you need 300 pixels.
If the device is 150 pixels per inch, you must draw a 150 pixels square.
So, you need to keep fixed the size in inches, not the desired DP.
i'm planning to create a simple list on my game that contains stuffs.. here's my idea.. it would looked like this
on the left side of the list, there are 3 images (look at the red arrow mark lol), that will be put on the list
the problem is, the size of these images are different..so is there any solution to create something like an image thumbnail in LibGdx so that the list will look nice like this?
I might be miss understanding you but can't you just draw the original in the size you want?
Texture t = new Texture("someImage");
//Now let's say those list rows are 100 units tall and you want some room on the edges.
float height = 80;
//Calculate width taking aspect ratio in consideration
float width = height * ((float)t.width / (float)t.height);
Now you can draw the image using those sizes. Each image will have a height of 80 and scale correctly.
If you want all the images have the same size you just draw it using the size you want. But then images gets stretched obviously. A third option would be the create your thumbnails manually.
I am creating an Android app and want to know how to scale my images, buttons, etc. to fit the screen size. I am drawing my objects using RectF's and Canvas. I am running my screens through the View class and am not using XML. I have already posted on here but I am still very stuck on this issue and it is disabling me from releasing my app. If someone could give me some quick instruction over skype or teamview I would be so happy!
Thanks guys :)
EDIT
titleBounds.set(screenWidth / 2 - screenWidth * 1/3 * density, 50 * density, screenWidth - titleBounds.left * density, titleBounds.top + screenHeight * 1/8 * density);
This is my rectF, I just want it to sit top middle on the home screen.
The density of a screen is determined by the amount of pixels and the physical size. A low density screen means there are not very many pixels per physical inch of screen. So something like a tablet, because of the big physical size, might have a low pixel density. New phones on the other hand, have a high number of pixels and yet a relatively small size screen, giving them a very high density.
You'll need to multiply your RectF values by the screen density, which will give you buttons etc that are approximately the same physical size across all screens. The screen density is a float value ranging from 0.75 (low density) to 4 (very very high density).
So if your button is about 1 inch wide on a phone, using the density multiplier will give you a button that is about 1 inch wide on a tablet as well.
You can get a density multiplier from the DisplayMetrics class:
http://developer.android.com/reference/android/util/DisplayMetrics.html#density
You get the DisplayMetrics from the context resources:
getResources().getDisplayMetrics()
So your RectF would then be created as:
float density=getResources().getDisplayMetrics().density;
RectF bounds=new RectF(left*density, top*density, right*density, bottom*density);
There's a whole lot of information on density here:
getting the screen density programmatically in android?
I've been positioning things (bitmaps, text) on a surface view using pixels. For example, to centre something I would take half of the width and height of the rectangle representing the screen. Screen width and height are being returned as expected. My phone (480*800) is reporting the available screen as 442*800, with density 1.5, and running on a 240*320 emulator the available screen is reported as 221*320, density 0.75. All as expected.
But these values, surely, are pixels, and so surely to centre something I should first multiply by the density factor before halving. The strange thing is everything is centred perfectly, on both the low res emulator and my high res phone. This doesn't make sense to me.
On both screens, in order to get text to appear the same, I set the size using a density scaled value, as I'd expect, and this works.
In the manifest I've declared support for all screen sizes (although I was seeing the same 'correct' behaviour before I did this as well)
Why are the pixel values 'working' without me adjusting them for density? I'm now very confused.
A pixel is a pixel is a pixel!
It is the lowest common denominator of screens and sizes.
Density is how many pixels are packed into a unit of measure, typically an inch. on a physical display.
For example, taking a physical width of 5 inches, with a resolution of 500x1000 yields a density of 200dpi (1000 pixels in 5 inches). A physical width of 4 inches with the same resolution yields a density of 250dpi (1000 pixels in 4 inches).
x=500 (half the width) is the centre of the screen on both and is independent of density. Indeed, half the width in pixels is the centre of the screen for any size screen at any density.
Welcome.
I need to prepare UI for different screen sizes. I make layouts
layout-small
layout-normal
layout-large
layout-XLARGE
For this I-small drawable, drawable-normal, drawable-large, drawable-XLARGE.
But where to get the screen size from small to XLARGE. I mean the size of
For example, 320 x 480 Where to get the dimensions for each screen? To prepare the graphics?
if you want to do in code you can use Display class of android and use method getRectSize and getSize for the same. Please See API here
If you want to see the differences between screen sizes you can look at this Supporting Multiple Screens
first of all you have to read this at least. As bit of advice, you can start with the graphics for the mdpi device. For the mdpi devices 1 pixel is equivalent to 1 dpi. Then you can make the same graphic for the hdpi just multiply the pixel of the mdpi image * 1.5, and for the ldpi just multiply the pixel of the mdpi image * 0.75..