需求:今天在做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. C语言 - 大小端问题

    目前使用的机器都是使用字节BYTE来存储的. 对于跨越多字节的对象,必须搞清楚两个规则: 这个对象的地址是什么 在存储器中如何按照这些字节的存放的书序 对于一个整型对象 a=0x12345678,一共 ...

  2. WinHex V18.7(16进制编辑器) 多国语言绿色版

    软件名称: WinHex V18.7(16进制编辑器)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.7MB图片预览: ...

  3. 【code vs】 2780 ZZWYYQWZHZ

    2780 ZZWYYQWZHZ 题目描述 Description 可爱的小管在玩吹泡泡.忽然,他想到了一种排序....... 输入描述 Input Description 第一行输入n,表示有n个数. ...

  4. 34.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求printCapitalLetter()方法 实现输出大写英文字母表的功能,

    //接口InterfaceA package jieKou; public interface IInterfaceA { void printCapitalLetter(); } //接口Inter ...

  5. Android 图片显示

    一.Android手机显示图片 若R.G.B每种颜色使用一个字节(8bit)表示,每幅图像可以有1670万种颜色:若R.G.B每种颜色使用两个字节(16bit)表示,每幅图像可以有10的12次方种颜色 ...

  6. CentOS的KVM实践(虚拟机创建、网桥配置、Spice)

    最近公司准备上一套基于openstack的虚拟桌面系统,作为该项目的负责人,觉得有必要自己实践一下,该系统的搭建.最基础的就是需要了解基于linux的kvm的实践. 一.基础软件包准备 系统是采用px ...

  7. crontab定时任务以及其中中文乱码问题

    一.小例子 1.写个测试文件 2.将文件权限变为可执行文件 3.在crontab文件中写定时任务 格式: 分/时 * * * 用户名 可执行文件路径 >> log文件路径 2>&am ...

  8. 《TCP/IP详解》读书笔记

    本书以UNIX为背景,紧贴实际介绍了数据链层.网络层.运输层   一.整体概念   1.各层协议的关系,只讨论四层 各层常见的协议:   网络层协议:IP协议.ICMP协议.ARP协议.RARP协议. ...

  9. leetcode387

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  10. Android实现Excel表格,且表格能左右、上下滑动

    1.自定义实现一个水平滚动控件HorizontalScrollView import android.content.Context; import android.util.AttributeSet ...