安卓开发之Glide图片处理框架的使用

前言

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

一、基本使用

1.1、引入依赖

implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

1.2、activity_main.xml

主布局就包含了一个`ImageView

<?xml version="1.0" encoding="utf-8"?>
<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>

1.3、加载网络图片

首先开启网络访问权限
在项目清单文件中添加

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

在这里插入图片描述

1.4、修改图片效果

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韭菜盖饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值