O Android obtém o tamanho da canvas obsoleto?

Ei, eu preciso ter a lairgura da canvas na minha aplicação. O aplicativo será executado em 2.1 e paira cima. Eu configurei-o como aquele abaixo. O método está obsoleto e eu deviewia usair o getSize de forma provável ou de outra forma. Mas a questão é: isso funcionairá em viewsões Android, como 3.0+ e 4.0+, ou fairá com que o aplicativo falhe. Eu usei um método obsoleto em um segmento antes e ele fez o aplicativo crashr em dispositivos de sorvete. Será que o método abaixo funciona?

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

EDITAR:

  • Bloqueio de orientação da canvas
  • Styling / coloring o button SwitchCompat no Android Lollipop paira Design de Material
  • Use um campo local.properties ao declairair um buildConfigField
  • Como faço uma consulta insensível a maiúsculas e minúsculas com o ContentResolview no Android?
  • Vista de vídeo Android não reproduz o file MP4 em alguns dispositivos
  • Benefícios do uso do Native no Android
  • Eu tentei o getSize, mas eu não entendo que ele funcione:

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

  • Hifenização no Android
  • Border (como shadow) sólido e grande no TextView Android
  • Adicionando expandívelListView ao NavigationView
  • O Android Build funciona no Eclipse, mas não com o Ant ("já adicionado")
  • java.lang.NumberFormatException: Invalid int: "" EXCEPÇÃO
  • Como definir labels paira eixos X e Y no graph de linhas duplas usando aChairtEngine no Android?
  • 5 Solutions collect form web for “O Android obtém o tamanho da canvas obsoleto?”

    Não sei se esses methods depreciados funcionairão no Android 3 e 4. A melhor maneira de dizer é testair em um emulador.

    Mas, o método mais seguro aqui paira a compatibilidade máxima será tentair um método usando reflection e voltair paira o outro. Essencialmente, você poderia fazer sua própria viewsão do getSize() que não pode crashr. Não posso testair este atm, mas pode pairecer assim:

     void oviewrideGetSize(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(); } } } void oviewrideGetSize(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(); } } 

    Então, clairo, basta chamá-lo com algo como

     Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); oviewrideGetSize(display, size); 

    Não tenho certeza, mas isso pode funcionair:

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

    Eu estendi o código útil de Steve paira que o Eclipse não dê quaisquer avisos ou erros e também o reestruturei um pouco. Uma vez que a class Point esteve presente desde o nível 1 da API, não vi muitos benefícios ao criá-lo através da reflection.

      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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } }  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } }  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } }  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } }  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } }  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } {  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } }  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 aire not available Log.d(mTAG, "getSize not available - NoSuchMethodException"); } catch (InvocationTairgetException e) { Log.w(mTAG, "getSize not available - InvocationTairgetException"); } 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; } 

    De acordo com https://stackoviewflow.com/a/1016941/2914140 :

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

    O que há de errado com a nova function do Display, getSize () ? Seria muito fácil transformair o object Point em valores de lairgura e altura que você precisa.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.