ImageView继承自View类,它的功能用于显示图片, 或者显示Drawable对象

xml属性:

src和background区别 参考:http://hi.baidu.com/sunboy_2050/item/843aaed77256fc1620e250f9

区别一:图片透明度

两者都可以设置ImageView的背景

android:src:在设置ImageView的setAlpha()时有效果

android:background:在设置ImageView的setAlpha()时无效果

区别二:图片拉伸

background会根据ImageView的长宽进行拉伸,按照组件的大小来放大或者缩小图片。

src就存放的是原图的大小,不会进行拉伸,原图显示,不该变图片的大小

例如:ImageView的长宽都设置为:100px

1  background 引用图片

2  src 引用图片

其他属性参考文章:http://407827531.iteye.com/blog/1117199

属性名称

描述

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

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

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

下面给出一个实例代码: 实现图片透明度变化,以及缩放

public class ImageViewTest extends Activity
{
//定义一个访问图片的数组
int[] images = new int[]{
R.drawable.lijiang,
R.drawable.qiao,
R.drawable.shuangta,
R.drawable.shui,
R.drawable.xiangbi,
};
//定义默认显示的图片
int currentImg = ;
//定义图片的初始透明度
private int alpha = ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button plus = (Button)findViewById(R.id.plus);
final Button minus = (Button)findViewById(R.id.minus);
final ImageView image1 = (ImageView)findViewById(R.id.image1);
final ImageView image2 = (ImageView)findViewById(R.id.image2);
final Button next = (Button)findViewById(R.id.next);
//定义查看下一张图片的监听器
next.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (currentImg >= )
{
currentImg = -;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
.getDrawable();
//如果图片还未回收,先强制回收该图片,避免OutOfMemoryError异常
if (!bitmapDrawable.getBitmap().isRecycled())
{
bitmapDrawable.getBitmap().recycle();
}
//改变ImageView显示的图片
image1.setImageBitmap(BitmapFactory.decodeResource(getResources()
, images[++currentImg]));
}
});
//定义改变图片透明度的方法
OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(v == plus)
{
alpha += ;
}
if(v == minus)
{
alpha -= ;
}
if(alpha >= )
{
alpha = ;
}
if(alpha <= )
{
alpha = ;
}
//改变图片的透明度
image1.setAlpha(alpha);
}
};
//为两个按钮添加监听器
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
//当点击image1图片某个部分,他就会显示在image2中
image1.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent event)
{
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
.getDrawable();
//获取第一个图片显示框中的位图
Bitmap bitmap = bitmapDrawable.getBitmap();
//bitmap图片实际大小与第一个ImageView的缩放比例
double scale = bitmap.getWidth() / 320.0;
//获取需要显示的图片的开始点
int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);
if (x + > bitmap.getWidth())
{
x = bitmap.getWidth() - ;
}
if (y + > bitmap.getHeight())
{
y = bitmap.getHeight() - ;
}
//显示图片的指定区域
image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, , ));
image2.setAlpha(alpha);
return false;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" > <Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大透明度" /> <Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="降低透明度" /> <Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout>
<!-- 定义显示图片整体的ImageView --> <ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="240px"
android:background="#0000ff"
android:scaleType="fitCenter"
android:src="@drawable/shuangta" />
<!-- 定义显示图片局部细节的ImageView --> <ImageView
android:id="@+id/image2"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="10dp"
android:background="#0000ff" /> </LinearLayout>

ImageView的功能和使用的更多相关文章

  1. Android(java)学习笔记204:自定义SmartImageView(继承自ImageView,扩展功能为自动获取网络路径图片)

    1.有时候Android系统配置的UI控件,不能满足我们的需求,Android开发做到了一定程度,多少都会用到自定义控件,一方面是更加灵活,另一方面在大数据量的情况下自定义控件的效率比写布局文件更高. ...

  2. Android(java)学习笔记147:自定义SmartImageView(继承自ImageView,扩展功能为自动获取网络路径图片)

    1. 有时候Android系统配置的UI控件,不能满足我们的需求,Android开发做到了一定程度,多少都会用到自定义控件,一方面是更加灵活,另一方面在大数据量的情况下自定义控件的效率比写布局文件更高 ...

  3. 图片切换器(ImageSwitcher)的功能与用法

    ImageSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果.ImageSwitcher继承了ViewSwitcher ...

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

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

  5. Android开发学习清单

    目录: 第1章 Android应用与开发环境1.1 Android的发展和历史1.1.1 Android的发展和简介1.1.2 Android平台架构及特性1.2 搭建Android开发环境1.2.1 ...

  6. Android_Gallery

    xml布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  7. android ListView优化

    android ListView通过优化重用历史缓存实现.listview相应的数据适配器一般使用自己定义BaseAdapter子类,重用历史缓冲区来提高性能. 例如,下面的示例代码演示: 1.lis ...

  8. android学习Gallery和ImageSwitch的使用

    Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了, * @deprec ...

  9. 二、Android应用的界面编程(七)ViewAnimator及其子类[ ViewSwitcher、ImageSwitcher、TextSwitcher、ViewFlipper ]

    ViewAnimator是一个基类,它继承了FrameLayout.因此它表现出FrameLayout的特征,可以将多个View组“叠”在一起. ViewAnimator可以在View切换时表现出动画 ...

随机推荐

  1. Java——标准异常

    Throwable这个java类被用来表示任何可以作为异常被抛出的类,Throwable可以分为两种类型,Error用来表示编译时和系统错误,Exception是可以被抛出的基本类型. 1.Runti ...

  2. (六)c#Winform自定义控件-单选框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. spring cloud 断路器 Hystrix

    一.微服务架构中使用断路器的原因 二.代码实现 1.在Ribbon中使用短路器 1.1.引入依赖 <dependency> <groupId>org.springframewo ...

  4. python学习之路(2)---字符编码

    二进制编码 bin(300)    python计算二进制编码,十进制转2进制 一个二进制位就是1bit 1bit代表了8个字节,00001111 1bit  = 1bytes   缩写1b 1kb ...

  5. word编辑visio文件

    Word文档中插入visio文件并编辑: (1)插入->对象->对象->选择“visio文件”,此种方式可插入visio文件的全部. (2)直接打开visio文件->ctrl+ ...

  6. html利用j获取局域网内的本机IP(根据客户端浏览器的ip获取)

    根据客户端浏览器的ip获取 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type ...

  7. 线上调试工具 jvm-sandbox使用

    jvm-sandbox使用 1 快速安装 1.1 下载解压 # 下载最新版本的JVM-SANDBOX wget http://ompc.oss-cn-hangzhou.aliyuncs.com/jvm ...

  8. oauth2.0授权详解

    学习oauth认证之前先回顾一下通过sessionid的会话过程 关于session与cookie的请戳:https://www.cnblogs.com/moran1992/p/10793748.ht ...

  9. 力导向图(关系图) echarts的运用

    <template> <div class="demo"> <div id="grap" class="grap&quo ...

  10. 快应用 吸顶 bug

    官网说, 加载页面时,所有元素的appear事件都会被触发一次.因此,需要过滤第一次的appear事件:  但是,即使设置了过滤,也无效 于是,我把show属性改成了if属性,问题就解决 如下图: