2016-04-24 11 views
1

私のMusic-AppにAlbum-Artworksをロードしています。 Main-Threadにロードできなかったので、私はスレッドを使用していて、混乱してしまいました。ビットマップの読み込みが正しく動作しません。RecyclerViewでスクロールしています

イメージが正しいサイズで読み込まれない場合や、茶色の四角形で表示されることがあります。これらの問題は、スクロールすると表示されます。私がゆっくりとスクロールすれば、それは機能します!私MusicStoreクラスの

重要な方法:

public Bitmap getAlbumArtwork(long AlbumID, int Height, int Width) { 
    ParcelFileDescriptor pfd; 

    Bitmap bCover = null; 
    BitmapFactory.Options bOptions = new BitmapFactory.Options(); 
    bOptions.inJustDecodeBounds = true; 

    try { 
     Uri ArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
     Uri uri = ContentUris.withAppendedId(ArtworkUri, AlbumID); 
     pfd = mContext.getContentResolver().openFileDescriptor(uri, "r"); 

     if (pfd != null) { 
      BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, bOptions); 

      bOptions.inSampleSize = calculateInSampleSize(bOptions, Width, Height); 

      bOptions.inJustDecodeBounds = false; 

      bCover = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, bOptions); 

      pfd.close(); 
     } 
    } 
    catch (IOException ioe) { 
     BitmapFactory.decodeResource(mContext.getResources(), R.drawable.standardartwork, bOptions); 

     bOptions.inSampleSize = calculateInSampleSize(bOptions, Width, Height); 

     bOptions.inJustDecodeBounds = false; 

     bCover = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.standardartwork, bOptions); 
    } 

    return bCover; 
} 

public void setAlbumArtwork(final long AlbumID, final ImageView ArtworkView) { 
    Thread thArtwork = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      final Bitmap bArtwork = getAlbumArtwork(AlbumID, ArtworkView.getHeight()/2, ArtworkView.getWidth()/2); 

      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        ArtworkView.setImageBitmap(bArtwork); 

        threadList.remove(Thread.currentThread()); 
       } 
      }, 50); 
     } 
    }); 

    threadList.add(thArtwork); 

    thArtwork.start(); 
} 

private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 

    int size = 1; 

    if (height > reqHeight && width > reqWidth) { 
     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     while ((halfHeight/size) > reqHeight && (halfWidth/size) > reqWidth) { 
      size *= 2; 
     } 
    } 

    return size; 
} 

そして、私のRecyclerViewAdapter:

public class SongRecyclerViewAdapter extends RecyclerView.Adapter<SongRecyclerViewAdapter.Holder> { 
    private Context mContext; 
    private Song[] sSongs; 

    private MusicStore musicStore; 

    public SongRecyclerViewAdapter(Context context, Song[] songs) { 
     mContext = context; 
     sSongs = songs; 

     musicStore = new MusicStore(mContext); 
    } 

    @Override 
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_songview, parent, false); 

     Holder holder = new Holder(view); 

     return holder; 
    } 

    @Override 
    public void onBindViewHolder(Holder holder, int position) { 
     musicStore.setAlbumArtwork(sSongs[position].getAlbumID(), holder.imvSong); 

     holder.txvSongTitle.setText(sSongs[position].getTitle()); 
     holder.txvSongInfo.setText(sSongs[position].getArtists()); 
    } 

    @Override 
    public void onViewDetachedFromWindow(Holder holder) { 

    } 

    @Override 
    public int getItemCount() { 
     return sSongs != null ? sSongs.length : 0; 
    } 

    public class Holder extends RecyclerView.ViewHolder { 
     LinearLayout linearLayout; 
     ImageView imvSong; 
     TextView txvSongTitle; 
     TextView txvSongInfo; 

     public Holder(View layout) { 
      super(layout); 

      linearLayout = (LinearLayout) layout; 

      imvSong = (ImageView) layout.findViewById(R.id.imvSong); 
      txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle); 
      txvSongInfo = (TextView) layout.findViewById(R.id.adap_txvSongInfo); 
     } 
    } 
} 

私は正しくビットマップをロードするために、他のideadのために絶対に開いています!

あなたがしたい場合は、それを試してみることができます: https://play.google.com/store/apps/details?id=at.guger.musixs

感謝を!

+0

実際にビットマップを読み込むには、PicassoまたはGlideを使用します。ライブラリはキャッシングを行い、あなたの記憶を爆破することはありません。 – toidiu

+0

どこで入手でき、どのように使用するのですか? –

答えて

0

http://square.github.io/picasso/

セットアップおよびドキュメントは非常に簡単です。 Uriを渡すこともできます。ピカソはそれを解決します。

Picasso.with(this).load(uri).into(imageView); 

あなたはまた、幅と高さ、プレースホルダ、および大いに多くのようなものを指定することができます。

.resize(width, height).placeholder(placeholderImg) 
+0

これをスレッドに入れなければならないのですか、これはメインのスレッドとは独立していますか? –

+0

ピカソはこれをすべて別のスレッドで処理します:) – toidiu

関連する問題