Android 多窗口

随着手机屏幕越来越大,单手操作手机越来越难,所以一些大厂早就开始研究多窗口,如iphone、samsung的单手模式,作为一个发展趋势google肯定也不会不考虑用户的体验,所以在android N中增加了多窗口的操作。

本编通过android官方给出的示例结合api指南及前人分析来研究一下android 的多窗口操作。
示例Github
API指南
博客

用户可以通过以下方式切换到多窗口模式:

  • 若用户打开 Overview 屏幕并长按 Activity 标题,则可以拖动该 Activity 至屏幕突出显示的区域,使Activity 进入多窗口模式。
  • 若用户长按 Overview 按钮,设备上的当前 Activity 将进入多窗口模式,同时将打开 Overview屏幕,用户可在该屏幕中选择要共享屏幕的另一个 Activity。

这里写图片描述

这里写图片描述

上面是github上示例的UI
按照它的显示我们一个一个分析
1、首先 start basic Default Activity 打开的是默认的Activity

public void onStartBasicActivity(View view) {
        Log.d(mLogTag, "** starting BasicActivity");

        // Start an Activity with the default options in the 'singleTask' launch mode as defined in
        // the AndroidManifest.xml.
        startActivity(new Intent(this, BasicActivity.class));

    }

这个和正常的activity一样。
2、Start unresizeable Activity

 public void onStartUnresizableCliconStartBasicActivityk(View view) {
        Log.d(mLogTag, "** starting UnresizableActivity");

        /*
         * This activity is marked as 'unresizable' in the AndroidManifest. We need to specify the
         * FLAG_ACTIVITY_NEW_TASK flag here to launch it into a new task stack, otherwise the
         * properties from the root activity would have been inherited (which was here marked as
         * resizable by default).
        */
        Intent intent = new Intent(this, UnresizableActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
在xml中
<activity
            android:name="com.android.multiwindowplayground.activities.UnresizableActivity"
            android:resizeableActivity="false"
            android:taskAffinity="" />

配置了android:resizeableActivity=”false”
运行后发现如果在分屏界面,会提示“应用不支持分屏”然后全屏显示。

所以这个配置是启用和禁用多窗口显示的,N里面默认为true。

3、start activity adjacent

 public void onStartAdjacentActivity(View view) {
        Log.d(mLogTag, "** starting AdjacentActivity");

        /*
         * Start this activity adjacent to the focused activity (ie. this activity) if possible.
         * Note that this flag is just a hint to the system and may be ignored. For example,
         * if the activity is launched within the same task, it will be launched on top of the
         * previous activity that started the Intent. That's why the Intent.FLAG_ACTIVITY_NEW_TASK
         * flag is specified here in the intent - this will start the activity in a new task.
         */
        Intent intent = new Intent(this, AdjacentActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    配置文件
     <activity
            android:name="com.android.multiwindowplayground.activities.AdjacentActivity"
            android:taskAffinity="" />

看效果
这里写图片描述

在启动新 Activity 时,用户可以提示系统如果可能,应将新 Activity 显示在当前 Activity 旁边。 要执行此操作,可使用标志 Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT。 传递此标志将请求以下行为

  • 如果设备处于分屏模式,系统会尝试在启动系统的 Activity 旁创建新 Activity,这样两个 Activity 将共享屏幕。
    系统并不一定能实现此操作,但如果可以,系统将使两个 Activity 处于相邻的位置。
  • 如果设备不处于分屏模式,则该标志无效。

    并且发现onMultiWindowModeChanged函数执行了
    这个函数的作用是
    Activity 进入或退出多窗口模式时系统将调用此方法。 在 Activity 进入多窗口模式时,系统向该方法传递 true 值,在退出多窗口模式时,则传递 false 值。

    4、start activity that handles configurationchanges

这个主要处理一些配置文件的改变

下面还有两个事件
5、start activity with Minimum size

 <activity
            android:name="com.android.multiwindowplayground.activities.MinimumSizeActivity"
            android:launchMode="singleInstance"
            android:taskAffinity="">
            <layout
                android:defaultHeight="500dp"
                android:defaultWidth="750dp"
                android:gravity="top|end"
                android:minWidth="500dp"
                android:minHeight="500dp" />
        </activity>

主要用来设置一些布局属性

android:defaultWidth :以自由形状模式启动时 Activity 的默认宽度。
android:defaultHeight:以自由形状模式启动时 Activity 的默认高度
android:gravity:以自由形状模式启动时 Activity 的初始位置。请参阅 Gravity 参考资料,了解合适的值设置。
android:minimalHeight、android:minimalWidth:分屏和自由形状模式中 Activity 的最小高度和最小宽度。 如果用户在分屏模式中移动分界线,使 Activity 尺寸低于指定的最小值,系统会将 Activity 裁剪为用户请求的尺寸。

进入自由形状模式的方法:

1). 打开模拟器或者用usb线连接已root了的设备
2). 在cmd命令行中输入adb shell
3). 然后输入su获得root操作权限
4). 输入settings put global enable_freeform_support 1
5). 重启模拟器或设备。

6、start activity with Launch Bounds

 public void onStartLaunchBoundsActivity(View view) {
        Log.d(mLogTag, "** starting LaunchBoundsActivity");

        // Define the bounds in which the Activity will be launched into.
        Rect bounds = new Rect(500, 300, 100, 0);

        // Set the bounds as an activity option.
        ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchBounds(bounds);

        // Start the LaunchBoundsActivity with the specified options
        Intent intent = new Intent(this, LaunchBoundsActivity.class);
        startActivity(intent, options.toBundle());

    }

这个和上面第5点差不多 只是在代码中设置新activity的尺寸和屏幕位置。

官方示例给的还是挺全的,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值