首页 > 其他分享 >Android开发-Android常用组件-ImageView图像视图

Android开发-Android常用组件-ImageView图像视图

时间:2023-03-29 16:33:10浏览次数:42  
标签:src layout 视图 background ImageView Android android scaleType

4.4  ImageView(图像视图)

ImageView见名知意,就是用来显示图像的一个View或者说控件

 

需掌握的知识点:

  1. ImageView的src属性和blackground的区别;
  2. adjustViewBounds设置图像缩放时是否按长宽比
  3. scaleType设置缩放类型
  4. 最简单的绘制圆形的ImageView
  • src属性和background属性的区别

    在API文档中我们发现ImageView有两个可以设置图片的属性,分别是:src和background

    常识:

    ① background通常指的都是背景,而src指的是内容!!

    ②  当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸,而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

案例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linerLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher_round"/>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher_round"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>

</LinearLayout>

 

 

  •  Java代码中设置background和src属性:

    前景(对应src属性):setImageDrawable();

    背景(对应background属性):setBackgroundDrawable();

  • 两者结合妙用:

 

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:padding="20dp"
        android:background="@mipmap/ic_launcher_round"  //背景
        android:src="@mipmap/wode"/>    //前景

 

 

  •  scaleType属性android:scaleType

android:scaleType用于设置显示的图片如何缩放或者移动以适应ImageView的大小。Java代码中可以通过imageView.setScaleType(ImageView.ScaleType.CENTER);来设置。可选值如下:

fitXY

对图像的横向与纵向进行独立缩放,使得该图片完全适应ImageView,但是图片的横纵比可能会发生改变

fitStart

保持纵横比缩放图片,直到较长的边与Image的编程相等,缩放完成后将图片放在ImageView的左上角

fitCenter

同上,缩放后放于中间

fitEnd

同上,缩放后放于右下角

center

保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理

centerCrop

保持横纵比缩放图片,直到完全覆盖ImageView,可能会出现图片的显示不完全

centerInside

保持横纵比缩放图片,直到ImageView能够完全地显示图片

matrix

默认值,不改变原图的大小,从ImageView的左上角开始绘制原图, 原图超过ImageView的部分作裁剪处理

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--fitXY:对图像的横向与纵向进行独立缩放,使得该图片完全适应ImageView,但是图片的横纵比可能会发生改变-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher_round"/>
<!--fitStart:保持纵横比缩放图片,直到较长的边与Image的编程相等,缩放完成后将图片放在ImageView的左上角-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="fitStart"
android:src="@mipmap/ic_launcher_round"/>
<!--fitCenter:同上,缩放后放于中间-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher_round"/>
<!--fitEnd:同上,缩放后放于右下角-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="fitEnd"
android:src="@mipmap/ic_launcher_round"/>
<!--center:保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="center"
android:src="@mipmap/ic_launcher_round"/>
<!--centerCrop:保持横纵比缩放图片,直到完全覆盖ImageView,可能会出现图片的显示不完全-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher_round"/>
<!--centerInside:保持横纵比缩放图片,直到ImageView能够完全地显示图片-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher_round"/>
<!--matrix:默认值,不改变原图的大小,从ImageView的左上角开始绘制原图, 原图超过ImageView的部分作裁剪处理-->
<ImageView
android:background="@color/lawn_green"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:scaleType="matrix"
android:src="@mipmap/ic_launcher_round"/>
</LinearLayout>

 

 

   

标签:src,layout,视图,background,ImageView,Android,android,scaleType
From: https://www.cnblogs.com/lysboke/p/17269437.html

相关文章

  • 又是Android studio下layout文件内容无提示
     又出现没有提示的问题。只不过这次我新建了一个模块。想想可能是模块的问题    之前是32,先在新建的是33.更改回32即可。 ......
  • Android https忽略证书信任问题
    【第一部分,忽略证书信任问题】直接去第二部分性能问题搬运自:https://blog.csdn.net/lizeyang/article/details/18983843java程序在访问https资源时,出现报错sun.security......
  • 视图创建、查询
                  ......
  • Android Studio 学习-第三章 Activity 第二组
    事先申明:所有android类型的学习记录全部基于《第一行代码Android》第三版,在此感谢郭霖老师的书籍帮助。1.注册activity在第一组中,我创建了一个activity,并且编辑布局后......
  • 京东金融Android瘦身探索与实践
    作者:京东科技冯建华一、背景随着业务不断迭代更新,App的大小也在快速增加,2019年~2022年期间一度超过了117M,期间我们也做了部分优化如图1红色部分所示,但在做优化的同时面临着......
  • Android系统服务DropBoxManagerService详解与实践应用
    作者:vivo互联网客户端团队-MaLian借助系统DropBoxManagerService对于系统文件目录dropbox管理的设计,了解其文件管理的规则、运行机制、读写机制、管控机制,根据其设计一个......
  • 【Android 逆向】apk反编译后重打包
    1.执行apktoolbsmali_dirsmali_dir为反编译出来的数据目录执行后可能会报错I:Buildingresources...W:/root/Desktop/tmp/qimao_dir/qimao_v5.4/AndroidManifes......
  • Android开发 触控事件分发_2_应用层的触控事件分发
    前言在上一篇博客讲解了系统层的触控通道注册,此篇博客将讲解应用层的触控事件分发分发概况流程通过下面的简单流程图大概了解,但是实际使用的时候分发并不是一个下面......
  • 【Android逆向】apk 反编译
    1.Kali搭建apktool环境1.访问apktool官网https://ibotpeaches.github.io/Apktool/install/参考红圈里的步骤处理即可2.执行命令反编译apkapktoold./xxxx_v5.4......
  • 【玖哥乱弹】三句半:Android程序员的一生
    两包泡面一天饱,代码提交天欲晓,领导上班问声早,通宵了;满脸皱纹面相老,神经紊乱血压高,浑身无力头发少,累病了;项目上线钱景好,老板只恨腰包小,卸磨杀驴把你炒,被裁了;房东敲门债主找,朋......