ImageView在官方的介绍上说是显示任意图像,如图标。ImageView类能够从各类来源(如资源或内容提供程序)加载图像,负责从图像中计算其度量,以即可以在任何布局管理器中使用,并提供各类显示选项,如缩放和着色。html
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/icon_check"/>
复制代码
public class MainActivity extends RxAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.image);
}
}
复制代码
ImageView view = new ImageView(this);
复制代码
相关方法:setAdjustViewBounds(boolean) 官方解释:经过调整ImageView的界限来保持图片的宽高比例android
adjustViewBounds参数默认为false,当咱们须要使用它的时候将其设置为true,其scaleType自动为“fitCenter”(当scaleType与adjustViewBounds同时在xml中设置后,若是设置了scaleType,则由adjustViewBounds自动设置的scaleType将失效,由于scaleType优先级比adjustViewBounds高),而且会根据当前View的最大宽高来填充View的内容,而且须要配合上maxHeight与maxWidth一块儿使用才会有效,由于其须要一个ImageView的界限bash
<ImageView
android:id="@+id/image"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/icon_check"/>
复制代码
相关方法:setBaseline(int) 官方解释:视图中基线的偏移量。ide
baseline默认为-1,此时基线处于ImageView的顶部,当经过setBaseLine设置偏移量为正数时表明视图的基线向下偏移,为负数时向上偏移,下面咱们能够经过代码与图片的结合清楚的了解那条基线的位置(当baseline与baselineAlignBottom同时存在一个视图中时,基线以设置了baselineAlignBottom为主)布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:text="你好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:baseline="50dp"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:text="你好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:baseline="-50dp"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:text="你好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
复制代码
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="你好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:baselineAlignBottom="true"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
复制代码
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="你好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:paddingLeft="-20dp"
android:cropToPadding="true"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
复制代码
图片居中展现,若是图片大与控件大小,则以中心为基准展现ImageView控件大小,图片多出的部分裁剪不展现,若是图片小于控件大小,则所有展现,其他地方留白ui
图片进行等比的缩放或拉伸,直到有宽或高有一方可以等于控件的宽高,多余的不展现this
将图片进行等比缩放,完整的展现在ImageView上,没有铺张到的地方显示背景色留白spa
默认模式,图片将进行等比缩放或放大,完整的展现在ImageView上,而且没有铺张到的地方显示背景色。这里与CENTER_INSIDE有点相似,区别在于当同时都是小图片的时候,FIT_CENTER会在小图片的时候将图片拉伸至控件大小,而CENTER_INSIDE则只会居中显示,不拉伸翻译
图片进行等比缩放或放大,完整的展现在ImageView上,这一点和FIT_CENTER类似,不一样点在于FIT_START是以左上为基准,当宽完整平铺展现而且高会有留白后,图片将在控件的上方,下方留白,若是高平铺展现,宽有留白时,则右边留白3d
图片进行等比缩放或放大,完整展现在ImageView上,与FIT_START效果相反
图片进行缩放或放大(非等比),显示在ImageView上,这里不是等比缩放或放大,会将图片完整的展现在ImageView上,通常来讲图片宽高比和控件宽高比不一致的状况下,图片会呈现扭曲感
指定一种矩阵,由于其余七种都是内部已经写好了矩阵,这一种为本身指定一种矩阵,配合setImageMatrix()方法使用 快速记忆:其中CENTER,CENTER_CROP,CENTER_INSIDE共性:居中显示,FIT_CENTER,FIT_END,FIT_START,FIT_XY共性:对图片进行缩放,MATRIX指定矩阵
ImageViewButton是一个相似Button的图像按钮
<ImageButton
android:id="@+id/imageview"
android:src="@drawable/bottom_line_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
复制代码
bottom_line_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
复制代码
用于显示具备标准QuickContact徽章和单击行为的图像的小部件 该方法如今用的很少了,参考文章:QuickContactBadge
能够染色的ImageView,实现了TintableBackgroundView与TintableImageSourceView两个接口,用做对背景颜色进行动态改变,该ImageView在5.0如下的系统容易出现问题,较难排查,不建议使用
注:在高版本中还有一些其余的子类,可是不常见,因此没有例举
ImageView的ScaleType原理及效果分析 :www.jianshu.com/p/fe5d2e3fe… ImageView的ScaleType详解 :www.cnblogs.com/pandapan/p/… Android中ImageView的ColorFilter图像处理解析 :www.jianshu.com/p/bbc77334b… 关于圆角ImageView的几种实现方式 :www.jianshu.com/p/626dbd932…