Android在使用图片的时候是相当麻烦的,因为需要一个像素一个像素地加载这些图片到内存。一个中端手机所拍摄的一张照片有2592×1936(5百万)像素,这会占用大概19M内存。如果你再加上各种好坏不一的网络下的图片请求,同时要处理缓存、图片加载等问题,焦头烂额。如果你这时候使用了一个像Glide一样经过不断优化和严格测试的图片处理库,你会庆幸你节省了大量的时间,同时也避免了很多头疼的问题。
官方API:
https://muyangmin.github.io/glide-docs-cn/#glide-v4-android-english-tip
参考文章
https://www.jianshu.com/p/2576d5a09dd5
https://blog.csdn.net/wangsongbin893603021/article/details/75426853
https://blog.csdn.net/Departure_/article/details/51698625
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
主布局就包含了一个`ImageView
<LinearLayout 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">
<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp" />
LinearLayout>
首先开启网络访问权限
在项目清单文件中添加
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MianActivity.java
package com.enjoy.glidedemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = findViewById(R.id.iv);
RequestOptions requestOptions = new RequestOptions()
//遇到不同情况展示的默认图片
.placeholder(R.drawable.hold)
.error(R.drawable.error)
.fallback(R.drawable.empty)
.override(1000, 1000);//图片像素
Glide.with(this)
.load("https://pic.ku66.net/tutu/2021/allimg/210323/23145404-4-Z40.jpg")
.apply(requestOptions)
.into(iv);
}
}
MainActivity.java
package com.enjoy.glidedemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = findViewById(R.id.iv);
RequestOptions requestOptions = new RequestOptions()
//遇到不同情况展示的默认图片
.placeholder(R.drawable.hold)
.error(R.drawable.error)
.fallback(R.drawable.empty)
.override(1000, 1000);//图片像素
Glide.with(this)
.load("https://pic.ku66.net/tutu/2021/allimg/210323/23145404-4-Z40.jpg")
.apply(requestOptions)
//变换图片
.transform(new CircleCrop()) //圆角效果
.into(iv);
}
}
更多的效果参考官方APIhttps://muyangmin.github.io/glide-docs-cn/#glide-v4-android-english-tip
创建一下两个类
2.1、MyAppModule.java
主要是继承AppGlideModule
,以及添加注解@GlideModule
package com.enjoy.glidedemo;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
@GlideModule
public class MyAppModule extends AppGlideModule {
}
2.2、MyAppExtension.java
这个类主要写图片的设置
但是不能设置图片的变换
package com.enjoy.glidedemo;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.BaseRequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
@GlideExtension
public class MyAppExtension {
private MyAppExtension() {
} // utility class
@GlideOption
public static BaseRequestOptions<?> defaultImg(BaseRequestOptions<?> options) {
DrawableCrossFadeFactory factory =
new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
return options
.placeholder(R.drawable.hold)
.error(R.drawable.error)
.fallback(R.drawable.empty)
.transform(new Rotate(90));
}
}
2.3、MainActivity.java
package com.enjoy.glidedemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = findViewById(R.id.iv);
GlideApp.with(this)
.load("https://avatar.csdnimg.cn/8/1/B/1_niulinbiao_1621353330.jpg")
.transform(new CircleCrop())
.defaultImg().into(iv);
}
}