I am using a Nexus 7 1280x800 android 4.2.2 API 17
I want to get the size of the screen to divide it in square sections of the same height and width.
I am using FrameLayout and my squares are subclass of ImageView.
I do this
context.getResources().getDisplayMetrics().heightPixels; ----> 1205
context.getResources().getDisplayMetrics().widthPixels; ------> 800
I suppose 1205 is not 1280 because there are two Android menus on top and bottom of the screen.
Each of my squares is 30x30 px.
To know how many squares I can draw as maximun I do:
int x=1205/30;
When I try to paint my image on coords (x-1)*30 ,y
it is drawn partially out of the screen.
How can I know the portion of screen my application can use?
I hope I explained well my issue.
Many thanks.
If all the squares are in the same ImageView, then I would guess that the easiest way is to create your own imageView:
class MyImageView extends ImageView {
Context context;
int myWidth = 0;
int myHeigh = 0;
int numBoxesX = 0;
int numBoxesY = 0;
private final int boxWidth = 30;
private final int boxHeight = 30;
ImageView(Context c) {
super(c);
context = c;
}
}
In the class, you override the onSizeChange function
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
myWidth = w;
myHeight = h;
// Set up other things that you work out from the width and height here, such as
numBoxesX = myWidth / boxWidth;
numBoxesY = myHeight / boxHeight;
}
On then create a function to draw the box:
public void drawSubBox(int x, int y, ...) {
// Fail silently if the box being drawn doesn't exist ...
if ((x<0) || (x>=numBoxesX)) return;
if ((y<0) || (y>=numBoxesY)) return;
// Your code to draw the box on the screen ...
}
Once you have created this View, you can add it to a layout, and access the functions for it just like any other view, including any you add to define the size of the subboxes etc etc. So in the class for the Activity with this layout
MyImageView miv;
miv = topView.findViewById("idforMyImageViewSetInLayoutXMLfile");
miv.drawSubBox(0,0, ...);
As to finding out the pixel dimensions of the screen I think you are correct. heightPixels
and widthPixels
will give you the absolute pixel dimensions of the screen you are using. Just something to be conscious about - I'd be worried about using px as the base measurement since the results will change depending on the density of the screen. It seems like a much safer idea to use dp
and after finding the absolute number of pixels in the screen, scalling that number by the dpi
so that the rectangles draw the same on every phone. Also the absolute pixel value does not represent the actual free space just the total number of pixles.
Oherwise I don't know that the math works out for this layout to fit on the screen perfectly. x = 1205/30 = 40.166667
. Plugging that number in (x-1)*30 = 1,175
. Assuming that the x direction on your phone does in fact include 1205px (@JRowan - it could be landscape) adding 30px would seem to make these rectangles fit on the screen. However, this only works if there is no boarder or offset between any of the rectangles. Using the numbers you provided there is only 4.8px of leeway across the 1205px width of the screen. With 40 rectanlges in that space, even a thin boarder would push the drawing off the screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With