当前位置:网站首页>折叠旧版应用程序
折叠旧版应用程序
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 应用程序中。在许多情况下,更新现有布局以支持可折叠设备和大屏幕设备上的两列模式非常简单。
您是否让旧版应用在可折叠设备上看起来不错?请在评论中分享您的想法。
边栏推荐
- MySQL60 homework
- c语言进阶篇:指针(五)
- Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
- Google Earth Engine ——快速实现MODIS影像NDVI动画的在线加载并导出
- MySQL删除表数据 MySQL清空表命令 3种方法
- Outsourcing worked for three years, it was abolished...
- Structured Streaming报错记录:Overloaded method foreachBatch with alternatives
- ValueError: Append mode is not supported with xlsxwriter解决方案
- 文字的选择与排版
- Navigation Bar----Personal Center Dropdown
猜你喜欢
基于ABP实现DDD--领域服务、应用服务和DTO实践
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
转义字符笔记记录
DistSQL 深度解析:打造动态化的分布式数据库
LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
【机器学习】梯度下降背后的数学之美
Image Restoration by Estimating Frequency Distribution of Local Patches
Use the map function to operate on each element in the list It seems that you don't need a map
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
A simple rich text editor
随机推荐
opencv,numpy,tensor格式转换
KingbaseES V8R6备份恢复案例之---同一数据库创建不同stanza备份
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
【菜鸡含泪总结】如何用pip、anaconda安装库
vlan简单实验
【Network Security Column Directory】--Penguin Column Navigation
MySQL60 homework
[Limited Time Bonus] 21-Day Learning Challenge - MySQL from entry to mastery
牛客小白月赛53 A-E
Enhancing Quality for HEVC Compressed Videos
QUALITY-GATED CONVOLUTIONAL LSTM FOR ENHANCING COMPRESSED VIDEO
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNsfor Soft Decoding of JPEG-Compressed Images
MySQL删除表数据 MySQL清空表命令 3种方法
大家都在用的plm项目管理软件有哪些
微信公众号授权登录后报redirect_uri参数错误的问题
ValueError: Append mode is not supported with xlsxwriter解决方案
数据质量提升
JUC原子类详解
JDBC(详解)