solucion phone detuvo descargar cast android android-screen

phone - ¿Cómo obtengo el ScreenSize programáticamente en Android?



mirror android to android (11)

Android define tamaños de pantalla como Normal Large XLarge, etc.

Selecciona de manera automática entre recursos estáticos en las carpetas apropiadas. Necesito esta información sobre el dispositivo actual en mi código de Java. DisplayMetrics solo brinda información sobre la densidad actual del dispositivo. No hay nada disponible con respecto al tamaño de la pantalla.

Encontré la enumeración ScreenSize en el código grep here Sin embargo, esto no parece disponible para 4.0 SDK. ¿Hay alguna manera de obtener esta información?


Puedes probar esto, está funcionando Ejemplo

DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int ht = displaymetrics.heightPixels; int wt = displaymetrics.widthPixels; if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) { Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG) .show(); } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(this, "Screen size is neither large, normal or small", Toast.LENGTH_LONG).show(); } // Determine density DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int density = metrics.densityDpi; if (density == DisplayMetrics.DENSITY_HIGH) { Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density == DisplayMetrics.DENSITY_MEDIUM) { Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density == DisplayMetrics.DENSITY_LOW) { Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show(); } else { Toast.makeText( this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + String.valueOf(density), Toast.LENGTH_LONG) .show(); } // These are deprecated Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight();


¡Creo que es una pieza de código simple y directa!

public Map<String, Integer> deriveMetrics(Activity activity) { try { DisplayMetrics metrics = new DisplayMetrics(); if (activity != null) { activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); } Map<String, Integer> map = new HashMap<String, Integer>(); map.put("screenWidth", Integer.valueOf(metrics.widthPixels)); map.put("screenHeight", Integer.valueOf(metrics.heightPixels)); map.put("screenDensity", Integer.valueOf(metrics.densityDpi)); return map; } catch (Exception err) { ; // just use zero values return null; } }

Este método ahora se puede usar en cualquier lugar de forma independiente. Donde quiera que desee obtener información sobre la pantalla del dispositivo, hágalo de la siguiente manera:

Map<String, Integer> map = deriveMetrics2(this); map.get("screenWidth"); map.get("screenHeight"); map.get("screenDensity");

Espero que esto pueda ser útil para alguien y que le resulte más fácil de usar. Si necesito volver a corregir o mejorar, no dude en hacérmelo saber. :-)

¡¡¡Aclamaciones!!!



Lo necesito para algunas de mis aplicaciones y el siguiente código fue mi solución al problema. Solo mostrando el código dentro de Crear. Esta es una aplicación independiente que se ejecuta en cualquier dispositivo para devolver la información de la pantalla.

setContentView(R.layout.activity_main); txSize = (TextView) findViewById(R.id.tvSize); density = (TextView) findViewById(R.id.density); densityDpi = (TextView) findViewById(R.id.densityDpi); widthPixels = (TextView) findViewById(R.id.widthPixels); xdpi = (TextView) findViewById(R.id.xdpi); ydpi = (TextView) findViewById(R.id.ydpi); Configuration config = getResources().getConfiguration(); if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show(); txSize.setText("Large screen"); } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) { Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG) .show(); txSize.setText("Normal sized screen"); } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG) .show(); txSize.setText("Small sized screen"); } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) { Toast.makeText(this, "xLarge sized screen", Toast.LENGTH_LONG) .show(); txSize.setText("Small sized screen"); } else { Toast.makeText(this, "Screen size is neither large, normal or small", Toast.LENGTH_LONG).show(); txSize.setText("Screen size is neither large, normal or small"); } Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); Log.i(TAG, "density :" + metrics.density); density.setText("density :" + metrics.density); Log.i(TAG, "D density :" + metrics.densityDpi); densityDpi.setText("densityDpi :" + metrics.densityDpi); Log.i(TAG, "width pix :" + metrics.widthPixels); widthPixels.setText("widthPixels :" + metrics.widthPixels); Log.i(TAG, "xdpi :" + metrics.xdpi); xdpi.setText("xdpi :" + metrics.xdpi); Log.i(TAG, "ydpi :" + metrics.ydpi); ydpi.setText("ydpi :" + metrics.ydpi);

Y un simple archivo XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/tvSize" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/density" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/densityDpi" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/widthPixels" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/xdpi" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/ydpi" android:layout_width="wrap_content" android:layout_height="wrap_content" />


Puede obtener el tamaño de visualización en píxeles utilizando este código.

Display display = getWindowManager().getDefaultDisplay(); SizeUtils.SCREEN_WIDTH = display.getWidth(); SizeUtils.SCREEN_HEIGHT = display.getHeight();


Si se encuentra en una actividad no getResources() , es decir, Fragmento, Adaptador, Clase de modelo o cualquier otra clase de Java que no amplíe la Activity simplemente getResources() no funcionará. Puede usar getActivity() en fragmento o usar context que pase a la clase correspondiente.

mContext.getResources()

Recomendaría hacer una clase que diga Utils que tendrá métodos / métodos para el trabajo común. El beneficio de esto es que puede obtener el resultado deseado con una sola línea de código en cualquier parte de la aplicación que invoque este método.


simon-

Diferentes tamaños de pantalla tienen diferentes densidades de píxeles. Una pantalla de 4 pulgadas en su teléfono podría tener más o menos píxeles y luego decir un televisor de 26 pulgadas. Si estoy entendiendo correctamente, él quiere detectar cuál de los grupos de tamaño es la pantalla actual, pequeña, normal, grande y extra grande. Lo único que se me ocurre es detectar la densidad de píxeles y usar eso para determinar el tamaño real de la pantalla.


Determinar el tamaño de la pantalla:

int screenSize = getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK; switch(screenSize) { case Configuration.SCREENLAYOUT_SIZE_LARGE: Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show(); break; case Configuration.SCREENLAYOUT_SIZE_NORMAL: Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show(); break; case Configuration.SCREENLAYOUT_SIZE_SMALL: Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show(); break; default: Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show(); }

Determine la densidad:

int density= getResources().getDisplayMetrics().densityDpi; switch(density) { case DisplayMetrics.DENSITY_LOW: Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show(); break; case DisplayMetrics.DENSITY_MEDIUM: Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show(); break; case DisplayMetrics.DENSITY_HIGH: Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show(); break; case DisplayMetrics.DENSITY_XHIGH: Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show(); break; }

para Ref: http://devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html


Copie y pegue este código en su Activity y cuando se ejecute Toast la categoría de tamaño de pantalla del dispositivo.

int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; String toastMsg; switch(screenSize) { case Configuration.SCREENLAYOUT_SIZE_LARGE: toastMsg = "Large screen"; break; case Configuration.SCREENLAYOUT_SIZE_NORMAL: toastMsg = "Normal screen"; break; case Configuration.SCREENLAYOUT_SIZE_SMALL: toastMsg = "Small screen"; break; default: toastMsg = "Screen size is neither large, normal or small"; } Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();


DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int width = displayMetrics.widthPixels; int height = displayMetrics.heightPixels;


private static String getScreenResolution(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); int width = metrics.widthPixels; int height = metrics.heightPixels; return "{" + width + "," + height + "}"; }