ImageView详解
知识点简介
ImageView在官方的介绍上说是显示任意图像,如图标。ImageView类可以从各种来源(如资源或内容提供程序)加载图像,负责从图像中计算其度量,以便可以在任何布局管理器中使用,并提供各种显示选项,如缩放和着色。
基本使用方法
- xml
xml中声明ImageView控件,然后在activity中通过findViewById()获取
<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);
}
}
- new
通过new创建出ImageView对象,传入一个context
ImageView view = new ImageView(this);
常用属性
常用属性详解
- android:adjustViewBounds
相关方法:setAdjustViewBounds(boolean)
官方解释:通过调整ImageView的界限来保持图片的宽高比例
adjustViewBounds参数默认为false,当我们需要使用它的时候将其设置为true,其scaleType自动为“fitCenter”(当scaleType与adjustViewBounds同时在xml中设置后,如果设置了scaleType,则由adjustViewBounds自动设置的scaleType将失效,因为scaleType优先级比adjustViewBounds高),并且会根据当前View的最大宽高来填充View的内容,并且需要配合上maxHeight与maxWidth一起使用才会有效,因为其需要一个ImageView的界限
<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"/>
- android:baseline
相关方法:setBaseline(int)
官方解释:视图中基线的偏移量。
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