ImageView的属性

网络上下载下来的图片自适应: android:adjustViewBounds="true"(其详细解释在下面)
<ImageView android:id="@+id/dynamic_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_marginTop="5dip" android:adjustViewBounds="true" android:background="@drawable/imageview_background" />
另外,android:background="@drawable/imageview_background"是给图片加了一个边框,其中

imageview_background.xml:

<?xmlversion="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/white"/><stroke android:width="2.0dip"android:color="#99D9D9D9" /> <cornersandroid:radius="2.0dip" /> <paddingandroid:left="5.0dip" android:top="5.0dip" android:right="5.0dip"android:bottom="5.0dip" /></shape>

ImageView属性说明:

1、类概述

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

2、XML属性

属性名称

描述

android:adjustViewBounds

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

android:cropToPadding

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

ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none">

android:maxHeight

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

1)设置setAdjustViewBoundstrue

2)设置maxWidthMaxHeight

3)设置设置layout_widthlayout_heightwrap_content

android:maxWidth

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

android:scaleType

设置图片的填充方式。

matrix

0

用矩阵来绘图

fitXY

1

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

ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="120" height="30">

layout_

height

:30px


layout_

width

:120px

fitStart

2

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

ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="120" height="30">

fitCenter

3

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

ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="120" height="30">

fitEnd

4

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

ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="120" height="30">

center

5

按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="80" height="60">

layout_

height

:60px


layout_

width

:80px


padding

:10px

centerCrop

6

按比例放大原图直至等于某边View的宽高显示。ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="80" height="60">

centerInside

7

当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="80" height="60">

android:src

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

android:tint

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

ImageView图片自适应" style="margin:0px; padding:0px; border:0px; list-style:none" width="81" height="69">左边为原图,右边为设置后的效果,见后面代码。



android:scaleType是ImageView的属性

等同于Java代码的 ImageView.setScaleType(ImageView.ScaleType)

android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:   CENTER /center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示   CENTER_CROP / centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)   CENTER_INSIDE / centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽   FIT_CENTER / fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示   FIT_END / fitEnd   把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置   FIT_START / fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置   FIT_XY / fitXY  把图片不按比例扩大/缩小到View的大小显示   MATRIX / matrix 用矩阵来绘制

//获得Bitmap的高和宽  int bmpWidth=bmp.getWidth();  int bmpHeight=bmp.getHeight();    //设置缩小比例  double scale=0.8;  //计算出这次要缩小的比例  scaleWidth=(float)(scaleWidth*scale);  scaleHeight=(float)(scaleHeight*scale);    //产生resize后的Bitmap对象  Matrix matrix=new Matrix();  matrix.postScale(scaleWidth, scaleHeight);  Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);

<ImageView  android:id="@+id/image"      android:layout_width="fill_parent"       android:layout_height="fill_parent"      android:scaleType="center"      android:src="@drawable/candle"  />




更多相关文章

  1. Android手机开发 控件 TextView文字居中
  2. Android(安卓)关闭/打开多点触控 (Android中设置不能同时点击2个v
  3. Android(安卓)属性总结
  4. Android(安卓)自定义TextView去除paddingTop和paddingBottom
  5. 最简单的android底部导航栏 + Fragment的实现方式
  6. android添加广告之--有米
  7. ImageView的src和background一些我的理解。
  8. Android(安卓)属性总结
  9. android 显示系统 surfaceflinger 分析

随机推荐

  1. Google Play支付接入(in-app Billing)
  2. 快速编译出WebRTC for Android(安卓)的一
  3. android产品研发(六)-->Apk混淆
  4. 为开发者准备的最佳 Android(安卓)函数库
  5. Android(安卓)数据库的使用SQLite 和GREE
  6. Gradle解决依赖冲突
  7. 聊聊自定义View那些事
  8. Android(安卓)内存优化与泄露
  9. Android进阶(十六)子线程调用Toast报Can'
  10. Android(安卓)Studio查看aar文件内容