ImageView的学习和使用

ImageView

常用属性
scaleType

fitXY:撑满控件,宽高比例可能会发生改变

fitCenter:保持宽高比缩放,直到能够完全显示

centerCrop:保持宽高比缩放,直至完全覆盖控件,裁剪显示

使用scaleType的三种不同方式引入图片
<!--
src 引入图片
-->
<ImageView
    android:id="@+id/iv_1"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="#FF9900"
    android:scaleType="fitXY"
    android:src="@drawable/bg_iron_hua" />

<ImageView
    android:id="@+id/iv_2"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_below="@id/iv_1"
    android:layout_marginTop="10dp"
    android:background="#FF9900"
    android:scaleType="fitCenter"
    android:src="@drawable/bg_iron_hua" />

<ImageView
    android:id="@+id/iv_3"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_below="@id/iv_2"
    android:layout_marginTop="10dp"
    android:background="#FF9900"
    android:scaleType="centerCrop"
    android:src="@drawable/bg_iron_hua" />
效果展示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8uNBXlNQ-1664640867789)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20221001235059675.png)].

加载网络图片

使用第三方开源库加载网络图片,在build.gradle中的dependencies中添加第三方开源库

dependencies {
    //这两行是引入的
    implementation 'com.github.bumptech.glide:glide:4.14.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.14.1'
    
    //下面这是项目初始化自带的
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

因为我们要加载网络当中的图片,那么就要给我们这个项目开通网络的权限,开通网络权限的方法是在AndroidManifest.xml文件中进行下面这行代码的添加

    <!--
    给当前项目开通网络权限
    -->
    <uses-permission android:name="android.permission.INTERNET"/>

然后在相关的view.xml中设置ImageView并且在项目的Activity当中编写加载网络图片的代码

<ImageView
    android:id="@+id/iv_4"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_below="@id/iv_3"
    android:layout_marginTop="10dp"
    android:background="#FF9900"
    android:scaleType="centerCrop" />
/**
 * 声明控件
 */
private ImageView mIv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_view);
    //找到控件
    mIv4 = findViewById(R.id.iv_4);
    Glide.with(this).load("https://www.baidu.com/img/pc_d421b05ca87d7884081659a6e6bdfaaa.png").into(mIv4);
}
最终的效果展示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z1Tedihe-1664640867791)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20221001234926389.png)].

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值