ImageViewActivity.class
package com.xdw.a122; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.bumptech.glide.Glide; public class ImageViewActivity extends AppCompatActivity { private ImageView mIv4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_imageview); mIv4=findViewById(R.id.iv_2); Glide.with(this).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(mIv4); } }
activity_imageview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ImageViewActivity" android:padding="15dp"> <ImageView android:id="@+id/iv_1" android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/back_1" android:scaleType="fitCenter"/> <ImageView android:id="@+id/iv_2" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="fitCenter" android:background="#000000" android:layout_below="@id/iv_1" android:layout_marginTop="20dp"/> </RelativeLayout>
并在build.gradle中添加
repositories {
mavenCentral()
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
在AndroidManifest.xml中的<application/>中添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
我学习的:
//导入网络图片
mIv4=findViewById(R.id.iv_2); Glide.with(this).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(mIv4);
//ImageView块
scaleType= 填充方式
fitCenter 按比例填充直到填满
fitXY 直接填充可能拉伸图片
center crop 按比例填充满,多余切割不显示
<ImageView android:id="@+id/iv_2" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="fitCenter" android:background="#000000" android:layout_below="@id/iv_1" android:layout_marginTop="20dp"/>