簡體   English   中英

XML與setImageDrawable / setImageBitmap

[英]XML vs. setImageDrawable/setImageBitmap

如果我預加載某些圖像,在我的應用程序中是有利的。 我在AsyncTask中正確地執行了此操作,因為它是在官方文檔中編寫的。 但是我有一個關於何時應該設置的問題/疑問。

我將展示代碼片段。 請注意,它已經簡化了(它們的互操作性在我的實際代碼中更好,它會檢查空值等)。

讓我們先看看原始(非預裝)版本:

<ImageView
    android:id="@+id/imageViewMyGraphicalImageElement"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop" 
    android:src="@drawable/my_graphical_element" >
</ImageView>

預加載版本具有以下XML(請注意缺少src屬性):

<ImageView
    android:id="@+id/imageViewMyGraphicalImageElement"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop">
</ImageView>

以及預裝代碼的片段:

sBitmap = bitmapBitmapFactory.decodeResource(context.getResources(), R.drawable.my_graphical_element, options); 
// 'sBitmap' is a Bitmap reference, while 'options' is BitmapFactory.Options

最后,我設置它的地方:

setContentView(R.layout.main);
...
ImageView imageViewMyGraphicalImageElement= (ImageView) findViewById(R.id.imageViewMyGraphicalImageElement);
imageViewMyGraphicalImageElement.setImageBitmap(sBitmap); 

問題:顯然,基於xml的解決方案調用setContentView(...) 之前知道圖像。 預加載版本該調用之后設置圖像。 有什么區別嗎? 由於這個原因,是否可以跳過某些自動縮放或系統完成的其他事情?

我剛剛為此寫了一篇文章。 希望能夠回答你的問題。

https://plus.google.com/112740367348600290235/posts/VNAfFLDcKrw

ImageView有4個API來指定圖像。 哪一個使用? 有什么不同?

  1. setImageDrawable(Drawable drawable)
  2. setImageBitmap(Bitmap bm)
  3. setImageResource(int resId)
  4. setImageURI(URI uri)

ImageView ,名稱,用於顯示圖像。 但是什么是圖像? Bitmap是一個圖像,不難理解,我們使用setImageBitmap來達到這個目的。 但是,在內部, ImageView有一個Drawable但不是Bitmap ,這就是setImageDrawable用途。 在內部調用setImageBitmap時,首先將位圖包裝到BitmapDrawable ,即IS-A Drawable ,然后調用setImageDrawable

這是代碼。

public void setImageBitmap(Bitmap bm) {
    setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}

那么,3和4 API怎么樣?

您應該已經知道,這些是從文件路徑,Uri或資源文件創建位圖的一系列方法。

BitmapFactory.decodeFile(String pathName)
BitmapFactory.decodeStream(Inputstream)
BitmapFactory.decodeResource(Resource res, int id)  
BitmapFactory.decodeByteArray(byte[] data)

意識到這一點,很容易理解setImageResource / setImageUrisetImageBitmap相同。

總而言之, setImageDrawable是其他API所依賴的原始函數。 其他3個只是幫助方法,使您編寫更少的代碼。

另外,要記住ImageView實際上有一個Drawable非常重要,它不一定是BitmapDrawable 您可以將任何Drawable設置為Image視圖。

除了通過Java API設置Drawable之外,您還可以使用XML屬性為ImageView設置源Drawable 見下面的例子。 請注意,形狀可以是圖像文件(.png,.jpg,.bmp)或xml文件。

<ImageView
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:src="@drawable/shape"/>

shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="270"/>
    <padding android:left="7dp" android:top="7dp android:right="7dp" android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

完全沒有區別。 你可以認為所有的ImageView構造函數都用android:src屬性調用setImageResource

更新:實際上它使用setImageDrawable ,這是獲取屬性的ImageView構造函數的實際代碼:

    Drawable d = a.getDrawable(com.android.internal.R.styleable.ImageView_src);
    if (d != null) {
        setImageDrawable(d);
    }

參考: ImageView.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM