zoukankan      html  css  js  c++  java
  • Android手机图片适配问题

    需求:今天在做ListView的时候遇到一个问题,就是ListView中加载图片的时候。有些图片的大小比较大,所以会出现图片显示不充分的问题。

    首先,再不做任何处理的情况下,大小是这样的。宽度是WrapContent。

    那么怎么解决呢??

    1、首先FIX_XY,但是这样会引起失真。

    2、于是需要换个解决方案,那就是自定义View,重写onMeasure方法。

    自定义一个属性:长宽高比。通过自己重写onMeasure方法来解决。

    具体解决代码如下:

    package com.itheima.googleplay_8.views;
    
    import com.itheima.googleplay_8.R;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.widget.FrameLayout;
    
    /**
     * @author  Administrator
     * @time     2015-7-18 下午2:10:54
     * @des    TODO
     *
     * @version $Rev: 33 $
     * @updateAuthor $Author: admin $
     * @updateDate $Date: 2015-07-18 15:13:26 +0800 (星期六, 18 七月 2015) $
     * @updateDes TODO
     */
    public class RatioLayout extends FrameLayout {
        private float                mPicRatio        = 0f;                // 图片的宽高比 2.43
        private static final int    RELATIVE_WIDTH    = 0;                // 控件宽度固定,已知图片的宽高比,求控件的高度
        private static final int    RELATIVE_HEIGHT    = 1;                // 控件高度固定,已知图片的宽高比,求控件的宽度
        private int                    mRelative        = RELATIVE_WIDTH;
    
        public RatioLayout(Context context) {
            this(context, null);
        }
    
        public RatioLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);
    
            mPicRatio = typedArray.getFloat(R.styleable.RatioLayout_picRatio, 0);
    
            mRelative = typedArray.getInt(R.styleable.RatioLayout_relative, RELATIVE_WIDTH);
    
            typedArray.recycle();
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            // 控件宽度固定,已知图片的宽高比,求控件的高度
            int parentWidthMode = MeasureSpec.getMode(widthMeasureSpec);
    
            // 控件高度固定,已知图片的宽高比,求控件的宽度
            int parentHeightMode = MeasureSpec.getMode(heightMeasureSpec);
    
            if (parentWidthMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_WIDTH) {// 控件宽度固定,已知图片的宽高比,求控件的高度
                // 得到父容器的宽度
                int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
                // 得到孩子的宽度
                int childWidth = parentWidth - getPaddingLeft() - getPaddingRight();
                // 控件的宽度/控件的高度 = mPicRatio;
    
                // 计算孩子的高度
                int childHeight = (int) (childWidth / mPicRatio + .5f);
    
                // 计算父容器的高度
                int parentHeight = childHeight + getPaddingBottom() + getPaddingTop();
    
                // 主动测绘孩子.固定孩子的大小
                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
                int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
                measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);
    
                // 设置自己的测试结果
                setMeasuredDimension(parentWidth, parentHeight);
    
            } else if (parentHeightMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_HEIGHT) {
                // 控件高度固定,已知图片的宽高比,求控件的宽度
                // 得到父亲的高度
                int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    
                // 得到孩子的高度
                int childHeight = parentHeight - getPaddingBottom() - getPaddingTop();
    
                // 控件的宽度/控件的高度 = mPicRatio;
                // 计算控件宽度
                int childWidth = (int) (childHeight * mPicRatio + .5f);
    
                // 得到父亲的宽度
                int parentWidth = childWidth + getPaddingRight() + getPaddingLeft();
    
                // 主动测绘孩子.固定孩子的大小
                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
                int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
                measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);
    
                // 设置自己的测试结果
                setMeasuredDimension(parentWidth, parentHeight);
    
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            }
    
        }
    }
  • 相关阅读:
    实战:当jquery遇上了json
    验证文本域字符个数的正则表达式
    分布式缓存方案:Memcached初探
    Asp.Net Forms验证(自定义、角色提供程序、单点登录) [转]
    C#3.0扩展方法[转]
    HttpWebRequest调用web服务的代码
    解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题[转]
    微软Asp.net Ajax 1.0的AutoComplete控件的几处修正和增强 [转]
    LINQ体验(5)——LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg(转)
    c# Linq 的分页[转]
  • 原文地址:https://www.cnblogs.com/tinyclear/p/6099881.html
Copyright © 2011-2022 走看看