隨著手機(jī)屏幕越來越大,單手操作手機(jī)越來越難,所以一些大廠早就開始研究多窗口秤掌,如iphone文兢、samsung的單手模式,作為一個(gè)發(fā)展趨勢google肯定也不會(huì)不考慮用戶的體驗(yàn)吕粗,所以在Android N中增加了多窗口的操作。
本編通過android官方給出的示例結(jié)合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 標(biāo)題,則可以拖動(dòng)該 Activity 至屏幕突出顯示的區(qū)域,使Activity 進(jìn)入多窗口模式竭缝。
2房维、若用戶長按 Overview 按鈕,設(shè)備上的當(dāng)前 Activity 將進(jìn)入多窗口模式歌馍,同時(shí)將打開 Overview屏幕握巢,用戶可在該屏幕中選擇要共享屏幕的另一個(gè) Activity。
上面是github上示例的UI 按照它的顯示我們一個(gè)一個(gè)分析
1松却、首先 start basic Default Activity 打開的是默認(rèn)的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));
}
這個(gè)和正常的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” 運(yùn)行后發(fā)現(xiàn)如果在分屏界面晓锻,會(huì)提示“應(yīng)用不支持分屏”然后全屏顯示歌焦。
所以這個(gè)配置是啟用和禁用多窗口顯示的,N里面默認(rè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="" />
效果提
在啟動(dòng)新 Activity 時(shí),用戶可以提示系統(tǒng)如果可能躁锁,應(yīng)將新 Activity 顯示在當(dāng)前 Activity 旁邊纷铣。 要執(zhí)行此操作,可使用標(biāo)志 Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT战转。 傳遞此標(biāo)志將請求以下行為
1搜立、如果設(shè)備處于分屏模式,系統(tǒng)會(huì)嘗試在啟動(dòng)系統(tǒng)的 Activity 旁創(chuàng)建新 Activity槐秧,這樣兩個(gè) Activity 將共享屏幕啄踊。 系統(tǒng)并不一定能實(shí)現(xiàn)此操作,但如果可以刁标,系統(tǒng)將使兩個(gè) Activity 處于相鄰的位置颠通。
2、如果設(shè)備不處于分屏模式膀懈,則該標(biāo)志無效顿锰。
并且發(fā)現(xiàn)onMultiWindowModeChanged函數(shù)執(zhí)行了 這個(gè)函數(shù)的作用是 Activity 進(jìn)入或退出多窗口模式時(shí)系統(tǒng)將調(diào)用此方法。 在 Activity 進(jìn)入多窗口模式時(shí)吏砂,系統(tǒng)向該方法傳遞 true 值撵儿,在退出多窗口模式時(shí),則傳遞 false 值狐血。
4淀歇、start activity that handles configurationchanges
這個(gè)主要處理一些配置文件的改變
下面還有兩個(gè)事件
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>
主要用來設(shè)置一些布局屬性
android:defaultWidth :以自由形狀模式啟動(dòng)時(shí) Activity 的默認(rèn)寬度匈织。 android:defaultHeight:以自由形狀模式啟動(dòng)時(shí) Activity 的默認(rèn)高度 android:gravity:以自由形狀模式啟動(dòng)時(shí) Activity 的初始位置浪默。請參閱 Gravity 參考資料牡直,了解合適的值設(shè)置。 android:minimalHeight纳决、android:minimalWidth:分屏和自由形狀模式中 Activity 的最小高度和最小寬度碰逸。 如果用戶在分屏模式中移動(dòng)分界線,使 Activity 尺寸低于指定的最小值阔加,系統(tǒng)會(huì)將 Activity 裁剪為用戶請求的尺寸饵史。
進(jìn)入自由形狀模式的方法:
1). 打開模擬器或者用usb線連接已root了的設(shè)備
2). 在cmd命令行中輸入adb shell
3). 然后輸入su獲得root操作權(quán)限
4). 輸入settings put global enable_freeform_support 1
5). 重啟模擬器或設(shè)備。
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());
}
這個(gè)和上面第5點(diǎn)差不多 只是在代碼中設(shè)置新activity的尺寸和屏幕位置胳喷。