2015-06-22 43 views
-1

我正在製作一個音樂播放器,到目前爲止我所做的是我使用元數據文件在其列表視圖中創建了一個列表視圖中的歌曲列表。 現在當我滾動我的列表它滯後,這可能是因爲圖像加載。 我提到Google Play音樂應用程序,他們所做的是他們在滾動時抓取專輯封面,所以滾動非常順暢。 有什麼辦法來實現這種功能?僅當它們即將顯示時在列表視圖中加載圖像

+0

在您的列表視圖可以將您的圖像異步加載到意見,這個類的調用。看到http://developer.android.com/training/displaying-bitmaps/load-bitmap.html它會有所幫助。 – Sayem

回答

0

你必須使用延遲加載的圖像,所以圖像將在後臺下載。

public class DownloadImage extends AsyncTask<ImageView, Void, Bitmap> { 

    ImageView imageView = null; 

    @Override 
    protected Bitmap doInBackground(ImageView... imageViews) { 
     this.imageView = imageViews[0]; 
     return download_Image((String) imageView.getTag()); 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     imageView.setImageBitmap(result); 
    } 

    private Bitmap download_Image(String url) { 
     try { 
      URL ulrn = new URL(url); 
      HttpURLConnection con = (HttpURLConnection) ulrn.openConnection(); 

      InputStream iS = con.getInputStream(); 
      Bitmap bmp = BitmapFactory.decodeStream(iS); 
      return bmp; 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
    } 

} 

請在您的適配器getview

imgThumbnail.setTag(<your image url>); 
new DownloadImage().execute(imgThumbnail); 
+0

我不想從url獲取圖像。我想從他們那裏得到他們。這是怎麼回事?是imgThumbnail一個ImageView? –

+0

這是異步任務,imgthumbnail是一個imageview。您可以修改異步任務以適應您的要求。如果你需要一個例子,給我一個樣本uri –

+0

final Uri sArtworkUri = Uri .parse(「content:// media/external/audio/albumart」); Uri uri = ContentUris.withAppendedId(sArtworkUri,currSong.getAlbumID()); 這是我的uri代碼。我嘗試修改異步任務,但它需要上下文,我不能通過,因爲它會超過任何參數。 –

相關問題