ImageDecoder 類別

定義

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

[Android.Runtime.Register("android/graphics/ImageDecoder", ApiSince=28, DoNotGenerateAcw=true)]
public sealed class ImageDecoder : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IAutoCloseable
[<Android.Runtime.Register("android/graphics/ImageDecoder", ApiSince=28, DoNotGenerateAcw=true)>]
type ImageDecoder = class
    inherit Object
    interface IAutoCloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
繼承
ImageDecoder
屬性
實作

備註

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

要使用它,首先要建立Source Source一個超載。createSource 例如,從 Uri解碼時,呼叫 #createSource(ContentResolver, Uri) 並將結果傳遞給 #decodeDrawable(Source)#decodeBitmap(Source)

File file = new File(...);
             ImageDecoder.Source source = ImageDecoder.createSource(file);
             Drawable drawable = ImageDecoder.decodeDrawable(source);

要更改預設設定,請將 和 OnHeaderDecodedListener OnHeaderDecodedListener 傳送Source Source#decodeDrawable(Source, OnHeaderDecodedListener)#decodeBitmap(Source, OnHeaderDecodedListener)。 例如,若要建立一張寬度與高度為原始影像一半的取樣影像,請在內部OnHeaderDecodedListener#onHeaderDecoded onHeaderDecoded呼叫#setTargetSampleSize setTargetSampleSize(2)

OnHeaderDecodedListener listener = new OnHeaderDecodedListener() {
                 public void onHeaderDecoded(ImageDecoder decoder, ImageInfo info, Source source) {
                     decoder.setTargetSampleSize(2);
                 }
             };
             Drawable drawable = ImageDecoder.decodeDrawable(source, listener);

ImageInfo ImageInfo包含關於編碼影像的資訊,如寬度與高度,Source SourceOnHeaderDecodedListener OnHeaderDecodedListener單一影像與多個Source Source物件使用,則可用來匹配Source Source特定影像。

也可以 OnHeaderDecodedListener OnHeaderDecodedListener 實作為 lambda:

Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -&gt; {
                 decoder.setTargetSampleSize(2);
             });

若編碼影像為動畫GIFWEBP或,#decodeDrawable decodeDrawable則回傳 AnimatedImageDrawable。 要開始其動畫,請呼叫 AnimatedImageDrawable#start AnimatedImageDrawable.start()

Drawable drawable = ImageDecoder.decodeDrawable(source);
             if (drawable instanceof AnimatedImageDrawable) {
                 ((AnimatedImageDrawable) drawable).start();
             }

預設情況下, Bitmap 由 創建的( ImageDecoder 包括位於 內 Drawable的 )將是不可變的(即 Bitmap#isMutable Bitmap.isMutable() 返回 false),通常具有 ConfigBitmap.Config#HARDWARE。 雖然這些屬性可以隨 改變#setMutableRequired setMutableRequired(true)(僅與 #decodeBitmap(Source)#decodeBitmap(Source, OnHeaderDecodedListener)#setAllocator相容),但也可以透過將 a PostProcessor 傳遞給 #setPostProcessor setPostProcessor,來套用自訂效果,不論最終回傳物件的可變性如何。 A PostProcessor 也可以是 lambda:

Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -&gt; {
                 decoder.setPostProcessor((canvas) -&gt; {
                         // This will create rounded corners.
                         Path path = new Path();
                         path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
                         int width = canvas.getWidth();
                         int height = canvas.getHeight();
                         path.addRoundRect(0, 0, width, height, 20, 20, Path.Direction.CW);
                         Paint paint = new Paint();
                         paint.setAntiAlias(true);
                         paint.setColor(Color.TRANSPARENT);
                         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
                         canvas.drawPath(path, paint);
                         return PixelFormat.TRANSLUCENT;
                 });
             });

若編碼影像不完整或有錯誤,或解碼過程中發生錯誤 Exception ,則會拋出 a DecodeException DecodeException 。 在某些情況下,他們 ImageDecoder 可能解碼了部分影像。 為了顯示部分影像,必須將 a OnPartialImageListener OnPartialImageListener 傳遞給 #setOnPartialImageListener setOnPartialImageListener。 例如:

Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -&gt; {
                 decoder.setOnPartialImageListener((DecodeException e) -&gt; {
                         // Returning true indicates to create a Drawable or Bitmap even
                         // if the whole image could not be decoded. Any remaining lines
                         // will be blank.
                         return true;
                 });
             });

Java 文件 android.graphics.ImageDecoder

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

欄位

名稱 Description
AllocatorDefault
已淘汰.

使用像素記憶體的預設配置。

AllocatorHardware
已淘汰.

需要一個 Bitmap.Config#HARDWAREBitmap.

AllocatorSharedMemory
已淘汰.

像素記憶體使用共用記憶體。

AllocatorSoftware
已淘汰.

使用軟體配置來管理像素記憶體。

MemoryPolicyDefault
已淘汰.

內部用最自然Bitmap.ConfigBitmap的。

MemoryPolicyLowRam
已淘汰.

如果可能的話,使用較 Bitmap.Config 密集的記憶體,但會犧牲一些影像品質。

屬性

名稱 Description
Allocator

回傳像素記憶體的配置器。 -或者- 選擇像素記憶體的背面。

Class

回傳此 Object的執行時類別。

(繼承來源 Object)
Crop

如果設定為,回傳裁切的矩形。 -或- 將輸出裁切成 subset (可能)縮放後的影像。

DecodeAsAlphaMaskEnabled

回傳是否將單通道輸入視為 alpha。 -或- 指定是否可能將輸出視為 alpha 遮罩。

Handle

底層 Android 實例的帳號。

(繼承來源 Object)
JniIdentityHashCode

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
JniPeerMembers

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

MemorySizePolicy

取得解碼後 Bitmap的記憶體策略。 -或- 指定解碼後 Bitmap的記憶體政策。

MutableRequired

回傳解碼 Bitmap 後的是否會是可變的。 -或- 指定是否 Bitmap 應該是可變的。

OnPartialImageListener

在這個物件上設定(替換)。OnPartialImageListener

PeerReference

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
PostProcessor

退掉 PostProcessor 目前的那套。 -或- 在解碼和縮放後修改影像。

ThresholdClass

此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。

(繼承來源 Object)
ThresholdType

此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。

(繼承來源 Object)
UnpremultipliedRequired

回傳是否 Bitmap 會有未預乘的像素。 -或- 指定是否 Bitmap 應該有未預乘的像素。

方法

名稱 Description
Clone()

建立並回傳此物件的副本。

(繼承來源 Object)
Close()

關閉此資源,放棄所有底層資源。

CreateSource(AssetManager, String)

android.net.Uri. 建立一個新的Source

CreateSource(Byte[], Int32, Int32)

android.net.Uri. 建立一個新的Source

CreateSource(Byte[])

android.net.Uri. 建立一個新的Source

CreateSource(ByteBuffer)

android.net.Uri. 建立一個新的Source

CreateSource(ContentResolver, Uri)

android.net.Uri. 建立一個新的Source

CreateSource(File)

android.net.Uri. 建立一個新的Source

CreateSource(ICallable)

android.net.Uri. 建立一個新的Source

CreateSource(Resources, Int32)

android.net.Uri. 建立一個新的Source

DecodeBitmap(ImageDecoder+Source, ImageDecoder+IOnHeaderDecodedListener)

參見 #decodeBitmap(Source, OnHeaderDecodedListener)

DecodeBitmap(ImageDecoder+Source)

參見 #decodeBitmap(Source, OnHeaderDecodedListener)

DecodeDrawable(ImageDecoder+Source, ImageDecoder+IOnHeaderDecodedListener)

參見 #decodeDrawable(Source, OnHeaderDecodedListener)

DecodeDrawable(ImageDecoder+Source)

參見 #decodeDrawable(Source, OnHeaderDecodedListener)

Dispose()

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
Dispose(Boolean)

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
Equals(Object)

表示是否有其他物體「等同」於此物。

(繼承來源 Object)
GetHashCode()

回傳物件的雜湊碼值。

(繼承來源 Object)
IsMimeTypeSupported(String)

如果給定的 MIME 類型是可由此類別解碼的支援檔案格式,則回傳。

JavaFinalize()

當垃圾回收判定該物件不再有相關參考時,由垃圾回收器呼叫。

(繼承來源 Object)
Notify()

喚醒一個正在該物件監視器上等待的執行緒。

(繼承來源 Object)
NotifyAll()

喚醒所有等待該物件監視器的執行緒。

(繼承來源 Object)
SetHandle(IntPtr, JniHandleOwnership)

設定 Handle 屬性。

(繼承來源 Object)
SetTargetColorSpace(ColorSpace)

指定輸出的期望 ColorSpace 值。

SetTargetSampleSize(Int32)

用 sampleSize 設定目標大小。

SetTargetSize(Int32, Int32)

指定輸出 Drawable 大小或 Bitmap

ToArray<T>()

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
ToString()

回傳物件的字串表示。

(繼承來源 Object)
UnregisterFromRuntime()

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
Wait()

導致目前執行緒等待被喚醒,通常是透過 <em>通知</><em 或 em>中斷</em> 來喚醒。

(繼承來源 Object)
Wait(Int64, Int32)

會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。

(繼承來源 Object)
Wait(Int64)

會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。

(繼承來源 Object)

事件

名稱 Description
PartialImage

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

明確介面實作

名稱 Description
IJavaPeerable.Disposed()

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
IJavaPeerable.DisposeUnlessReferenced()

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
IJavaPeerable.Finalized()

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
IJavaPeerable.JniManagedPeerState

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
IJavaPeerable.SetJniIdentityHashCode(Int32)

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)
IJavaPeerable.SetPeerReference(JniObjectReference)

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

(繼承來源 Object)

擴充方法

名稱 Description
GetJniTypeName(IJavaPeerable)

取得實例 self類型的 JNI 名稱。

JavaAs<TResult>(IJavaPeerable)

試著強制self輸入 TResult,檢查 強制在 Java 端是否有效。

JavaCast<TResult>(IJavaObject)

執行 Android 執行時檢查型別轉換。

JavaCast<TResult>(IJavaObject)

一個用來將編碼影像(如 PNGJPEGWEBPHEIFGIF、 或 )轉換成 Drawable 或 物件Bitmap的類別。

TryJavaCast<TResult>(IJavaPeerable, TResult)

試著強制self輸入 TResult,檢查 強制在 Java 端是否有效。

適用於