美文网首页Android开发经验谈Android技术知识
Android ImageView宽度填满屏幕,高度自适应

Android ImageView宽度填满屏幕,高度自适应

作者: 蓝库知识 | 来源:发表于2019-10-28 19:12 被阅读0次

    搞明白这个花的时间有点多,最后将代码copy出来运行断点后才知道原来是这样子的

    方法一:
    img.setAdjustViewBounds(true);
    

    解析:

    @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
           ····
    位置一
                if (mAdjustViewBounds) {
                    resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
                    resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
    
                    desiredAspect = (float) w / (float) h;
                }
            }
    
           ·····
    位置二
            if (resizeWidth || resizeHeight) {
                /* If we get here, it means we want to resize to match the
                    drawables aspect ratio, and we have the freedom to change at
                    least one dimension.
                */
    
                // Get the max possible width given our constraints
                widthSize = resolveAdjustedSize(w + pleft + pright, mMaxWidth, widthMeasureSpec);
    
                // Get the max possible height given our constraints
                heightSize = resolveAdjustedSize(h + ptop + pbottom, mMaxHeight, heightMeasureSpec);
    
                if (desiredAspect != 0.0f) {
                    // See what our actual aspect ratio is
                    final float actualAspect = (float)(widthSize - pleft - pright) /
                                            (heightSize - ptop - pbottom);
    
                    if (Math.abs(actualAspect - desiredAspect) > 0.0000001) {
    
                        boolean done = false;
    
                        // Try adjusting width to be proportional to height
                        if (resizeWidth) {
                            int newWidth = (int)(desiredAspect * (heightSize - ptop - pbottom)) +
                                    pleft + pright;
    
                            // Allow the width to outgrow its original estimate if height is fixed.
                            if (!resizeHeight && !sCompatAdjustViewBounds) {
                                widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec);
                            }
    
                            if (newWidth <= widthSize) {
                                widthSize = newWidth;
                                done = true;
                            }
                        }
    
                        // Try adjusting height to be proportional to width
                        if (!done && resizeHeight) {
                            int newHeight = (int)((widthSize - pleft - pright) / desiredAspect) +
                                    ptop + pbottom;
    
                            // Allow the height to outgrow its original estimate if width is fixed.
                            if (!resizeWidth && !sCompatAdjustViewBounds) {
                                heightSize = resolveAdjustedSize(newHeight, mMaxHeight,
                                        heightMeasureSpec);
                            }
    
                            if (newHeight <= heightSize) {
                                heightSize = newHeight;
                            }
                        }
                    }
                }
            } else {
                /* We are either don't want to preserve the drawables aspect ratio,
                   or we are not allowed to change view dimensions. Just measure in
                   the normal way.
                */
                w += pleft + pright;
                h += ptop + pbottom;
    
                w = Math.max(w, getSuggestedMinimumWidth());
                h = Math.max(h, getSuggestedMinimumHeight());
    
                widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
                heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
            }
    
            setMeasuredDimension(widthSize, heightSize);
        }
    

    1.上面那一行解决问题的代码的含义是改变mAdjustViewBounds的值
    2.根据代码二的位置可以看出,imageview的测量其实分了两种情况,默认情况是在一定空间中按比例显示最小的尺寸(else中的代码),其次是按比例放大或缩小(为wrap的情况赋值),mAdjustViewBounds则控制这两种情况。
    mAdjustViewBounds默认为false,这里我们需要改为true,来满足宽度充满全屏,高度按比例确定

    方法二:

    自定义Image
    参考:
    ImageView宽度填满屏幕,高度自适应

    后记

    推荐:Android获取View的宽度和高度
    如有问题,欢迎批评指正

    喵印~~~

    相关文章

      网友评论

        本文标题:Android ImageView宽度填满屏幕,高度自适应

        本文链接:https://www.haomeiwen.com/subject/kszbvctx.html