kotlin - 平板分屏,左右拖動,2個Activity計算寬度,使用ActivityOptions、Rect
?使用平板,api33才支持,可以左右拖動,分屏第一個頁面 , 思考:分屏后,對整個app的影響,包括屏幕旋轉android:configChanges,點擊跳轉,傳遞參數,屏幕變小后的布局、影響。
分屏有3種方式(這里實現第一種):
一:任務棧中有Main,A頁面,A打開全新的B頁面分屏,A左邊, B右邊
二:任務棧中有Main,A頁面,Main和A頁面分屏,Main在左邊,A在右邊
三:任務棧中只有Main頁面,分屏左右2部分(left,right),把右部分的view緩存到新打開的A頁面顯示。分屏后Main頁面顯示left的視圖,A頁面顯示right的視圖
package com.example.androidkotlindemo2.pad.splitscreen;import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.Button;import androidx.appcompat.app.AppCompatActivity;import com.example.androidkotlindemo2.R;
import com.example.androidkotlindemo2.utils.LogUtils;/*** Author : wn* Email : maoning20080808@163.com* Date : 2025/8/10 10:24* Description : 使用平板,api33才支持,可以左右拖動,分屏第一個頁面 , 思考:分屏后,對整個app的影響,包括屏幕旋轉android:configChanges,點擊跳轉,傳遞參數,屏幕變小后的布局、影響。* 分屏有3種方式(這里實現第一種):* 一:任務棧中有Main,A頁面,A打開全新的B頁面分屏,A左邊, B右邊* 二:任務棧中有Main,A頁面,Main和A頁面分屏,Main在左邊,A在右邊* 三:任務棧中只有Main頁面,分屏左右2部分(left,right),把右部分的view緩存到新打開的A頁面顯示。分屏后Main頁面顯示left的視圖,A頁面顯示right的視圖* 官方分屏窗口:https://github.com/googlearchive/android-MultiWindowPlayground*/
public class SplitScreenAActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.split_screen_a);Button btnSplitScreen = findViewById(R.id.btn_split_screen_a);Button btnFullScreen = findViewById(R.id.btn_full_screen_a);btnSplitScreen.setOnClickListener(v -> {// 啟動分屏模式if (isInMultiWindowMode()) {LogUtils.Companion.d("啟動分屏if");// 如果已經在分屏模式,直接啟動SplitScreenAActivitystartActivity(new Intent(this, SplitScreenAActivity.class));} else {LogUtils.Companion.d("啟動分屏else");// 進入分屏模式并啟動SplitScreenAActivityenterSplitScreen();}});btnFullScreen.setOnClickListener(v -> {// 退出分屏模式(如果正在分屏)if (isInMultiWindowMode()) {exitSplitScreen();}});}private void enterSplitScreen() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {// 1. 首先進入多窗口模式if (!isInMultiWindowMode()) {LogUtils.Companion.d("enterSplitScreen 11");// 啟動自己進入多窗口模式Intent selfIntent = new Intent(this, SplitScreenAActivity.class);selfIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |Intent.FLAG_ACTIVITY_NEW_TASK);DisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);// 配置SecondaryActivity的邊界(右側)Rect leftBounds = new Rect(0, 0, metrics.widthPixels/2, metrics.heightPixels);ActivityOptions options = ActivityOptions.makeBasic();options.setLaunchBounds(leftBounds);startActivity(selfIntent, options.toBundle());//return;}// 2. 現在已經在多窗口模式,啟動第二個ActivityDisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);int halfWidth = metrics.widthPixels / 2;// 配置SecondaryActivity的邊界(右側)Rect rightBounds = new Rect(halfWidth, 0, metrics.widthPixels, metrics.heightPixels);ActivityOptions options = ActivityOptions.makeBasic();options.setLaunchBounds(rightBounds);LogUtils.Companion.d("enterSplitScreen 22");Intent secondaryIntent = new Intent(this, SplitScreenBActivity.class);secondaryIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(secondaryIntent, options.toBundle());} else {// 不支持分屏的設備,簡單啟動ActivitystartActivity(new Intent(this, SplitScreenBActivity.class));}}private void exitSplitScreen() {SplitScreenBActivity.mActivity.finish();/*// 關閉SplitScreenBActivityIntent intent = new Intent(this, SplitScreenBActivity.class);//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent);//finish();*/}
}
package com.example.androidkotlindemo2.pad.splitscreen;import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Button;import androidx.appcompat.app.AppCompatActivity;import com.example.androidkotlindemo2.R;/*** Author : wn* Email : maoning20080808@163.com* Date : 2025/8/10 10:24* Description :*/
public class SplitScreenBActivity extends AppCompatActivity {//這里簡單使用,可以使用廣播,獲取任務棧的方式實現。public static SplitScreenBActivity mActivity;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.split_screen_b);mActivity = this;Button btnBack = findViewById(R.id.btn_back);btnBack.setOnClickListener(v -> finish());}@Overridepublic void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);}@Overrideprotected void onStop() {super.onStop();// 當ActivityB停止時(例如用戶退出分屏),可以執行一些清理操作}
}
split_screen_a.xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:background="#FF9800"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="分屏第1個頁面"android:textSize="24sp"android:textColor="#FFFFFF"/><Buttonandroid:id="@+id/btn_split_screen_a"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:text="進入分屏模式"/><Buttonandroid:id="@+id/btn_full_screen_a"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:text="全屏模式"/></LinearLayout>
split_screen_b.xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:background="#4CAF50"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="分屏第2個頁面"android:textSize="24sp"android:textColor="#FFFFFF"/><Buttonandroid:id="@+id/btn_back"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:text="返回"/></LinearLayout>