android 多窗口

随着手机屏幕越来越大,单手操作手机越来越难,所以一些大厂早就开始研究多窗口,如iphone、samsung的单手模式,作为一个发展趋势google肯定也不会不考虑用户的体验,所以在Android N中增加了多窗口的操作。
本编通过android官方给出的示例结合api指南及前人分析来研究一下android 的多窗口操作。
https://github.com/googlesamples/android-MultiWindowPlayground

https://developer.android.com/guide/topics/ui/multi-window.html

http://blog.csdn.net/qibin0506/article/details/52270674

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

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

《android 多窗口》 1.gif
《android 多窗口》 4.png

上面是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);
    }

  <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="" />

效果提

《android 多窗口》 2.gif

在启动新 Activity 时,用户可以提示系统如果可能,应将新 Activity 显示在当前 Activity 旁边。 要执行此操作,可使用标志 Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT。 传递此标志将请求以下行为
1、如果设备处于分屏模式,系统会尝试在启动系统的 Activity 旁创建新 Activity,这样两个 Activity 将共享屏幕。 系统并不一定能实现此操作,但如果可以,系统将使两个 Activity 处于相邻的位置。
2、如果设备不处于分屏模式,则该标志无效。
并且发现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, 500, 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的尺寸和屏幕位置。

    原文作者:solary2016
    原文地址: https://www.jianshu.com/p/f4bf04fae3ce
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞