Android手机图片适配问题

今日长缨在手,何时缚住苍龙。这篇文章主要讲述Android手机图片适配问题相关的知识,希望能为你提供帮助。
需求:今天在做ListView的时候遇到一个问题,就是ListView中加载图片的时候。有些图片的大小比较大,所以会出现图片显示不充分的问题。
首先,再不做任何处理的情况下,大小是这样的。宽度是WrapContent。

Android手机图片适配问题

文章图片

 
 
那么怎么解决呢??
1、首先FIX_XY,但是这样会引起失真。
Android手机图片适配问题

文章图片

【Android手机图片适配问题】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; /** * @authorAdministrator * @time2015-7-18 下午2:10:54 * @desTODO * * @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 floatmPicRatio= 0f; // 图片的宽高比 2.43 private static final intRELATIVE_WIDTH= 0; // 控件宽度固定,已知图片的宽高比,求控件的高度 private static final intRELATIVE_HEIGHT= 1; // 控件高度固定,已知图片的宽高比,求控件的宽度 private intmRelative= 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); }} }

 

    推荐阅读