Screen Size Used

I am using the Nexus 7 API 1280x800 android 4.2.2 API

I want to get the size of the screen to divide it into square sections of the same height and width.

I use FrameLayout, and my squares are a subclass of ImageView.

I'm doing it

context.getResources().getDisplayMetrics().heightPixels; ----> 1205 context.getResources().getDisplayMetrics().widthPixels; ------> 800

I believe that 1205 is not 1280, because there are two Android menus at the top and bottom of the screen.

Each of my squares is 30x30 px.

To find out how many squares I can do as much as possible, I do:

int x=1205/30;

When I try to draw my image on the coordinates (x-1)*30 ,y, it is partially deleted from the screen.

How do I know the part of the screen that my application can use?

Hope I explained my problem well.

Many thanks.

+5
source share
4 answers

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;
    }
}

onSizeChange

@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;
}

, :

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 ...
}

, , , , - .. .. ,

MyImageView miv;

miv = topView.findViewById("idforMyImageViewSetInLayoutXMLfile");

miv.drawSubBox(0,0, ...);
+3

, , . heightPixels widthPixels . -, - px , , , dp , dpi, . , .

, , , . x = 1205/30 = 40.166667. (x-1)*30 = 1,175. , x 1205 (@JRowan - ), 30 , , . , . , , 4.8px 1205 . 40 rectanlges , .

+1

, .

- , , .

, .

getWindow().requestFeature(Window.FEATURE_NO_TITLE);

, . , .

How to increase the title bar size of an Android application?

Android status bar height

Thanks for answers.

+1
source

If the grid is in a fragment, then the problem is that the size of the screen used increases before the fragment is oversized. In a similar scenario, I got the width and height of the fragments container in the hosting activity and passed them as parameters to the fragment with the grid

-2
source

All Articles