1

有沒有辦法將圖像從互聯網延遲加載到遠程視圖中。appwidget中的lazyload圖像

remoteView.setImageViewBitmap(R.id.image, UrlUtils.loadBitmap(bitmapUrl)); 

我使用此功能,但它在很短的時間內阻止了我的小部件。

public static Bitmap loadBitmap(String url) { 
    Bitmap bitmap = null; 
    InputStream in = null; 
    BufferedOutputStream out = null; 

    try { 

     in = new BufferedInputStream(getPageInputStream(url)); 

     final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); 
     out = new BufferedOutputStream(dataStream); 
     Utils.copy(in, out); 
     out.flush(); 

     final byte[] data = dataStream.toByteArray(); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     // options.inSampleSize = 1; 

     bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, 
       options); 

     in.close(); 
     out.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return bitmap; 
} 

感謝

回答

3

絕對

  1. 畫出你的插件的常規方式,所有的文字部分,圖像等
  2. 旁邊創建一個將載入圖片的服務。 Here's good tutorial,其中包括如何創建並從appwidget調用服務
  3. 更新您的小部件後調用該服務。通過小部件ID和圖像URL
  4. 從緩存或遠程加載圖像到您的服務中,並再次更新您的小部件。瞧,你現在有它
+0

如果沒有這兩個更新之間再次更新只小部件。在這種情況下,您將失去新的窗口小部件狀態 – Maxim

+0

還有一件事,您需要在將圖像展示到窗口小部件之前向下取樣您的圖像,否則您將被「RemoteViews用於窗口小部件更新超出最大位圖內存使用情況例外」的衝擊。 – TilalHusain

+0

@Bostone如果你需要加載appWidget中的listView的圖像?根據我所看到的,這是在「getViewAt」(在BG線程上運行)中完成的,但在加載圖像之前無法更新其餘行(文本等),這太糟糕了...... –