iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android中怎么使用Glide加载图像
  • 572
分享到

Android中怎么使用Glide加载图像

2023-06-28 23:06:11 572人浏览 安东尼
摘要

今天就跟大家聊聊有关Android中怎么使用Glide加载图像,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。在进入 Glide 示例之前,我们应该知道什么是 glide,Glide

今天就跟大家聊聊有关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加载图像有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。

--结束END--

本文标题: Android中怎么使用Glide加载图像

本文链接: https://www.lsjlt.com/news/321241.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作