2

私はこれを使用しています:

int wi=getWindowManager().getDefaultDisplay().getWidth();

画面サイズをピクセル単位で取得します。画面サイズを取得する方法はありますdpiか?

これは、画面サイズに基づいてさまざまなレイアウトを選択するために行っています。

4

4 に答える 4

3

escreenのサイズをdpiで取得する方法はありますか?

DisplayMetricsピクセル単位の画面サイズと画面密度を教えてくれます。そこから、で画面サイズを計算できますdp

これは、画面サイズに基づいてさまざまなレイアウトを選択するために行っています。

レイアウトを適切なディレクトリ(たとえば、)に配置すると、これはリソースフレームワークによって自動的に処理されres/layout-large/ますres/layout-sw600dp/

于 2013-03-04T23:44:33.287 に答える
1

メトリックオブジェクトを作成し、以下を実行する必要があります

 public class LocalUtil extends Application {
private static Context context1;
private static DisplayMetrics  metrics;
public static void setContext(Context context)
{

        context1=context;
        metrics=context1.getResources().getDisplayMetrics();
}
public static float getDensity()
{
    return metrics.density;
}
public static int getScreenWidth()
{
    return metrics.widthPixels;
}
public static int getScreenHeight()
{
    return metrics.heightPixels;
}
public static float getScreenWidthInDpi()
{
    return metrics.widthPixels/metrics.density;
}
public static float getScreenHeightInDpi()
{
    return metrics.heightPixels/metrics.density;
}

このメソッドを使用するたびに、setcontextメソッドを使用してコンテキストを設定し、このコードのように目的に応じて最適なメソッドを呼び出すことができます。このコードは、メインアクティビティのoncreateViewです。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    LocalUtil.setContext(getApplicationContext());
    LocalUtil.getScreenWidthInDpi();
    //or call  each method you want
  }
于 2015-07-20T18:32:32.437 に答える
0

「dpi単位の画面サイズ」は無意味な表現です。ディスプレイのDPIを取得したいだけですか?

第二に、これをしないでください。

真剣に、やめて。しないでください。

意図したとおりにレイアウトフォルダを使用します。HDPIに別のレイアウトが必要な場合は、カスタムレイアウトをに配置しlayout-hdpiます。

とはいえ、密度が必要な場合は次のようになります。

DisplayMetrics metrics = new DisplayMetrics();
getWindow().getDisplayMetrics(metrics);
int dpi = metrics.densityDpi;
于 2013-03-04T23:44:54.410 に答える
0

画面の幅と高さをピクセル単位で計算する方法は次のとおりです。

int widthPixels = getWindowManager().getDefaultDisplay().getWidth();
int heightPixels = getWindowManager().getDefaultDisplay().getHeight();

float scale = getApplicationContext().getResources().getDisplayMetrics().density;

int width = (int) (widthPixels - 0.5f)/scale; //width of screen in dpi
int height = (int) (heightPixels - 0.5f)/scale; //height of screen in dpi
于 2013-03-05T00:15:43.653 に答える