当前位置: 首页 > article >正文

Android Folding

查看文档

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
    }




}

http://www.kler.cn/a/422131.html

相关文章:

  • 查询品牌涉及两张表(brand、brand_admin_mapping)
  • 003-SpringBoot整合Pagehelper
  • CSS函数
  • Burp Suite 实战指南:Proxy 捕获与修改流量、HTTP History 筛选与分析
  • 【小白学机器学习41】如何从正态分布的总体中去抽样?比较不同的取样方差的差别
  • uniapp在App端引用echarts组件,解决无法渲染formatter问题
  • kafka 配置消息编码格式、解决消费者中文乱码
  • uniapp动态表单
  • Llama模型分布式训练(微调)
  • 在RK3588/RK3588s中提升yolov8推理速度
  • WPF_2
  • IOS ARKit进行图像识别
  • 05云计算HCIA学习笔记
  • Git操作学习2
  • Simulink的SIL软件在环测试
  • Modern Effective C++ 条款二十九三十:移动语义和完美转发失败的情况
  • 基于Matlab实现三维点坐标生成点云(源码+数据)
  • IDEA中Maven相关使用
  • IDEA中更改了项目模块名,IDEA丢失该模块的问题
  • 基于Java Springboot武汉市公交路线查询APP且微信小程序
  • elasticsearch 8.x常用命令
  • 容器运行应用及Docker命令
  • SpringBoot 基于 MVC 高校办公室行政事务管理系统:设计构思与实现范例展示
  • XRP 深度解析:从技术到 Meme 币交易指南
  • TCP/IP 9 网络安全
  • C# CancellationToken 终止操作