如何在运行时计算屏幕高度和宽度?

发布于 2024-12-27 05:52:13 字数 2210 浏览 6 评论 0原文

我在 android 中有以下课程,其中定义了一些函数来计算屏幕上气泡的运动。

public class floatBubble  {
     private Bitmap img; // the image of the ball
     private int coordX = 512; // the x coordinate at the canvas
     private int coordY = 600; // the y coordinate at the canvas 
     private int id; // gives every ball his own id, for now not necessary
     private static int count = 1;
     private boolean goRight = true;
     private boolean goDown = true;
   public floatBubble(Context context, int drawable) {
     BitmapFactory.Options opts = new BitmapFactory.Options();
       opts.inJustDecodeBounds = true;
       img = BitmapFactory.decodeResource(context.getResources(), drawable); 
       id=count;
     count++;
    }

    public static int getCount() {
        return count;
    }
    void setX(int newValue) {
        coordX = newValue;
        }
    public int getX() {
        return coordX;
    }
    void setY(int newValue) {
        coordY = newValue;
        }
    public int getY() {
        return coordY;
    }

    public int getID() {
        return id;
    }
    public Bitmap getBitmap() {
        return img;
    }

    public void moveBall(int goX, int goY) {
        // check the borders, and set the direction if a border has reached

        if (coordX > 1024){
            goRight = false;
        }
        if (coordX < -100){
            goRight = false;
            coordX = 512;
            coordY = 600;
        }
        if (coordY > 600){
            goDown = false;
        }
        if (coordY < -100){
            coordY = 600;
            goDown = false;
        }
        // move the x and y 
        if (goRight){
            coordX += goX;
        }else
        {
            coordX -= goX;
        }
        if (goDown){
            coordY += goY;
        }else
        {
            coordY -= goY;
        }
    }
}

这适用于屏幕分辨率 1024*600px。但我想在运行时计算屏幕尺寸。意味着需要在运行时定义coordX和coordY。你能帮我修改一下代码吗?

我尝试在 moveBall() 中使用以下代码,但该代码在那里不起作用。

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);      
coordX = dm.widthPixels;
coordY = dm.heightPixels;

有人可以提出解决方案吗?

I have following class in android, where I am defining some functions to calculate motion of Bubbles on the screen.

public class floatBubble  {
     private Bitmap img; // the image of the ball
     private int coordX = 512; // the x coordinate at the canvas
     private int coordY = 600; // the y coordinate at the canvas 
     private int id; // gives every ball his own id, for now not necessary
     private static int count = 1;
     private boolean goRight = true;
     private boolean goDown = true;
   public floatBubble(Context context, int drawable) {
     BitmapFactory.Options opts = new BitmapFactory.Options();
       opts.inJustDecodeBounds = true;
       img = BitmapFactory.decodeResource(context.getResources(), drawable); 
       id=count;
     count++;
    }

    public static int getCount() {
        return count;
    }
    void setX(int newValue) {
        coordX = newValue;
        }
    public int getX() {
        return coordX;
    }
    void setY(int newValue) {
        coordY = newValue;
        }
    public int getY() {
        return coordY;
    }

    public int getID() {
        return id;
    }
    public Bitmap getBitmap() {
        return img;
    }

    public void moveBall(int goX, int goY) {
        // check the borders, and set the direction if a border has reached

        if (coordX > 1024){
            goRight = false;
        }
        if (coordX < -100){
            goRight = false;
            coordX = 512;
            coordY = 600;
        }
        if (coordY > 600){
            goDown = false;
        }
        if (coordY < -100){
            coordY = 600;
            goDown = false;
        }
        // move the x and y 
        if (goRight){
            coordX += goX;
        }else
        {
            coordX -= goX;
        }
        if (goDown){
            coordY += goY;
        }else
        {
            coordY -= goY;
        }
    }
}

THis for the screen resolution 1024*600px. BUt I want to calculate the screensize at runtime. means need to define coordX and coordY at runtime. Can you help me in modifying the code.

I tried to use the following code in my moveBall(), but the code didn't worked there.

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);      
coordX = dm.widthPixels;
coordY = dm.heightPixels;

Anybody can suggest a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摇划花蜜的午后 2025-01-03 05:52:13

如果您想要以像素为单位的显示尺寸,则可以使用

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

如果您无权访问要调用 getWindowManager 的活动。您可以使用:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

If you want the the display dimensions in pixels you can use

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

If you don't have access to an activity to call getWindowManager on. You can use:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
余生共白头 2025-01-03 05:52:13

也可以在这个类中创建。

package com.example.android.fragments;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;

public class ScreenUtility {

    private Activity activity;
    private float dpWidth;
    private float dpHeight;

    public ScreenUtility(Activity activity) {
        this.activity = activity;

        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        float density = activity.getResources().getDisplayMetrics().density;
        dpHeight = outMetrics.heightPixels / density;
        dpWidth = outMetrics.widthPixels / density;
    }

    public float getWidth() {
        return dpWidth;
    }

    public float getHeight() {
        return dpHeight;
    }

}

It can also be created in this class.

package com.example.android.fragments;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;

public class ScreenUtility {

    private Activity activity;
    private float dpWidth;
    private float dpHeight;

    public ScreenUtility(Activity activity) {
        this.activity = activity;

        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        float density = activity.getResources().getDisplayMetrics().density;
        dpHeight = outMetrics.heightPixels / density;
        dpWidth = outMetrics.widthPixels / density;
    }

    public float getWidth() {
        return dpWidth;
    }

    public float getHeight() {
        return dpHeight;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文