Android用Glide加载图像

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android用Glide加载图像

allway2   2022-05-23 我要评论

在进入 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("http://pixy.org/src/125/thumbs350/1251267.jpg")
                .into(imageView);
    }
}

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们