需求:今天在做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); } }
}

Android手机图片适配问题的更多相关文章

  1. 老李分享:android手机测试之适配(1)

    Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因.重要概念.解决方案及最佳实践,我相信如果你能认真的学习 ...

  2. Android开发——Android手机屏幕适配方案总结

    )密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...

  3. 老李分享:android手机测试之适配(2)

    但 Android 版本低于 3.2 的设备不支持此技术,原因是这些设备无法将 sw600dp 识别为尺寸限定符,因此我们仍需使用 large 限定符.这样一来,就会有一个名称为 res/layout ...

  4. android 手机 多分辨率适配

    近来在做android屏幕适配这方面的工作, 今天总算有点眉目.  小记一下 基础知识就不科普了, 网上一大堆. 作为一个刚接触这方面人, 最先进入我脑子的, 是从小到大的各种屏, 小到手表, 大到街 ...

  5. Android手机图片路径

    H:\dcim\100MEDIA H:\Tencent\MobileQQ\photo H:\Tencent\MobileQQ\photo H:\Tencent\MobileQQ\thumb H:\Te ...

  6. Android 关于屏幕适配

    android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 转自:http://www.c ...

  7. 【Android】Android中不同手机分辨率适配问题

    在项目开发的过程中,同一个布局对应不同的手机会显示出不同的效果.导致这个现象产生的原因是不同手机的分辨率不同.在android sdk提供的帮助文档中,我们可以看到各种手机的分辨率和对应的屏大小.QV ...

  8. Android中的适配方式

    1,图片适配(在不同像素密度的手机上,加载不同文件夹下的图片) 一套图(800*480,将截取的图片放置在hdpi下,小图(变形不明显), 大图(根据适配的手机,做单独的截取,比如有两款手机适配(做两 ...

  9. android手机和ios手机的分辨率

    Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3 VGA     640*480 (Video Graphics Array) QVGA  320*240 (Quarter VGA ...

随机推荐

  1. openwrt设置语言的过程

    设置语言的流程一.关联的配置文件/etc/config/luci查看配置文件内容如下:root@hbg:/# cat /etc/config/luci config core 'main'       ...

  2. Just do it!!!

    4月英语竞赛期中考试 5月初级程序员考试 6月英语四级考试 7月期末考试 加油吧年轻人!

  3. EXEC 和 SP_EXECUTESQL的区别

    摘要: MSSQL为我们提供了两种动态执行sql语句的命令:EXEC 和 SP_EXECUTESQL.通常SP_EXECUTESQL更具优势,因为它提供了输入输出的接口,且能够重用执行计划,大大提高执 ...

  4. ural 1013. K-based Numbers. Version 3(动态规划)

    1013. K-based Numbers. Version 3 Let’s consider K-based numbers, containing exactly N digits. We def ...

  5. laravel性能优化

    1. 配置信息缓存 使用以下 Artisan 自带命令,把 config 文件夹里所有配置信息合并到一个文件里,减少运行时文件的载入数量: php artisan config:cache 上面命令会 ...

  6. 《JavaScript高级程序设计》读书笔记 ---操作符二

    关系操作符 小于(<).大于(>).小于等于(<=)和大于等于(>=)这几个关系操作符用于对两个值进行比较,比较的规则与我们在数学课上所学的一样.这几个操作符都返回一个布尔值, ...

  7. chrome浏览器调试工具的使用

    废话不多说,给大家介绍一下最基本的浏览器调试工具

  8. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  9. sqlDeveloper连接oracle

    1.解决oracle11g的ORA-12505问题 启动oraclehome92TNSlistener服务,启动oracleserviceXXXX,XXXX就是你的database SID. < ...

  10. 在Activity之间传递数据—传递值对象

    传递有两种方式,一种是类继承自Serializable(Java方式,速度较慢),另一种是类继承自Parcelable(Android方式) 继承自Serializable的时候,实现比较简单,类只需 ...