/** * 获取默认专辑图片 */ public static Bitmap getDefaultArtwork(Context context,boolean small) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.RGB_565; if(small){ //返回小图片 return BitmapFactory.decodeStream(context.getResources().openRawResource(R.mipmap.music), null, opts); } return BitmapFactory.decodeStream(context.getResources().openRawResource(R.mipmap.music), null, opts); } /** * 从文件当中获取专辑封面位图 */ private static Bitmap getArtworkFromFile(Context context, long songid, long albumid){ Bitmap bm = null; if(albumid < 0 && songid < 0) { throw new IllegalArgumentException("Must specify an album or a song id"); } try { BitmapFactory.Options options = new BitmapFactory.Options(); FileDescriptor fd = null; if(albumid < 0){ Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart"); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); if(pfd != null) { fd = pfd.getFileDescriptor(); } } else { Uri uri = ContentUris.withAppendedId(albumArtUri, albumid); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); if(pfd != null) { fd = pfd.getFileDescriptor(); } } options.inSampleSize = 1; // 只进行大小判断 options.inJustDecodeBounds = true; // 调用此方法得到options得到图片大小 BitmapFactory.decodeFileDescriptor(fd, null, options); // 我们的目标是在800pixel的画面上显示 // 所以需要调用computeSampleSize得到图片缩放的比例 options.inSampleSize = 100; // 我们得到了缩放的比例,现在开始正式读入Bitmap数据 options.inJustDecodeBounds = false; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; //根据options参数,减少所需要的内存 bm = BitmapFactory.decodeFileDescriptor(fd, null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } return bm; } /** * 获取专辑封面位图对象 */ public static Bitmap getArtwork(Context context, long song_id, long album_id, boolean allowdefalut, boolean small){ if(album_id < 0) { if(song_id < 0) { Bitmap bm = getArtworkFromFile(context, song_id, -1); if(bm != null) { return bm; } } if(allowdefalut) { return getDefaultArtwork(context, small); } return null; } ContentResolver res = context.getContentResolver(); Uri uri = ContentUris.withAppendedId(albumArtUri, album_id); if(uri != null) { InputStream in = null; try { in = res.openInputStream(uri); BitmapFactory.Options options = new BitmapFactory.Options(); //先制定原始大小 options.inSampleSize = 1; //只进行大小判断 options.inJustDecodeBounds = true; //调用此方法得到options得到图片的大小 BitmapFactory.decodeStream(in, null, options); /** 我们的目标是在你N pixel的画面上显示。 所以需要调用computeSampleSize得到图片缩放的比例 **/ /** 这里的target为800是根据默认专辑图片大小决定的,800只是测试数字但是试验后发现完美的结合 **/ if(small){ options.inSampleSize = computeSampleSize(options, 40); } else{ options.inSampleSize = computeSampleSize(options, 600); } // 我们得到了缩放比例,现在开始正式读入Bitmap数据 options.inJustDecodeBounds = false; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; in = res.openInputStream(uri); return BitmapFactory.decodeStream(in, null, options); } catch (FileNotFoundException e) { Bitmap bm = getArtworkFromFile(context, song_id, album_id); if(bm != null) { if(bm.getConfig() == null) { bm = bm.copy(Bitmap.Config.RGB_565, false); if(bm == null && allowdefalut) { return getDefaultArtwork(context, small); } } } else if(allowdefalut) { bm = getDefaultArtwork(context, small); } return bm; } finally { try { if(in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 对图片进行合适的缩放 */ public static int computeSampleSize(Options options, int target) { int w = options.outWidth; int h = options.outHeight; int candidateW = w / target; int candidateH = h / target; int candidate = Math.max(candidateW, candidateH); if(candidate == 0) { return 1; } if(candidate > 1) { if((w > target) && (w / candidate) < target) { candidate -= 1; } } if(candidate > 1) { if((h > target) && (h / candidate) < target) { candidate -= 1; } } return candidate; }
//回调播放状态下的UI设置 public void changeUIStatusOnPlay(int position){//更新UI状态--当前播放的音乐,传进来当前音乐的下标 if (position>=0 && position<mp3Infos.size()){ Mp3Info mp3Info = mp3Infos.get(position);//获取音乐信息 textView_songName.setText(mp3Info.getTitle());//设置歌名 textView2_singer.setText(mp3Info.getArtist());//设置歌手名 //imageView2_play_pause.setImageResource(R.mipmap.player_btn_pusr_normal);//更新播放暂停图片 //获取专辑封面图片 Bitmap albumBitmap = MediaUtils.getArtwork(mainActivity,mp3Info.getId(),mp3Info.getAlbumId(),true,true); //改变播放界面专辑封面图片 imageView_album.setImageBitmap(albumBitmap); } }
package com.iwanghang.drmplayer.adapter; import android.content.Context; import android.graphics.Bitmap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.iwanghang.drmplayer.MyMusicListFragment; import com.iwanghang.drmplayer.R; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import java.util.ArrayList; /** * 自定义的音乐列表适配器 * 为了方便扩展,因为之前没有考虑到显示专辑封面 * Created by iwanghang on 16/4/16. */ public class MyMusicListAdapter extends BaseAdapter{ private Context ctx; //上下文对象引用 private ArrayList<Mp3Info> mp3Infos;//存放Mp3Info引用的集合 private Mp3Info mp3Info; //Mp3Info对象引用 private int pos = -1; //列表位置 /** * 构造函数 * @param ctx 上下文 * @param mp3Infos 集合对象 */ public MyMusicListAdapter(Context ctx, ArrayList<Mp3Info> mp3Infos){ this.ctx = ctx; this.mp3Infos = mp3Infos; System.out.println("MyMusicListAdapter.java #0 : ctx = " + ctx + ",mp3Infos = " + mp3Infos.size()); } public ArrayList<Mp3Info> getMp3Infos() { System.out.println("MyMusicListAdapter.java #1 : public ArrayList<Mp3Info> getMp3Infos() {"); return mp3Infos; } public void setMp3Infos(ArrayList<Mp3Info> mp3Infos) { System.out.println("MyMusicListAdapter.java #2 : public void setMp3Infos(ArrayList<Mp3Info> mp3Infos) {"); this.mp3Infos = mp3Infos; } @Override public int getCount() { //System.out.println("MyMusicListAdapter.java #3 : public int getCount() {" + mp3Infos.size()); //return mp3Infos.size(); return mp3Infos.size(); } @Override public Object getItem(int position) { System.out.println("MyMusicListAdapter.java #4 : public Object getItem(int position) {"); return mp3Infos.get(position); } @Override public long getItemId(int position) { //System.out.println("MyMusicListAdapter.java #5 : public long getItemId(int position) {"); return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //System.out.println("MyMusicListAdapter.java #6 : public View getView "); ViewHolder vh; if(convertView==null){ //vh = new ViewHolder(); convertView = LayoutInflater.from(ctx).inflate(R.layout.item_music_list,null); vh = new ViewHolder(); vh.textView1_title = (TextView) convertView.findViewById(R.id.textView1_title); vh.textView2_singer = (TextView) convertView.findViewById(R.id.textView2_singer); vh.textView3_time = (TextView) convertView.findViewById(R.id.textView3_time); vh.imageView1_ablum = (ImageView) convertView.findViewById(R.id.imageView1_ablum); //System.out.println("MyMusicListAdapter.java #7 : textView1_title = " + vh.textView1_title); convertView.setTag(vh);//表示给View添加一个格外的数据, }else { vh = (ViewHolder)convertView.getTag();//通过getTag的方法将数据取出来 } Mp3Info mp3Info = mp3Infos.get(position); vh.textView1_title.setText(mp3Info.getTitle());//显示标题 vh.textView2_singer.setText(mp3Info.getArtist());//显示歌手 vh.textView3_time.setText(MediaUtils.formatTime(mp3Info.getDuration()));//显示时长 //获取专辑封面图片 Bitmap albumBitmapItem = MediaUtils.getArtwork(ctx,mp3Info.getId(),mp3Info.getAlbumId(),true,true); System.out.println("MyMusicListAdapter.java #8 : albumBitmapItem = " + albumBitmapItem.getConfig()); //改变播放界面专辑封面图片 vh.imageView1_ablum.setImageBitmap(albumBitmapItem); //vh.imageView1_ablum.setImageResource(R.mipmap.music); return convertView; } /** * 定义一个内部类 * 声明相应的控件引用 */ static class ViewHolder{ //所有控件对象引用 TextView textView1_title;//标题 TextView textView2_singer;//歌手 TextView textView3_time;//时长 ImageView imageView1_ablum;//专辑封面图片 } } //inflate(R.layout.item_music_list,null);