Android开发入门组件(六)——ImageView

今天要写的是ImageView的使用,先上成果图,大概大家就懂了一半要做什么效果了

选了篮球之神乔大爷的照片,顺便致敬一波乔丹。

1.ImageView的基本用法(重点是缩放类型android:scaleType)

(1)android:scaleType="fitXY" 撑满控件,宽高比可能发生改变

<ImageView
        android:id="@+id/iv1"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:background="#FF9900"
        android:src="@drawable/jordan"
//图片资源的地址,需要事先复制到项目的文件夹下面,然后进行引用即可
        android:layout_marginBottom="15dp"
        android:scaleType="fitXY"/> //缩放类型

(2)android:scaleType="fitCenter":保持宽高比缩放,直至能够完全显示

<ImageView
        android:id="@+id/iv2"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:background="#FF9900"
        android:src="@drawable/jordan"
        android:layout_marginBottom="15dp"
        android:scaleType="fitCenter"/>

(3)android:scaleType="centerCrop":保持宽高比缩放,直至完全覆盖控件,裁剪显示

<ImageView
        android:id="@+id/iv3"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:background="#FF9900"
        android:src="@drawable/jordan"
        android:layout_marginBottom="25dp"
        android:scaleType="centerCrop"/>

 

2.加载网络图片

日常为了让项目的内存占用小一点,通常会使用网络图片进行显示。

比如微信小程序就是限制小程序项目包的大小,所以你不得不用网络图片的资源。

使用网络图片资源的步骤如下

(1)使用三方下载,导入并下载(Glide的使用)

附一下GitHub上的Glide地址:Glide介绍以及下载地址

进入之后看具体的文档说明(纯英文滴)

 (1)在build.gradle文件中写入引用,具体它会帮你办好

具体加入以下代码到文件中即可

repositories {
    mavenCentral()
}
dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

(2)然后点击按钮进行Gradle文件的更新(右上角大象一个斜向下的按钮点击即可)

(3)然后在Activity中下载图片网络资源即可

Glide.with(this).load("http://img.redocn.com/sheying/20150213/mulanweichangcaoyuanfengjing_3951976.jpg").into(iv4);

 

放一下xml文件以及activity

<ImageView
        android:id="@+id/iv4"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:background="#FF9900"
        android:scaleType="centerCrop"/>
package com.example.androidstudy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class ImageViewActivity extends AppCompatActivity {

    ImageView iv4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageview);
        iv4=findViewById(R.id.iv4);
        Glide.with(this).load("http://img.redocn.com/sheying/20150213/mulanweichangcaoyuanfengjing_3951976.jpg").into(iv4);
    }
}

然后效果图如下(需要启动应用之后会出现网络图片)

 

PS:如果启动之后没有图片识为什么:

Answer:可能是没有开启网络请求的允许

在AndroidManifest.xml文件中进行声明即可,即加入以下代码

<uses-permission android:name="android.permission.INTERNET">/</uses-permission>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Demo.demo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值