Android Folding

发布于:2024-12-06 ⋅ 阅读:(95) ⋅ 点赞:(0)

查看文档

https://developer.android.google.cn/develop/ui/compose/layouts/adaptive/foldables/make-your-app-fold-aware?authuser=0&hl=zh-cn

引入库
implementation ‘androidx.window:window:1.3.0’

编码动态设置屏幕

    fun checkFoldingState(){
        lifecycleScope.launch(Dispatchers.Main) {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                WindowInfoTracker.getOrCreate(this@DemoNestedActivity)
                    .windowLayoutInfo(this@DemoNestedActivity)
                    .collect { layoutInfo ->
                        val foldingFeature:FoldingFeature? = layoutInfo.displayFeatures
                            .filterIsInstance<FoldingFeature>()
                            .firstOrNull()
                        val b = isTableTopPosture(foldingFeature)
                        val str =  "check = ${b}"
                        Log.e("vic_ddd_fold","str = ${str}")
                        button?.text = str

                        if (b){
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                        }else{
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        }
                    }
            }
        }
    }

    fun isTableTopPosture(foldFeature : FoldingFeature?) : Boolean {
        //contract { returns(true) implies (foldFeature != null) }
        Log.e("vic_ddd_fold","state = ${foldFeature?.state}")
        Log.e("vic_ddd_fold","foldFeature = ${foldFeature}")
        Log.e("vic_ddd_fold","orientation = ${foldFeature?.orientation}")
        //true 是折叠的 false 是展开的
        if (foldFeature == null){
            return true
        }
        if (foldFeature?.state == FoldingFeature.State.HALF_OPENED){
            return true
        }

        if (foldFeature?.state == FoldingFeature.State.FLAT){
            return false
        }
        return true

        //return foldFeature?.state == FoldingFeature.State.HALF_OPENED
                //&&  foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL
    }

package gw.com.android.app

import android.content.pm.ActivityInfo
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoTracker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import www.com.library.app.Logger

object FoldingActivityHelper {

    fun checkFoldingState(activity: AppCompatActivity){
        activity.lifecycleScope.launch(Dispatchers.Main) {
            activity.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                WindowInfoTracker.getOrCreate(activity)
                    .windowLayoutInfo(activity)
                    .collect { layoutInfo ->
                        val foldingFeature: FoldingFeature? = layoutInfo.displayFeatures
                            .filterIsInstance<FoldingFeature>()
                            .firstOrNull()
                        val b = isTableTopPosture(activity,foldingFeature)
                        Logger.e("vic_ddd_fold","isTableTopPosture = ${b}")
                        if (!b){
                            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        }
                    }
            }
        }
    }

    fun isTableTopPosture(activity: AppCompatActivity,foldFeature : FoldingFeature?) : Boolean {
        //contract { returns(true) implies (foldFeature != null) }
        Logger.e("vic_ddd_fold","state = ${foldFeature?.state}")
        Logger.e("vic_ddd_fold","foldFeature = ${foldFeature}")
        Logger.e("vic_ddd_fold","orientation = ${foldFeature?.orientation}")
        //true 是折叠的 false 是展开的
        if (foldFeature == null){
            return true
        }
        if (foldFeature?.state == FoldingFeature.State.HALF_OPENED){
            return true
        }

        if (foldFeature?.state == FoldingFeature.State.FLAT){
            //是折叠屏 判断一下 宽和高
            activity.window.decorView.postDelayed({
                val width = activity.window.decorView.measuredWidth
                val height = activity.window.decorView.measuredHeight
                Logger.e("vic_ddd_fold","222 width = ${width} height = ${height} orientation = ${foldFeature?.orientation}")
                //width = 2640 height = 1080 orientation = VERTICAL
                //222 width = 1080 height = 2640 orientation = HORIZONTAL
                if (foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL){
                    if (width>height){
                        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    }else{
                        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    }
                }
                if (foldFeature.orientation == FoldingFeature.Orientation.VERTICAL){
                    if (height>width){
                        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    }else{
                        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    }
                }

            },200)

            return false
        }
        return true
        //return foldFeature?.state == FoldingFeature.State.HALF_OPENED
        //&&  foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL
    }




}

网站公告

今日签到

点亮在社区的每一天
去签到