Android中动态调整ImageView的宽高比

2024-01-16 00:58

本文主要是介绍Android中动态调整ImageView的宽高比,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原理

  • 在写好布局文件后,首先拿到整个屏幕的宽度,然后减去左右两侧的padding和margin所占的宽度,得到的结果就是ImageView应该设置的宽度
  • 再由一定比例计算出高度
  • 最后以ImageView.setLayoutParams()完成宽高的设定

布局文件 

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="22dp"><ImageViewandroid:id="@+id/image"android:layout_width="match_parent"android:layout_height="wrap_content"android:scaleType="fitXY" /></LinearLayout>

代码块

 

        ImageView image = findViewById(R.id.image);int w = DensityTool.getScressWidth(mContext);int width = w - DensityTool.dp2px(mContext, 22f) * 2;//乘以2是因为左右两侧的宽度int height = (int)(width * 0.424);//设置高度为宽度的0.424倍DensityTool.setWH(image, width, height);

DensityTool

这是我自己造的一个轮子,主要用于像素转换,非常实用的!

package com.pande.elec.pandemimi.utils;import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;public class DensityTool {//获取屏幕宽度public static int getScressWidth(Context context){WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();m.getDefaultDisplay().getMetrics(outMetrics);return outMetrics.widthPixels;}//获取屏幕高度public static int getScresHeight(Context context){WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();m.getDefaultDisplay().getMetrics(outMetrics);return outMetrics.heightPixels;}//根据手机的分辨率从 dp 的单位 转成为 px(像素)public static float dp2px(Resources resources, float dpValue) {final float scale = resources.getDisplayMetrics().density;return (dpValue * scale + 0.5f);}//根据手机的分辨率从 dp 的单位 转成为 px(像素)public static int dp2px(Context context, float dpValue) {float scale = context.getResources().getDisplayMetrics().density;return (int)(dpValue * scale + 0.5f);}//根据手机的分辨率从 px(像素) 的单位 转成为 dppublic static float px2dp(Resources resources, float pxValue) {final float scale = resources.getDisplayMetrics().density;return (pxValue / scale + 0.5f);}//获取屏幕dpipublic static int getDpi(Context context) {return context.getResources().getDisplayMetrics().densityDpi;}//动态设置view的宽高public static void setWH(View view, int width, int height){ViewGroup.LayoutParams layoutParams = view.getLayoutParams();layoutParams.width = width;layoutParams.height = height;view.setLayoutParams(layoutParams);}
}

参考于:https://blog.csdn.net/Chuck_0430/article/details/52092329?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control

这篇关于Android中动态调整ImageView的宽高比的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610798

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.