Android Bitmap 在decode的时候快速取消(打断)的方法

    xiaoxiao2025-04-10  14

    在做图片浏览的时候经常要取消图片的decode任务,一般做法都是从队列里取消,一旦进入decode的阻塞状态,就不能立刻取消了,只能等到decode完毕,而decode是比较耗时的。网上搜了一圈有人提出,用关闭输入流的方法。 经过试验,关闭输入流的方法确实是有效的,不过图片可能出现半张图的情况。并且在 使用byte数组的情况下无法使用。

    不过刚刚发现了一个给力的方法, 那就是 BitmapFactory.Options 里面的

    /** * This can be called from another thread while this options object is * inside a decode... call. Calling this will notify the decoder that * it should cancel its operation. This is not guaranteed to cancel * the decode, but if it does, the decoder... operation will return * null, or if inJustDecodeBounds is true, will set outWidth/outHeight * to -1 */ public void requestCancelDecode() { mCancel = true; requestCancel(); }

    方法说明里说,取消成功Bitmap返回null ,如果是取图片大小,那么全返回-1 不存在图片取得一半的情况。

    转载请注明原文地址: https://ju.6miu.com/read-1297929.html
    最新回复(0)