tomasslavicek

Xamarin.Android MonoDroid tablet detection code

May 11th, 2013
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. private void RecalculateScreen()
  2. {
  3.     Display d = Activity.WindowManager.DefaultDisplay;
  4.     graphics.PreferredBackBufferWidth = d.Width;
  5.     graphics.PreferredBackBufferHeight = d.Height;
  6.     graphics.ApplyChanges();
  7.            
  8.     DisplayMetrics metrics = new DisplayMetrics();
  9.     d.GetMetrics(metrics);
  10.            
  11.     // Enum: Low = 120, Medium/Default = 160, High = 240, XHigh = 320
  12.     // - přepočet na 100%: Low = 50, Medium/Default = 66.6, High = 100, XHigh = 133.3, XXH: 200
  13.     // - Samsung Galaxy Ace 2: = High (240), ale densityX = 160
  14.     // - převedeme to na 1f = default
  15.     //var densityDPI = metrics.DensityDpi;
  16.     var densityX = metrics.Xdpi;
  17.  
  18.     //TODO jak to bude vypadat na XXHigh = 480?
  19.     Engine.Params.ScreenSize = new Microsoft.Xna.Framework.Point(d.Width, d.Height);
  20.     Engine.Params.DpiScale = (float)densityX / (float)DisplayMetricsDensity.Default;
  21.     Engine.Params.VirtualSize = new Point((int)(d.Width / (float)Engine.Params.DpiScale), (int)(d.Height / (float)Engine.Params.DpiScale));
  22.     Engine.Params.IsTabletDevice = AndroidOperations.IsTabletDevice(Activity, Application.Context);
  23.     Engine.Params.HQGraphics = false; // Zatím máme pro všechny HQGraphics = false (abychom si to nekomplikovali)
  24.  
  25.     // Přepočítá danou obrazovku
  26.     Engine.RecalculateScreenResolution();
  27. }
  28.  
  29. public static class AndroidOperations
  30. {
  31.     public static bool IsTabletDevice(AndroidGameActivity activity, Context activityContext)
  32.     {
  33.         bool xlarge = (activityContext.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) >= ScreenLayout.SizeLarge;
  34.            
  35.         // If XLarge, checks if the Generalized Density is at least MDPI
  36.         // (160dpi)
  37.         if (xlarge)
  38.         {
  39.             Display d = activity.WindowManager.DefaultDisplay;
  40.             DisplayMetrics metrics = new DisplayMetrics();
  41.             d.GetMetrics(metrics);
  42.  
  43.             // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
  44.             // DENSITY_TV=213, DENSITY_XHIGH=320
  45.             if (metrics.DensityDpi != DisplayMetricsDensity.Low)
  46.                 return true;
  47.         }
  48.         return false;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment