[Android] ImageView.ScaleType设置图解

本文为优快云博客的一个示例链接,具体内容未给出,通常包含信息技术领域的深入探讨和技术分享。
我发现这样设置好像能达到原来的背景效果。1、colors.xml代码如下: #FFBB86FC #FF6200EE #FF3700B3 #FF03DAC5 #FF018786 #FF000000 #FFFFFFFF <!-- 防止白条 --> <color name="background_splash">#7F3300CC</color> <!-- 内容区域背景 --> <color name="surface_background">#FFFFFFFF</color> </resources> 2、themes.xml代码如下:<resources xmlns:tools="http://schemas.android.com/tools"> <style name="Theme.Bus" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <!-- 主色调 --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- 次要色 --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- 状态栏颜色 --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <!-- 导航栏颜色 --> <item name="android:navigationBarColor">@color/purple_700</item> <item name="android:windowBackground">@color/background_splash</item> </style> </resources> 3、activity_main.xml代码如下:<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:background="@color/black"> <!-- 替代原来的 paddingTop --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> <fragment android:id="@+id/nav_host_fragment_activity_main" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/mobile_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout> 4、fragment_home代码如下: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=“.ui.home.HomeFragment” android:fitsSystemWindows=“true” android:background=“@color/surface_background”> <!-- 🔍 输入框 --> <EditText android:id="@+id/home_input" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="请输入需要查询的公交线路或站点" android:background="@drawable/rounded_edittext" android:minHeight="48dp" android:textSize="16sp" android:padding="12dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/home_search" app:layout_constraintHorizontal_chainStyle="packed" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" android:layout_marginTop="88dp" /> <!-- 🔎 搜索按钮 --> <Button android:id="@+id/home_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" app:layout_constraintTop_toTopOf="@id/home_input" app:layout_constraintBottom_toBottomOf="@id/home_input" app:layout_constraintStart_toEndOf="@id/home_input" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" /> <!-- 🚌 图片:居中偏上 --> <ImageView android:id="@+id/image_bus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" android:src="@drawable/bus" app:layout_constraintTop_toBottomOf="@id/home_input" app:layout_constraintBottom_toTopOf="@+id/text_home" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="144dp" android:layout_marginBottom="144dp"/> <!-- ℹ️ 底部说明文字 --> <TextView android:id="@+id/text_home" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="11sp" app:layout_constraintTop_toBottomOf="@id/image_bus" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 5、fragment_map.xml代码如下: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=“.ui.map.MapFragment” android:fitsSystemWindows=“true” android:background=“@color/surface_background”> <!-- 🔹 起点输入框 --> <EditText android:id="@+id/map_input1" android:layout_width="0dp" android:layout_height="48dp" android:hint="请输入起点" android:background="@drawable/rounded_edittext" android:padding="12dp" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" android:layout_marginTop="28dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/guideline_search" app:layout_constraintTop_toTopOf="parent" /> <!-- 🔹 终点输入框 --> <EditText android:id="@+id/map_input2" android:layout_width="0dp" android:layout_height="48dp" android:hint="请输入终点" android:background="@drawable/rounded_edittext" android:padding="12dp" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/guideline_search" app:layout_constraintTop_toBottomOf="@id/map_input1" /> <!-- ✅ 分割线:75% 处 --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.75" /> <!-- 🔍 搜索按钮:纵向拉高,覆盖两个输入框 --> <Button android:id="@+id/map_search" android:layout_width="0dp" android:layout_height="0dp" android:text="搜索" android:textSize="16sp" android:gravity="center" app:layout_constraintStart_toStartOf="@id/guideline_search" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@id/map_input1" app:layout_constraintBottom_toBottomOf="@id/map_input2" android:layout_marginEnd="16dp" /> <!-- 🗺️ 地图视图:从终点输入框下方开始,延伸到底部(避开导航栏) --> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/map_input2" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="4dp" android:layout_marginBottom="0dp"/> <!-- 改为约束到文字上方 --> </androidx.constraintlayout.widget.ConstraintLayout> 6、fragment_settings.xml代码如下: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=“.ui.settings.SettingsFragment” android:fitsSystemWindows=“true” android:background=“@color/surface_background”> <!-- 🔹 Material Design 按钮组 --> <com.google.android.material.button.MaterialButton android:id="@+id/btn_gps" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="权限设置" style="@style/Widget.MaterialComponents.Button.OutlinedButton" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="56dp" /> <com.google.android.material.button.MaterialButton android:id="@+id/btn_apps" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="关于 Bus" style="@style/Widget.MaterialComponents.Button.OutlinedButton" app:layout_constraintTop_toBottomOf="@id/btn_gps" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="16dp" /> <com.google.android.material.button.MaterialButton android:id="@+id/btn_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户调研" style="@style/Widget.MaterialComponents.Button.OutlinedButton" app:layout_constraintTop_toBottomOf="@id/btn_apps" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="16dp" /> <com.google.android.material.button.MaterialButton android:id="@+id/btn_exit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="退出程序" style="@style/Widget.MaterialComponents.Button.OutlinedButton" app:layout_constraintTop_toBottomOf="@id/btn_user" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="24dp" /> <!-- 🔹 图片:放在按钮下方 --> <ImageView android:id="@+id/image_bus" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="64dp" android:layout_marginBottom="64dp" android:src="@drawable/bus" app:layout_constraintBottom_toTopOf="@+id/text_settings" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/btn_exit" /> <!-- 🔹 底部 TextView --> <TextView android:id="@+id/text_settings" android:text="公交查询系统-1.0" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="11sp" app:layout_constraintTop_toBottomOf="@id/image_bus" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 7、activity_survey.xml代码如下: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 问题1:评分 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="32dp" android:text="🚌 用户体验调研" android:textSize="24sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1. 您对本应用的整体评分是?" android:textStyle="bold" android:layout_marginBottom="8dp" /> <RadioGroup android:id="@+id/radioGroupRating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="24dp"> <RadioButton android:text="⭐ 1分 - 非常不满意" android:id="@+id/rb1" android:layout_height="match_parent" android:layout_width="match_parent"/> <RadioButton android:text="⭐⭐ 2分 - 不满意" android:id="@+id/rb2" android:layout_height="match_parent" android:layout_width="match_parent"/> <RadioButton android:text="⭐⭐⭐ 3分 - 一般" android:id="@+id/rb3" android:layout_height="match_parent" android:layout_width="match_parent"/> <RadioButton android:text="⭐⭐⭐⭐ 4分 - 满意" android:id="@+id/rb4" android:layout_height="match_parent" android:layout_width="match_parent"/> <RadioButton android:text="⭐⭐⭐⭐⭐ 5分 - 非常满意" android:id="@+id/rb5" android:layout_height="match_parent" android:layout_width="match_parent"/> </RadioGroup> <!-- 问题2:满意度 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2. 您对以下哪些方面或功能感到满意?(可多选)" android:textStyle="bold" android:layout_marginBottom="8dp" /> <CheckBox android:id="@+id/cb_ui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面美观易用" /> <CheckBox android:id="@+id/cb_speed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载速度快" /> <CheckBox android:id="@+id/cb_information" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询信息准确" /> <CheckBox android:id="@+id/cb_other" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其他" /> <!-- 问题3:建议 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3. 您有什么改进建议?" android:textStyle="bold" android:layout_marginTop="24dp" android:layout_marginBottom="8dp" /> <EditText android:id="@+id/et_suggestion" android:layout_width="match_parent" android:layout_height="120dp" android:gravity="top" android:hint="请输入您的建议..." android:inputType="textMultiLine" android:minLines="3" /> <!-- 提交按钮 --> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交反馈" android:textSize="18sp" android:layout_marginTop="32dp" style="?android:attr/buttonStyle" /> </LinearLayout> </ScrollView> 8、activity_about.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:padding="20dp"> <!-- 软件名称 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="公交查询系统" android:textSize="28sp" android:textStyle="bold" android:textColor="#000" android:layout_marginBottom="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bus" android:textSize="18sp" android:textColor="#555" android:layout_marginBottom="80dp" /> <!-- 软件图片 --> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/bus" android:layout_marginBottom="100dp"/> <!-- 版本信息 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="版本:1.0" android:textSize="16sp" android:layout_marginBottom="20dp" /> <!-- 开发信息 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开发团队:white luminous" android:textSize="16sp" android:layout_marginBottom="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="邮箱:support@busapp.cn" android:textSize="16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hao" android:textSize="8sp" android:layout_marginTop="100dp" android:textColor="#EEE"/> </LinearLayout>
10-16
内容概要:本文介绍了一个基于多传感器融合的定位系统设计方案,采用GPS、里程计和电子罗盘作为定位传感器,利用扩展卡尔曼滤波(EKF)算法对多源传感器数据进行融合处理,最终输出目标的滤波后位置信息,并提供了完整的Matlab代码实现。该方法有效提升了定位精度与稳定性,尤其适用于存在单一传感器误差或信号丢失的复杂环境,如自动驾驶、移动采用GPS、里程计和电子罗盘作为定位传感器,EKF作为多传感器的融合算法,最终输出目标的滤波位置(Matlab代码实现)机器人导航等领域。文中详细阐述了各传感器的数据建模方式、状态转移与观测方程构建,以及EKF算法的具体实现步骤,具有较强的工程实践价值。; 适合人群:具备一定Matlab编程基础,熟悉传感器原理和滤波算法的高校研究生、科研人员及从事自动驾驶、机器人导航等相关领域的工程技术人员。; 使用场景及目标:①学习和掌握多传感器融合的基本理论与实现方法;②应用于移动机器人、无人车、无人机等系统的高精度定位与导航开发;③作为EKF算法在实际工程中应用的教学案例或项目参考; 阅读建议:建议读者结合Matlab代码逐行理解算法实现过程,重点关注状态预测与观测更新模块的设计逻辑,可尝试引入真实传感器数据或仿真噪声环境以验证算法鲁棒性,并进一步拓展至UKF、PF等更高级滤波算法的研究与对比。
内容概要:文章围绕智能汽车新一代传感器的发展趋势,重点阐述了BEV(鸟瞰图视角)端到端感知融合架构如何成为智能驾驶感知系统的新范式。传统后融合与前融合方案因信息丢失或算力需求过高难以满足高阶智驾需求,而基于Transformer的BEV融合方案通过统一坐标系下的多源传感器特征融合,在保证感知精度的同时兼顾算力可行性,显著提升复杂场景下的鲁棒性与系统可靠性。此外,文章指出BEV模型落地面临大算力依赖与高数据成本的挑战,提出“数据采集-模型训练-算法迭代-数据反哺”的高效数据闭环体系,通过自动化标注与长尾数据反馈实现算法持续进化,降低对人工标注的依赖,提升数据利用效率。典型企业案例进一步验证了该路径的技术可行性与经济价值。; 适合人群:从事汽车电子、智能驾驶感知算法研发的工程师,以及关注自动驾驶技术趋势的产品经理和技术管理者;具备一定自动驾驶基础知识,希望深入了解BEV架构与数据闭环机制的专业人士。; 使用场景及目标:①理解BEV+Transformer为何成为当前感知融合的主流技术路线;②掌握数据闭环在BEV模型迭代中的关键作用及其工程实现逻辑;③为智能驾驶系统架构设计、传感器选型与算法优化提供决策参考; 阅读建议:本文侧重技术趋势分析与系统级思考,建议结合实际项目背景阅读,重点关注BEV融合逻辑与数据闭环构建方法,并可延伸研究相关企业在舱泊一体等场景的应用实践。
要在设置 `setScaleType(ImageView.ScaleType.MATRIX)` 后使图片显示效果不变,需要根据原本的 `scaleType` 来构建合适的 `Matrix` 对象。以下是针对不同常见 `scaleType` 的处理方式: #### 1. `CENTER` `CENTER` 类型会将图片居中显示,不进行缩放。代码示例如下: ```java ImageView imageView = findViewById(R.id.imageView); imageView.setScaleType(ImageView.ScaleType.CENTER); // 现在将 scaleType 改为 MATRIX imageView.setScaleType(ImageView.ScaleType.MATRIX); // 获取图片的宽高 Drawable drawable = imageView.getDrawable(); if (drawable != null) { int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int viewWidth = imageView.getWidth(); int viewHeight = imageView.getHeight(); // 计算平移量 float dx = (viewWidth - drawableWidth) / 2f; float dy = (viewHeight - drawableHeight) / 2f; // 创建 Matrix 对象 Matrix matrix = new Matrix(); matrix.postTranslate(dx, dy); // 将矩阵应用到 ImageView imageView.setImageMatrix(matrix); } ``` #### 2. `CENTER_INSIDE` `CENTER_INSIDE` 会将图片完整显示在 `ImageView` 内,可能会进行等比缩放。代码示例如下: ```java ImageView imageView = findViewById(R.id.imageView); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); // 现在将 scaleType 改为 MATRIX imageView.setScaleType(ImageView.ScaleType.MATRIX); Drawable drawable = imageView.getDrawable(); if (drawable != null) { int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int viewWidth = imageView.getWidth(); int viewHeight = imageView.getHeight(); float scale = Math.min((float) viewWidth / drawableWidth, (float) viewHeight / drawableHeight); float dx = (viewWidth - drawableWidth * scale) / 2f; float dy = (viewHeight - drawableHeight * scale) / 2f; Matrix matrix = new Matrix(); matrix.postScale(scale, scale); matrix.postTranslate(dx, dy); imageView.setImageMatrix(matrix); } ``` #### 3. `FIT_XY` `FIT_XY` 会将图片拉伸或压缩以填充整个 `ImageView`。代码示例如下: ```java ImageView imageView = findViewById(R.id.imageView); imageView.setScaleType(ImageView.ScaleType.FIT_XY); // 现在将 scaleType 改为 MATRIX imageView.setScaleType(ImageView.ScaleType.MATRIX); Drawable drawable = imageView.getDrawable(); if (drawable != null) { int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int viewWidth = imageView.getWidth(); int viewHeight = imageView.getHeight(); float scaleX = (float) viewWidth / drawableWidth; float scaleY = (float) viewHeight / drawableHeight; Matrix matrix = new Matrix(); matrix.postScale(scaleX, scaleY); imageView.setImageMatrix(matrix); } ``` 通过以上方法,可以在将 `scaleType` 改为 `MATRIX` 后,保持图片原本的显示效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值