注意

  请查看本文后期更新完整版:

  http://www.cnblogs.com/over140/archive/2011/06/08/2075054.html

来源: 农民伯伯: http://www.cnblogs.com/over140/

正文

  一、结构

    java.lang.Object

      android.view.View

        android.widget.ImageView

    已知直接子类:

    ImageButton, QuickContactBadge

    已知间接子类:

    ZoomButton

  二、类概述

    显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

  三、XML属性

属性名称

描述

android:adjustViewBounds

是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。

android:cropToPadding

是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:

android:maxHeight

设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

1) 设置setAdjustViewBounds为true;

2) 设置maxWidth、MaxHeight;

3) 设置设置layout_width和layout_height为wrap_content。

android:maxWidth

设置View的最大宽度。同上。

android:scaleType

设置图片的填充方式。

matrix

0

用矩阵来绘图

fitXY

1

拉伸图片(不按比例)以填充View的宽高

layout_

height

:30px

layout_

width

:120px

fitStart

2

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边

fitCenter

3

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间

fitEnd

4

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边

center

5

按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示

layout_

height

:60px

layout_

width

:80px

padding

:10px

centerCrop

6

按比例放大原图直至等于某边View的宽高显示。

centerInside

7

当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。

android:src

设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)

android:tint

将图片渲染成指定的颜色。见下图:

 左边为原图,右边为设置后的效果,见后面代码。

  四、代码  

    4.1  android:tint

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:src="@drawable/btn_mode_switch_bg" >
</ImageView> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:src="@drawable/btn_mode_switch_bg"
android:tint="#ffff00" >
</ImageView>

 4.2  android:cropToPadding

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:cropToPadding="true"
android:scrollY="-10px"
android:src="@drawable/btn_mode_switch_bg" >
</ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:cropToPadding="true"
android:scrollY="10px"
android:src="@drawable/btn_mode_switch_bg" >
</ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:cropToPadding="true"
android:paddingTop="10px"
android:scrollY="10px"
android:src="@drawable/btn_mode_switch_bg" >
</ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:cropToPadding="false"
android:paddingTop="10px"
android:scrollY="10px"
android:src="@drawable/btn_mode_switch_bg" >
</ImageView>

  五、 系列

    Android2.2 API 中文文档系列(1) —— TextView

    Android2.2 API 中文文档系列(2) —— EditText

    Android2.2 API 中文文档系列(3) —— AccessibilityService

    Android2.2 API 中文文档系列(4) —— Manifest

    Android2.2 API 中文文档系列(5) —— View

结束

  本文android:tint属性比较有意思,而android:adjustViewBounds与android:cropToPadding是难点,都花了超过一个小时才试出效果。

Android2.2 API —— ImageView的更多相关文章

  1. Android2.2 API中文文档——View

    概述 java.lang.Object ↳        android.view.View 已知直接子类: AnalogClock, ImageView, KeyboardView, Progres ...

  2. Android Camera Api的心得

    (一) 前言最近看Camera的api,觉得写的真的不错.现在翻译过来,给大家分享分享,译文可能不太好,大家将就着看哈. (二) 正文1. CameraCamera是Android framework ...

  3. Android学习笔记-ImageView(图像视图)

    本节引言: 本节介绍的UI基础控件是:ImageView(图像视图),见名知意,就是用来显示图像的一个View或者说控件! 官方API:ImageView;本节讲解的内容如下: ImageView的s ...

  4. 【转】GitHub 排名前 100 的安卓、iOS项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...

  5. Android开发自学笔记(Android Studio)—4.3ImageView及其子类

    一.引言 ImageView继承自View组件,主要功能用来显示图片,实际上他能显示的不仅是图片,Drawable对象都可以用ImageView来显示. ImageView派生了ImageButton ...

  6. Android图片加载库的理解

    前言     这是“基础自测”系列的第三篇文章,以Android开发需要熟悉的20个技术点为切入点,本篇重点讲讲Android中的ImageLoader这个库的一些理解,在Android上最让人头疼是 ...

  7. GitHub Android Libraries Top 100 简介

    本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过 ...

  8. 2016年GitHub 排名前 100 的安卓、iOS项目简介(收藏)

    排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并不具备任何官方效力, 仅供参考学习, 方便初学者 ...

  9. 64.GitHub 排名前100的android项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...

随机推荐

  1. java读取properties

    Java读取properties文件的方法比较多,网上我最多的文章是“Java读取properties文件的六种方法”,我看了好多的文章,在读到“博客之星-熔岩”的“Java读取properties文 ...

  2. Android 系统名字、版本、API level的对应关系

    从官网上找到的,具体地址是: http://source.android.com/source/build-numbers.html Code name Version API level Lolli ...

  3. 通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法

    在用本地的navicat连接服务器的mysql数据库时候出现下面的问题: 解决的方法: 解决方法: 1.改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhos ...

  4. BringWindowToTop(), SetForegroundWindow(), SetActiveWindow()

    1. SetActiveWindow() 原型: <span style="font-size:14px;">CWnd* SetActiveWindow(); HWND ...

  5. cocos2d-x学习日志(13) --A星寻路算法demo

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢?如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! A星算法简介: A*搜寻算法俗称A星 ...

  6. [学习笔记]设计模式之Adapter

    写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Adapter(适配器)模式主要解决接口不匹配的问题.为此,让我们要回到最初Builder模式创建平行世界时,白雪公主和小霍比特人的谜 ...

  7. VS2010项目转化为VS2008项目

    第一步: 打开VS2010项目的SLN文件有如下代码: Microsoft Visual Studio Solution File, Format Version 11.00# Visual Stud ...

  8. Orcle数据库恢复

    不知道什么原因,服务器上的数据库报错:ORA-01033:ORACLE initialization or shutdown in progress 首先检查:监听文件的主机名及端口号是否更改 数据文 ...

  9. 8个开发必备的PHP功能(转)

    又是好几天没写博客,今天看到了个不错的文章,就转载到自己的博客,好以后查询方便. 1.传递任意数量的函数参数 我们在.NET或者JAVA编程中,一般函数参数个数都是固定的,但是PHP允许你使用任意个数 ...

  10. javascript 之原型理解

    最近一直在了解javascript原型的问题,也算是理解了一点,希望把我所理解的,用简单的例子和说明,让更多人清除的去理解javascript原型 1,原型 prototype 是一个什么东西 我们创 ...