可用的屏幕尺寸 [英] size of the usable screen

查看:144
本文介绍了可用的屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Nexus 7 1280×800的Andr​​oid 4.2.2 API 17

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.

我使用的FrameLayout和我的方块是ImageView的子类。

I am using FrameLayout and my squares are subclass of ImageView.

我这样做

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

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

我觉得1205是不是1280,因为有在屏幕的上方和下方的两个Android的菜单。

I suppose 1205 is not 1280 because there are two Android menus on top and bottom of the screen.

我的每个平方是30×30像素。

Each of my squares is 30x30 px.

要知道有多少平方我能画的最高可我做的:

To know how many squares I can draw as maximun I do:

 INT X = 1205至1230年;

int x=1205/30;

当我尝试画我的形象在COORDS (X-1)* 30,Y 这部分抽出的画面。

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.

非常感谢。

推荐答案

如果所有的方块都在同​​一个ImageView的,那么我猜想,最简单的方法是创建你自己的ImageView的:

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

在类中,覆盖onSizeChange功能

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

一旦你创建了这个观点,你可以将它添加到布局,并访问功能,它就像任何其他的观点,包括任何你添加定义subboxes等等等等的大小,所以在类的与此布局的活动

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, ...);

这篇关于可用的屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆