English 中文(简体)
如何计算高温和宽度。
原标题:How can I Calculate screen height and width at run time?

我的舱位和星体为后一类,当时我正在确定一些功能,以计算布贝莱斯在屏幕上的动议。

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号决议。 BUt I想要计算在运行时间的屏幕。 A. 界定 co的手段 页: 1 Y在业时间。 你能够帮助我修改法典。

我试图在我的行车(Ball())中使用以下守则,但该守则在那里工作。

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

}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签