【Android 内存优化】Bitmap 长图加载 ( BitmapRegionDecoder 简介 | BitmapRegionDecoder 使用流程 | 区域解码加载示例 )

简介: 【Android 内存优化】Bitmap 长图加载 ( BitmapRegionDecoder 简介 | BitmapRegionDecoder 使用流程 | 区域解码加载示例 )

文章目录

一、BitmapRegionDecoder 简介

二、图片信息

三、BitmapRegionDecoder 对象创建

四、解码图像

五、图像区域解码示例

六、源码及资源下载







一、BitmapRegionDecoder 简介


官方文档 API : BitmapRegionDecoder




BitmapRegionDecoder 简介 :



① 主要作用 : BitmapRegionDecoder 可以从图像中 解码一个矩形区域 ;


② 适用场景 : 当一张图片非常大 , 在手机中只需要显示其中一部分内容 , BitmapRegionDecoder 非常有用 ;


③ 基本使用流程 : 先创建 , 后解码 ;


流程 1 : 创建 BitmapRegionDecoder : 调用 newInstance 方法 , 创建 BitmapRegionDecoder 对象 ;

// 创建 BitmapRegionDecoder 对象方法
static BitmapRegionDecoder newInstance(InputStream is, boolean isShareable)
static BitmapRegionDecoder newInstance(FileDescriptor fd, boolean isShareable)
static BitmapRegionDecoder newInstance(String pathName, boolean isShareable)
static BitmapRegionDecoder newInstance(byte[] data, int offset, int length, boolean isShareable)


流程 2 : 解码图像区域内容 : 调用 decodeRegion 方法 , 获取指定 Rect 矩形区域的解码后的 Bitmap 对象 ;

Bitmap decodeRegion(Rect rect, BitmapFactory.Options options)






二、图片信息


将一张图片存放在 assets 目录下 , 图片尺寸为 938 x 7561 , 这是 BitmapRegionDecoder 的文档截图 ;


该图片如果按照默认的 ARGB_8888 格式加载到内存中 , 会占用 28,368,872 字节的内存 , 大约 27 MB ;



内存大小计算过程如下 :


938 × 7561 × 4 = 28 , 368 , 872 938 \times 7561 \times 4 = 28,368,872

938×7561×4=28,368,872


image.png






三、BitmapRegionDecoder 对象创建


1 . BitmapRegionDecoder 对象创建 : 调用 newInstance 方法创建该对象 ;



① 函数作用 : 根据输入流创建 BitmapRegionDecoder 对象 ;


② 输入流的数据位置 : 输入流的当前读取位置就是在之前读取的的解码数据的后面一个字节位置 ;


③ 支持的图像格式 : 目前图像区域解码对象只支持 JPEG 和 PNG 两种图像格式 ;




2 . 函数原型 :



InputStream is 参数 : 图片的输入流 ;


boolean isShareable 参数 : 是否共享输入流 ; 如果设置了共享为 true , 如果将该输入流关闭 , 假如 BitmapRegionDecoder 对象中也在使用该输入流 , 那么关闭以后 , BitmapRegionDecoder 对象也无法使用该输入流了 ; 如果设置该参数为 false , 那么关闭该输入流 , 不影响 BitmapRegionDecoder 对象使用 , 一般都是该区域解码对象需要长时间使用 , 此处都要设置成 false ;


 

public static BitmapRegionDecoder newInstance(InputStream is,
            boolean isShareable) throws IOException {
        // 当前的输入流是 AssetInputStream 输入流的情况
        if (is instanceof AssetManager.AssetInputStream) {
            return nativeNewInstance(
                    ((AssetManager.AssetInputStream) is).getNativeAsset(),
                    isShareable);
        } else {
          // 当前的输入流是文件输入流
          // 传入临时缓存到 Native 代码中 ; 
          // 创建一个足够大的临时缓存区 , 这样可以减少 is.read 方法的回调次数 ; 
          // 应该避免 is.read 回调次数太多 , 同时每次读取很少数据的情况 ; 
            byte [] tempStorage = new byte[16 * 1024];
            return nativeNewInstance(is, tempStorage, isShareable);
        }
    }





四、解码图像


函数原型 : 解码 JPEG 或 PNG 中指定的矩形区域 ;


Rect rect 参数 : 要解码的矩形区域 ;

BitmapFactory.Options options 参数 : 解码选项 ;

public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options)

1





五、图像区域解码示例


1 . 主界面代码 : 先创建 BitmapRegionDecoder 对象 , 然后调用该对象的 decodeRegion 方法 , 进行图像剪切 ;


package kim.hsl.lgl;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        // 显示剪切后的正方形图像
        showImage();
    }
    private void showImage(){
        InputStream inputStream = null;
        try {
            // 获取 Assets 文件的输入流
            inputStream = getAssets().open("bitmap_region_decoder.png");
            /*
                函数原型 :
                public static BitmapRegionDecoder newInstance(InputStream is,
                    boolean isShareable) throws IOException {
                InputStream is 参数 : 图片的输入流
                boolean isShareable 参数 : 是否共享输入流
                如果设置了共享为 true , 如果将该输入流关闭 ,
                假如 BitmapRegionDecoder 对象中也在使用该输入流 ,
                那么关闭以后 , BitmapRegionDecoder 对象也无法使用该输入流了 ;
                如果设置该参数为 false , 那么关闭该输入流 , 不影响 BitmapRegionDecoder 对象使用 ,
                一般都是该区域解码对象需要长时间使用 , 此处都要设置成 false ;
             */
            BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder
                    .newInstance(inputStream, false);
            /*
                解码图片
                这里解析前面的一部分图片
             */
            Bitmap bitmap = bitmapRegionDecoder.decodeRegion(
                    new Rect(0, 0, 938, 938),   //解码区域
                    null);  //解码选项 BitmapFactory.Options 类型
            ImageView imageView = findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public native String stringFromJNI();
}




2 . 布局文件 : 在布局中放置一个正方形的 ImageView , 显示剪切后的 938 x 938 大小的 Bitmap 图片 ;


<?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">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:scaleType="fitXY"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/sample_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>



3 . 执行效果 : 正方形的 ImageView , 显示从 938 x 7561 大小的图片上剪切下来的 938 x 938 大小的图片 , 效果如下 ;


image.png






六、源码及资源下载


源码及资源下载地址 :


① GitHub 工程地址 : Long_Graph_Loading


② MainActivity.java 主界面代码地址 : MainActivity.java , 这是上述示例代码中的主界面代码 ;


目录
相关文章
|
7月前
|
缓存 编解码 Android开发
Android内存优化之图片优化
本文主要探讨Android开发中的图片优化问题,包括图片优化的重要性、OOM错误的成因及解决方法、Android支持的图片格式及其特点。同时介绍了图片储存优化的三种方式:尺寸优化、质量压缩和内存重用,并详细讲解了相关的实现方法与属性。此外,还分析了图片加载优化策略,如异步加载、缓存机制、懒加载等,并结合多级缓存流程提升性能。最后对比了几大主流图片加载框架(Universal ImageLoader、Picasso、Glide、Fresco)的特点与适用场景,重点推荐Fresco在处理大图、动图时的优异表现。这些内容为开发者提供了全面的图片优化解决方案。
278 1
|
存储 前端开发 Java
Android MVVM架构模式下如何避免内存泄漏
Android采用MVVM架构开发项目,如何避免内存泄漏风险?怎样避免内存泄漏?
379 1
|
监控 Java Android开发
深入探讨Android系统的内存管理机制
本文将深入分析Android系统的内存管理机制,包括其内存分配、回收策略以及常见的内存泄漏问题。通过对这些方面的详细讨论,读者可以更好地理解Android系统如何高效地管理内存资源,从而提高应用程序的性能和稳定性。
516 16
|
12月前
|
监控 Java Android开发
深入探索Android系统的内存管理机制
本文旨在全面解析Android系统的内存管理机制,包括其工作原理、常见问题及其解决方案。通过对Android内存模型的深入分析,本文将帮助开发者更好地理解内存分配、回收以及优化策略,从而提高应用性能和用户体验。
|
Android开发 开发者
Android性能优化——内存管理的艺术
Android性能优化——内存管理的艺术
|
缓存 监控 Java
在使用 Glide 加载 Gif 动画时避免内存泄漏的方法
【10月更文挑战第20天】在使用 Glide 加载 Gif 动画时,避免内存泄漏是非常重要的。通过及时取消加载请求、正确处理生命周期、使用弱引用、清理缓存和避免重复加载等方法,可以有效地避免内存泄漏问题。同时,定期进行监控和检测,确保应用的性能和稳定性。需要在实际开发中不断积累经验,根据具体情况灵活运用这些方法,以保障应用的良好运行。
|
存储 前端开发 测试技术
Android kotlin MVVM 架构简单示例入门
Android kotlin MVVM 架构简单示例入门
261 1
|
编解码 Android开发 UED
构建高效Android应用:从内存优化到用户体验
【10月更文挑战第11天】本文探讨了如何通过内存优化和用户体验改进来构建高效的Android应用。介绍了使用弱引用来减少内存占用、懒加载资源以降低启动时内存消耗、利用Kotlin协程进行异步处理以保持UI流畅,以及采用响应式设计适配不同屏幕尺寸等具体技术手段。
199 2
|
2月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
269 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
2月前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
226 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡

热门文章

最新文章