当前位置:网站首页>折叠旧版应用程序
折叠旧版应用程序
2022-07-30 21:32:00 【yihanss】
前言
本系列专注于可折叠和大屏幕设备。它的大部分文章都使用 Kotlin,它在几年前已成为 Android 应用程序的首选编程语言。然而,仍然有很多基于 Java 的 Android 应用程序。将它们称为遗留应用程序并不意味着它们不再处于积极开发中,或者至少处于维护模式。因此,我们难道不应该确保它们在新设备类别上也能提供出色的体验吗?我们当然应该。因此,在本文中,我将向您展示如何使用Views 使 Java Android 应用程序在可折叠设备和大屏幕上表现良好。
Jetpack 窗口管理器
使用Jetpack WindowManager,您可以查询与可折叠设备相关的某些设备特征。例如,该库会告诉您设备是否有铰链,以及该铰链是否挡住了屏幕的某些区域。例如,双屏设备Surface Duo及其继任者就是这种情况。
Jetpack WindowManager使用基于Flows 的 Kotlin 友好 api。幸运的是,它也给了 Java 一些爱。要在 Java 应用程序中使用该库,您需要在模块级build.gradle文件中添加实现依赖项:
implementation "androidx.window:window-java:1.0.0-rc01"
下一步是确保您的应用从Jetpack WindowManager接收信息。首先,让我们声明一个实例变量:
private WindowInfoTrackerCallbackAdapter adapter;
它在以下位置初始化onCreate():
adapter = new WindowInfoTrackerCallbackAdapter(
WindowInfoTracker.Companion.getOrCreate(
this
)
);
现在是时候将它与我们的代码连接起来了。为简洁起见,我使用旧式Activity回调,但请考虑使用Jetpack
Lifecycle。
@Override
protected void onStart() {
super.onStart();
adapter.addWindowLayoutInfoListener(this,
ContextCompat.getMainExecutor(this),
callback);
…
}
@Override
protected void onStop() {
super.onStop();
adapter.removeWindowLayoutInfoListener(callback);
…
}
那么,什么是callback?
private final Consumer<WindowLayoutInfo> callback =
(windowLayoutInfo -> {
final var windowMetrics = WindowMetricsCalculator.getOrCreate()
.computeCurrentWindowMetrics(this);
final var windowWidth = windowMetrics.getBounds().width();
final var windowHeight = windowMetrics.getBounds().height();
final var leftPaneParams = binding.leftPane.getLayoutParams();
final var rightPaneParams = binding.rightPane.getLayoutParams();
final var hingeParams = binding.hinge.getLayoutParams();
var hasFoldingFeature = false;
List<DisplayFeature> displayFeatures
= windowLayoutInfo.getDisplayFeatures();
…
});
首先,我们设置几个变量。WindowMetricsCalculator帮助我们获得窗口指标。所以,windowWidth并windowHeight包含宽度和高度。那么leftPane,rightPane和hinge呢?
布局结构
leftPane并rightPane表示您的应用程序的内容,以两列形式呈现。hinge放置在它们之间。这三个线性布局,LinearLayout如果需要,他们的方向会改变。我很快就会给你看。
现在,您可能在想,但我的应用程序没有使用这种结构。
我知道。大多数旧版应用程序都没有针对平板电脑进行优化。但这就是这种简单方法的美妙之处。只需创建用户界面的根目录leftPane,添加hingeandrightPane并将所有三个包装在一个LinearLayout. 如果您的应用程序严重依赖Fragments,则可能需要进行更大量的返工。
现在,让我们看看如何处理displayFeatures.
for (DisplayFeature displayFeature : displayFeatures) {
FoldingFeature foldingFeature =
(FoldingFeature) displayFeature;
if (foldingFeature != null) {
hasFoldingFeature = true;
boolean isVertical = foldingFeature.getOrientation()
== FoldingFeature.Orientation.VERTICAL;
final var foldingFeatureBounds = foldingFeature.getBounds();
hingeParams.width = foldingFeatureBounds.width();
hingeParams.height = foldingFeatureBounds.height();
if (isVertical) {
binding.parent.setOrientation(LinearLayout.HORIZONTAL);
leftPaneParams.width = foldingFeatureBounds.left;
leftPaneParams.height =
LinearLayout.LayoutParams.MATCH_PARENT;
rightPaneParams.width = windowWidth
- foldingFeatureBounds.right;
rightPaneParams.height =
LinearLayout.LayoutParams.MATCH_PARENT;
} else {
int[] intArray = new int[2];
binding.leftPane.getLocationOnScreen(intArray);
binding.parent.setOrientation(LinearLayout.VERTICAL);
leftPaneParams.width =
LinearLayout.LayoutParams.MATCH_PARENT;
leftPaneParams.height =
foldingFeatureBounds.top - intArray[1];
rightPaneParams.width =
LinearLayout.LayoutParams.MATCH_PARENT;
rightPaneParams.height = windowHeight
- foldingFeatureBounds.bottom;
}
}
}
…
那么,这里发生了什么?如果我们找到铰链,我们
- 设置
leftPane,rightPane, 和hinge相应的大小 LieaerLayout根据铰链的配置配置方向
你注意到了getLocationOnScreen()吗?此代码在铰链的方向为水平时执行。然后,leftPane和rightPane被垂直排列,hinge在它们之间。虽然两个屏幕通常大小相同,但一个屏幕会包含状态栏和应用栏,因此内容的区域会更小。我发现以这种方式计算偏移量是最可靠的。
我们的最后一步是处理没有铰链的情况。普通智能手机和平板电脑就是这种情况。
if (!hasFoldingFeature) {
final float density =
getResources().getDisplayMetrics().density;
final float dp = windowMetrics.getBounds().width() / density;
binding.parent.setOrientation(LinearLayout.HORIZONTAL);
hingeParams.width = 0;
hingeParams.height = 0;
if (dp >= 600) {
leftPaneParams.width = windowWidth / 2;
leftPaneParams.height =
LinearLayout.LayoutParams.MATCH_PARENT;
rightPaneParams.width = windowWidth / 2;
rightPaneParams.height =
LinearLayout.LayoutParams.MATCH_PARENT;
} else {
leftPaneParams.width =
LinearLayout.LayoutParams.MATCH_PARENT;
leftPaneParams.height =
LinearLayout.LayoutParams.MATCH_PARENT;
rightPaneParams.width = 0;
rightPaneParams.height = 0;
}
}
我通过计算密度独立像素的屏幕宽度来决定是否要使用两列模式。如果计算值小于 600,我会配置单列布局。否则leftPane,rightPane将具有相同的大小。在任何情况下,铰链的大小都设置为 0。
结论
在本文中,我向您展示了如何将Jetpack WindowManager合并到 Java Android 应用程序中。在许多情况下,更新现有布局以支持可折叠设备和大屏幕设备上的两列模式非常简单。
您是否让旧版应用在可折叠设备上看起来不错?请在评论中分享您的想法。
边栏推荐
- TransGAN代码复现—九天毕昇平台
- GPGGA NTRIP RTCM 笔记
- navicat无法连接mysql超详细处理方法
- MySQL60 homework
- 一个网络两种用途!南开&哈工程提出TINet,通过细化纹理和边缘,在显著性目标检测和伪装目标检测上实现双SOTA!...
- QUALITY-GATED CONVOLUTIONAL LSTM FOR ENHANCING COMPRESSED VIDEO
- Markdown的使用
- KingbaseES V8R6备份恢复案例之---同一数据库创建不同stanza备份
- 【Network Security Column Directory】--Penguin Column Navigation
- 8 ways to get element attributes in JS
猜你喜欢

【网络安全专栏目录】--企鹅专栏导航
![[Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques](/img/51/b351385c1f0f4e0a545e54c8ae7491.png)
[Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques

IDEA2018.3.5 cancel double-click Shift shortcut
![[Deep Learning] Target Detection | SSD Principle and Implementation](/img/07/ea4ff3ffbe7e0c11ff7baec0e1818f.jpg)
[Deep Learning] Target Detection | SSD Principle and Implementation

navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)

基于ABP实现DDD--领域逻辑和应用逻辑

关于MySQL主从复制的数据同步延迟问题

Deep Non-Local Kalman Network for VideoCompression Artifact Reduction

mysql 时间字段默认设置为当前时间

【Nacos】解决Nacos下载速度缓慢的问题
随机推荐
拿什么来保护数据安全?基层数据安全体系建设待提升
nVisual网络可视化管理平台功能和价值点
MySQL笔记2(函数,约束,多表查询,事务)
Image Restoration by Estimating Frequency Distribution of Local Patches
Outsourcing worked for three years, it was abolished...
Niu Ke Xiaobaiyue Race 53 A-E
How strict Typescript strict mode?
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
navicat无法连接mysql超详细处理方法
A simple rich text editor
HCIP第十六天
牛客小白月赛53 A-E
新书上市 |《谁在掷骰子?》在“不确定性时代”中确定前行
触摸屏状态机
Google Earth Engine ——快速实现MODIS影像NDVI动画的在线加载并导出
【深度学习】对迁移学习中域适应的理解和3种技术的介绍
我是如何让公司后台管理系统焕然一新的(上) -性能优化
【信息安全技术】RSA算法的研究及不同优化策略的比较
navicat新建数据库
Redis数据更新,是先更新数据库还是先更新缓存?