關于AndroidStudio的ImageView使用網路上的圖片的方法
1.新建一個Activity名為 ImageActivity
2.在layout下的activity_img_view.xml下新建ImageView
<ImageView
android:id="@+id/imgv01"
android:layout_marginTop="10dp"
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
/>
3.打開GitHub
鏈接: GitHub.
4.輸入glide

5.找到第一個

6.找到使用Gradle的方法

要將這一串代碼粘到AndroidStudio中
repositories {
google()
jcenter()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
7.找到AndroidStudio的build.gradle
這個地方會發現有兩個build.gradle,我們要的是APP下的build.gradle,也就是上面選中這個

或者打開左上角這個Project,就可以找到build.gradle

8.粘貼代碼進去

9.使用

打開IimageViewActivity
“先宣告”
private ImageView imgv;
“再找到”
imgv=findViewById(R.id.imgv01);
“再使用”
Glide.with(this).load("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(imgv);
IimageViewActivity完整代碼(僅關注插入陳述句的位置即可,其他有不同可以不管)
package com.example.androidlearning;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImgViewActivity extends AppCompatActivity {
private ImageView imgv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_img_view);
imgv=findViewById(R.id.imgv01);
Glide.with(this).load("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(imgv);
}
}
10.給網路權限
在Androidmanifest.xml下添加以下陳述句
<uses-permission android:name="android.permission.INTERNET" />
Androidmanifest.xml完整代碼(僅關注插入陳述句的位置即可,其他有不同可以不管)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidlearning">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLearning">
<activity android:name=".ListViewActivity"></activity>
<activity android:name=".ImgViewActivity" />
<activity android:name=".SwitchActivity" />
<activity android:name=".CheckBoxActivity" />
<activity android:name=".RadioButtonActivity" />
<activity android:name=".EditTextActivity" />
<activity android:name=".ButtonActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
11.運行

本方法在B站向天哥學習,寫博客加強記憶,在此分享給有需要的朋友,
本人Android入門新手,歡迎各位批評指正
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/252705.html
標籤:其他
