Android分屏显示(多窗口支持) 开发总结

最近公司有分屏显示需求,遇到几点问题 ,在此记录,以做备忘。

我所谓的分屏显示,是在同一个界面内,分屏显示两个app的界面

Android分屏显示(多窗口支持) 开发总结_第1张图片

或者是

Android分屏显示(多窗口支持) 开发总结_第2张图片

注意点1:

系统好像没有获取应用显示的左右或者上下位置的方法,所以我采用迂回战略,使用

getWindow().getDecorView().findViewById(android.R.id.content).post(new Runnable() {
    @Override
    public void run() {
        int[] outLocation = new int[2];
        getWindow().getDecorView().findViewById(android.R.id.content).getLocationOnScreen(outLocation);
        int x = outLocation[0];
        int y = outLocation[1];
    }
});

其中,x代表距离屏幕左边距,y代表距离屏幕上边距,如此即可知道应用处于什么位置,也可以做一些操作

注意点2:

获取分屏时app显示的宽高,这里需要说明一下,方法必须是使用activity的上下文,否则获取的值是不准确的

DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;

以上即可获取app显示的宽高,但是,分屏模式下状态栏是不受控制的,即使设置了全屏模式,状态栏还是会显示出来盖在app上面影响操作,因此,app在贴近状态栏的一侧时,要去除状态栏高度才是真正的app显示的高度

int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
    int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    heightPixels = heightPixels - statusBarHeight;
}

大概总结就这样,比较简单

你可能感兴趣的:(android,android,android分屏显示,多窗口模式)