ImageView详解

本文详细介绍了Android中的ImageView组件,包括其基本使用、常用属性如adjustViewBounds、baseline、cropToPadding、maxHeight和maxWidth、scaleType等的详细解释和应用场景,以及子类ImageButton、QuickContactBadge和AppCompatImageView的特点。同时提供了相关文章链接,帮助深入理解ImageView的ScaleType和ColorFilter图像处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识点简介

ImageView在官方的介绍上说是显示任意图像,如图标。ImageView类可以从各种来源(如资源或内容提供程序)加载图像,负责从图像中计算其度量,以便可以在任何布局管理器中使用,并提供各种显示选项,如缩放和着色。

基本使用方法

  1. 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);
    }

}
  1. 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为主)

  1. 设置偏移量为正数
<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
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值