Android中如何使用Glide加載圖像
在進入 Glide 示例之前,我們應(yīng)該知道什么是 glide,Glide 是 muyangmin 開發(fā)的一個圖像處理庫。使用 glide 庫,我們可以顯示圖像、解碼圖像、緩存圖像、動畫 gif 等等。
這個例子演示了如何在 android 中集成 glide。
第 1 步- 在 Android Studio 中創(chuàng)建一個新項目,轉(zhuǎn)到 File ⇒ New Project 并填寫所有必需的詳細信息以創(chuàng)建一個新項目。
第 2 步- 在 build.gradle(Module:app)中添加以下代碼。
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.myapplication"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}第 3 步- 在 AndroidManifest.xml 中添加以下代碼。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>第 4 步- 將以下代碼添加到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#797979"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>第 5 步- 將以下代碼添加到 src/MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.example.myapplication.R;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
Glide.with(this)
.load("https://pixy.org/src/125/thumbs350/1251267.jpg")
.into(imageView);
}
}讓我們嘗試運行您的應(yīng)用程序。我假設(shè)您已將實際的 Android 移動設(shè)備與您的計算機連接起來。要從 android studio 運行應(yīng)用程序,請打開項目的活動文件之一,然后單擊 工具欄中的運行圖標。選擇您的移動設(shè)備作為選項,然后檢查您的移動設(shè)備,它將顯示您的默認屏幕

到此這篇關(guān)于Android中如何使用Glide加載圖像的文章就介紹到這了,更多相關(guān)Android用Glide加載圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義view實現(xiàn)圓形waveview
這篇文章主要為大家詳細介紹了Android自定義view實現(xiàn)圓形waveview,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
點擊圖標進入指定瀏覽器將首頁設(shè)置全透明解決一閃而過問題
進入瀏覽器之前有一個頁面閃了一下,那是因為從那個空白的首頁跳過去的。解決的辦法是把他變成透明的就好了2014-08-08
Android Studio 自定義Debug變量視圖的方法
這篇文章主要介紹了Android Studio 自定義Debug變量視圖的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Android開發(fā)之ImageSwitcher相冊功能實例分析
這篇文章主要介紹了Android開發(fā)之ImageSwitcher相冊功能,結(jié)合實例形式分析了Android ImageSwitcher相冊的原理、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-03-03
android 修改launcher行數(shù)和列數(shù)的方法
這篇文章主要介紹了android 修改launcher行數(shù)和列數(shù)的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
Android自定義View實現(xiàn)繪制水波浪溫度刻度表
這篇文章主要為大家詳細介紹了Android如何利用自定義View實現(xiàn)一個水波浪溫度刻度表,文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下2022-11-11

