Android中怎么使用Glide加载图像

发布时间:2022-01-24 09:08:42 作者:柒染
来源:亿速云 阅读:144

今天就跟大家聊聊有关Android中怎么使用Glide加载图像,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

在进入 Glide 示例之前,我们应该知道什么是 glide,Glide 是 muyangmin 开发的一个图像处理库。使用 glide 库,我们可以显示图像、解码图像、缓存图像、动画 gif 等等。

这个例子演示了如何在 android 中集成 glide。

第 1 步- 在 Android Studio 中创建一个新项目,转到 File ⇒ New Project 并填写所有必需的详细信息以创建一个新项目。

第 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);
    }
}

让我们尝试运行您的应用程序。我假设您已将实际的 Android 移动设备与您的计算机连接起来。要从 android studio 运行应用程序,请打开项目的活动文件之一,然后单击   工具栏中的运行图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕

Android中怎么使用Glide加载图像

看完上述内容,你们对Android中怎么使用Glide加载图像有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. android 图片加载库 Glide 的使用介绍
  2. Android中Volley和Glide框架如何使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

glide android

上一篇:HTML5的语义标签怎么用

下一篇:C语言如何实现图书管理系统

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》