Android中如何使用Glide加載圖像
在進(jìn)入 Glide 示例之前,我們應(yīng)該知道什么是 glide,Glide 是 muyangmin 開(kāi)發(fā)的一個(gè)圖像處理庫(kù)。使用 glide 庫(kù),我們可以顯示圖像、解碼圖像、緩存圖像、動(dòng)畫(huà) gif 等等。
這個(gè)例子演示了如何在 android 中集成 glide。
第 1 步- 在 Android Studio 中創(chuàng)建一個(gè)新項(xiàng)目,轉(zhuǎn)到 File ⇒ New Project 并填寫(xiě)所有必需的詳細(xì)信息以創(chuàng)建一個(gè)新項(xià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ùn)行您的應(yīng)用程序。我假設(shè)您已將實(shí)際的 Android 移動(dòng)設(shè)備與您的計(jì)算機(jī)連接起來(lái)。要從 android studio 運(yùn)行應(yīng)用程序,請(qǐng)打開(kāi)項(xiàng)目的活動(dòng)文件之一,然后單擊 工具欄中的運(yùn)行圖標(biāo)。選擇您的移動(dòng)設(shè)備作為選項(xiàng),然后檢查您的移動(dòng)設(shè)備,它將顯示您的默認(rèn)屏幕
到此這篇關(guān)于Android中如何使用Glide加載圖像的文章就介紹到這了,更多相關(guān)Android用Glide加載圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義view實(shí)現(xiàn)圓形waveview
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)圓形waveview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01點(diǎn)擊圖標(biāo)進(jìn)入指定瀏覽器將首頁(yè)設(shè)置全透明解決一閃而過(guò)問(wèn)題
進(jìn)入瀏覽器之前有一個(gè)頁(yè)面閃了一下,那是因?yàn)閺哪莻€(gè)空白的首頁(yè)跳過(guò)去的。解決的辦法是把他變成透明的就好了2014-08-08Android Studio 自定義Debug變量視圖的方法
這篇文章主要介紹了Android Studio 自定義Debug變量視圖的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07詳解Android使用Socket對(duì)大文件進(jìn)行加密傳輸
這篇文章主要介紹了詳解Android使用Socket對(duì)大文件進(jìn)行加密傳輸,使用Socket進(jìn)行文件傳輸過(guò)程時(shí),需要先進(jìn)行加密,有興趣的可以了解一下。2017-01-01Android開(kāi)發(fā)之ImageSwitcher相冊(cè)功能實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)之ImageSwitcher相冊(cè)功能,結(jié)合實(shí)例形式分析了Android ImageSwitcher相冊(cè)的原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03android 修改launcher行數(shù)和列數(shù)的方法
這篇文章主要介紹了android 修改launcher行數(shù)和列數(shù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Android自定義View實(shí)現(xiàn)繪制水波浪溫度刻度表
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)一個(gè)水波浪溫度刻度表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11