android 等比控件,Android打造可按宽高比例缩放的ImageView

一、概述

图片多的页面,经常会听见UI说,你写的UI变形了。图片拉伸、变形也是开发有时会遇到的问题。

二、先看一下缩放的效果图

698c01cfcca1

效果图.jpg

测试图片的宽高尺寸:840 * 420,也就是 2:1,宽是高的2倍。

三、缩放的需求分析

1. 按宽或者高来缩放

2. 缩放的倍数或者说比例

四、自定义属性

五、控件的宽高是在onMeasure指定的,先分析一下有多少种情况

1. 明确的指定了宽高

2. 按宽度来缩放

3. 按高度缩放

/**

* 测量控件的代码

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// 不设置任何属性

if (mScaleType == 0 || mScaleRadio == 0) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

return;

}

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

// 如果子类设置了精确的宽高

if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY

&& (widthSize != 0 && heightSize != 0)) {

setMeasuredDimension(widthSize, heightSize);

return;

}

// 如果是按宽度来缩放

if (mScaleType == WIDTH && widthMode == MeasureSpec.EXACT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个要求,你可以自定义一个继承自 ImageView 的类,并在其中重写 onMeasure 方法来控制 ImageView 的最大长度和度。同时,在加载图片时,你可以根据图片的比例来计算缩放比例,然后使用 Matrix 对图片进行缩放操作。 下面是一个示例代码: ```java public class CustomImageView extends ImageView { private int mMaxWidth; // 最大度 private int mMaxHeight; // 最大度 public CustomImageView(Context context) { super(context); init(); } public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { // 设置默认的最大度和度 mMaxWidth = Integer.MAX_VALUE; mMaxHeight = Integer.MAX_VALUE; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 获取 ImageView 的测量模式和尺寸 int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 计算根据最大度和度得到的度和度 int maxWidth = Math.min(widthSize, mMaxWidth); int maxHeight = Math.min(heightSize, mMaxHeight); int scaledWidth = widthSize; int scaledHeight = heightSize; // 根据图片的比例计算缩放比例 Drawable drawable = getDrawable(); if (drawable != null) { int imageWidth = drawable.getIntrinsicWidth(); int imageHeight = drawable.getIntrinsicHeight(); float scale = Math.min((float) maxWidth / imageWidth, (float) maxHeight / imageHeight); scaledWidth = (int) (imageWidth * scale); scaledHeight = (int) (imageHeight * scale); } // 根据测量模式设置最终的度和度 int finalWidth = (widthMode == MeasureSpec.EXACTLY) ? widthSize : scaledWidth; int finalHeight = (heightMode == MeasureSpec.EXACTLY) ? heightSize : scaledHeight; // 设置最终的度和度 setMeasuredDimension(finalWidth, finalHeight); } public void setMaxSize(int maxWidth, int maxHeight) { mMaxWidth = maxWidth; mMaxHeight = maxHeight; } } ``` 你可以在布局文件中使用这个自定义的 ImageView,然后通过调用 `setMaxSize()` 方法来设置最大度和度。当加载图片时,ImageView 会按照比例缩放图片来适应最大的度和度限制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值