如何找到屏幕的android系统的高度和宽度 [英] How to find the height and width of screen in android

查看:232
本文介绍了如何找到屏幕的android系统的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类叫做箱子在这里我想找到的高度和宽度。要做到这一点我使用的:

I have a class called crate where i would like to find the height and width. to do this I am using:

public class Crate {
    public int acrossCrate;
    public int upDownCrate;

    public Crate(Context context) {

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
}
}

但我有另一个类叫做平局它调用箱类,当我在箱子使用上下文的背景下它抽奖类调用时,它给出了一个错误:

But I have another class called draw which calls the crate class, and when i use the Context context in crate it gives an error when calling it in the draw class:

public class Draw extends View {
    float x, y;
    Paint red = new Paint();
    Paint green = new Paint();
    Paint black = new Paint();
    Paint blue = new Paint();
    Bitmap player;
    Bitmap crate;
    int rectSide = 1000;

    Player thePlayer = new Player();
    Crate theCrate = new Crate();//<-- ERROR when i use Context context

林真的卡住,如果有人知道如何做到这一点,可以帮助我,将是巨大的。

Im really stuck and if anyone knows how to do this and could help me that would be great.

推荐答案

在尝试以下方法

// Get screen width
public static int getScreenWidth(Context c) {
    DisplayMetrics dmetrics = new DisplayMetrics();
    ((Activity) c).getWindowManager().getDefaultDisplay()
            .getMetrics(dmetrics);
    return dmetrics.widthPixels;
}

// Get screen height
public static int getScreenHeight(Context c) {
    DisplayMetrics dmetrics = new DisplayMetrics();
    ((Activity) c).getWindowManager().getDefaultDisplay()
            .getMetrics(dmetrics);
    return dmetrics.heightPixels;
}

希望这将帮助你。

Hope this will helps you.

这篇关于如何找到屏幕的android系统的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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