6

私は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;

誰でも解決策を提案できますか?

4

2 に答える 2

10

表示寸法をピクセル単位で表示したい場合は、

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();
于 2012-01-15T14:00:34.297 に答える
0

このクラスで作成することもできます。

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

}
于 2016-01-06T19:20:32.057 に答える