17

アプリケーションで画面の幅を取得する必要があります。アプリケーションは2.1以降で実行されます。以下のように設定しました。このメソッドは非推奨であり、おそらくgetSizeまたは他の方法を使用する必要があります。しかし、問題は次のとおりです。これは3.0以降や4.0以降のAndroidバージョンで機能するのでしょうか、それともアプリがクラッシュするのでしょうか。以前、スレッドで非推奨のメソッドを使用しましたが、アイスクリームデバイスでアプリがクラッシュしました。以下の方法は機能しますか?

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

編集:

getSizeを試しましたが、機能しません。

  Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;
4

5 に答える 5

25

よくわかりませんが、これでうまくいくかもしれません:

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
} else {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
}
于 2012-05-18T22:11:51.143 に答える
12

これらの廃止されたメソッドが Android 3 および 4 で機能するかどうかはわかりません。確認する最善の方法は、エミュレーターでテストすることです。

ただし、互換性を最大にするための最も安全な方法は、リフレクションを使用して 1 つの方法を試し、別の方法にフォールバックすることです。基本的に、getSize()失敗しない独自のバージョンを作成できます。この atm をテストすることはできませんが、次のようになります。

void overrideGetSize(Display display, Point outSize) {
    try {
      // test for new method to trigger exception
      Class pointClass = Class.forName("android.graphics.Point");
      Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass });

      // no exception, so new method is available, just use it
      newGetSize.invoke(display, outSize);
    } catch(NoSuchMethodException ex) {
      // new method is not available, use the old ones
      outSize.x = display.getWidth();
      outSize.y = display.getHeight();
    }
}

それからもちろん、次のようなもので呼び出すだけです

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
overrideGetSize(display, size);
于 2012-05-18T22:04:50.370 に答える
3

Eclipse が警告やエラーを出さないように、Steve の役立つコードを拡張し、少し再構築しました。Point クラスは API レベル 1 から存在していたので、リフレクションによって作成するメリットはあまりありませんでした。

  final static String mTAG = "MYTAG";

  // Cope with deprecated getWidth() and getHeight() methods
  Point getSize(Display xiDisplay) 
  {
    Point outSize = new Point();
    boolean sizeFound = false;

    try 
    {
      // Test if the new getSize() method is available
      Method newGetSize = 
        Display.class.getMethod("getSize", new Class[] { Point.class });

      // No exception, so the new method is available
      Log.d(mTAG, "Use getSize to find screen size");
      newGetSize.invoke(xiDisplay, outSize);
      sizeFound = true;
      Log.d(mTAG, "Screen size is " + outSize.x + " x " + outSize.y);
    } 
    catch (NoSuchMethodException ex) 
    {
      // This is the failure I expect when the deprecated APIs are not available
      Log.d(mTAG, "getSize not available - NoSuchMethodException");
    }  
    catch (InvocationTargetException e) 
    {
      Log.w(mTAG, "getSize not available - InvocationTargetException");
    } 
    catch (IllegalArgumentException e) 
    {
      Log.w(mTAG, "getSize not available - IllegalArgumentException");
    } 
    catch (IllegalAccessException e) 
    {
      Log.w(mTAG, "getSize not available - IllegalAccessException");
    }

    if (!sizeFound)
    {
      Log.i(mTAG, "Used deprecated methods as getSize not available");
      outSize = new Point(xiDisplay.getWidth(), xiDisplay.getHeight());
    }

    return outSize;
  }
于 2012-08-22T22:03:16.657 に答える
2

https://stackoverflow.com/a/1016941/2914140によると:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
于 2016-05-23T13:07:32.047 に答える
0

Displayの新しい関数getSize()の何が問題になっていますか?Pointオブジェクトを必要な幅と高さの値に変換するのは本当に簡単です。

于 2012-05-18T21:30:42.910 に答える