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



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

THs表示屏幕分辨率1024*600px。但是我想在运行时计算屏幕大小。意味着需要在运行时定义coordX和coordY。你能帮我修改代码吗。

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

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

有人能提出解决方案吗?

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

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();

它也可以在这个类中创建。

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

相关内容

  • 没有找到相关文章

最新更新