Launcher中获取副屏分辨率、密度

作者: youseewhat | 来源:发表于2019-12-04 12:42 被阅读0次

    目的是要在launcher的副屏显示广告,所以第一是要控制副屏,第二是要适配.因为主屏幕和副屏幕尺寸、密度都不一样所以必须获取副屏信息

    获取到副屏

    我使用了控制媒体通道获取屏幕信息

    MediaRouter允许应用程序控制媒体通道和流从当前设备到外部扬声器和目标设备的路由

    要注意的是MediaRouter的API不是线程安全的;与它的所有交互都必须在流程的主线程中完成

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                MediaRouter mediaRouter = MediaRouter.getInstance(this);
                List<MediaRouter.RouteInfo> routes = mediaRouter.getRoutes();
                Display display = null;
                for (MediaRouter.RouteInfo routeInfo : routes) {
               //我的设备只有一个副屏
                    if (routeInfo.getPresentationDisplay() != null) {
                      //display就是我要拿到的副屏信息
                        display = routeInfo.getPresentationDisplay();
                        break;
                    }
                }
    
                if (display != null) {
                    presentation = new MainPresentation(this, display);
                  //控制显示副屏
                    presentation.show();
                }
            }
    
    断点看看获取的信息

    Presentation显示

    Presentation是一种特殊的dialog(继承自Dialog),用于副屏显示控制

    使用例子:

    DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
     Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
      if (presentationDisplays.length > 0) {
          // If there is more than one suitable presentation display, then we could consider
        // giving the user a choice.  For this example, we simply choose the first display
         // which is the one the system recommends as the preferred presentation display.
        Display display = presentationDisplays[0];
        Presentation presentation = new MyPresentation(context, presentationDisplay);
       presentation.show();
    

    代码获取屏幕分辨率,密度
    屏幕密度:为每英寸像素点数

    
    /**
     * 屏幕管理类
     */
    @SuppressLint({"NewApi"})
    public class MainPresentation extends Presentation {
        private static final String TAG = "MainPresentation";
        private Context mContext;
        private Display mParamDisplay;
    
        public MainPresentation(Context paramContext, Display paramDisplay) {
            super(paramContext, paramDisplay);
            mContext = paramContext;
            mParamDisplay = paramDisplay;
    
        }
    
        @Override
        protected void onCreate(Bundle paramBundle) {
            super.onCreate(paramBundle);
            setContentView(R.layout.main_presentation);
            //获取Height  Width
            if (mParamDisplay != null) {
                int physicalHeight = mParamDisplay.getMode().getPhysicalHeight();
                int physicalWidth = mParamDisplay.getMode().getPhysicalWidth();
            }
    
            //获取屏幕密度
            DisplayMetrics displayMetrics = new DisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mParamDisplay.getRealMetrics(displayMetrics);
            } else {
                mParamDisplay.getMetrics(displayMetrics);
            }
            String density = String.valueOf(displayMetrics.densityDpi);
            Log.e(TAG, "onCreate density: " + density);
        }
    
    
    }
    
    

    如何知道获取的是否正确,可以dump屏幕信息进行对比

    adb shell dumpsys display

    Display 1为副屏

    相关文章

      网友评论

        本文标题:Launcher中获取副屏分辨率、密度

        本文链接:https://www.haomeiwen.com/subject/anbkgctx.html