private class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private ImageView displayArt;
private ImageView songImage;
private TextView playerName;
private CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return songList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.adapter_audio_player, container, false);
FieldIntialization(itemView);
updatePlayerView(position);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
private void FieldIntialization(View view) {
displayArt = (ImageView) view.findViewById(R.id.songImage);
songImage = (ImageView) view.findViewById(R.id.songDisplayArt);
playerName = (TextView) view.findViewById(R.id.songName);
}
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
private void updatePlayerView(int songIndex) {
playerName.setText(songList.get(songIndex).getPlayerTitle());
displayArt.setScaleType(ImageView.ScaleType.FIT_XY);
final String contentURI = "content://media/external/audio/media/" + songList.get(songIndex).getPlayerId() + "/albumart";
Glide.with(getApplicationContext()).load(contentURI).dontAnimate().
error(R.drawable.music_player_background)
.bitmapTransform(new BlurTransformation(getApplicationContext()))
.into(displayArt);
songImage.setScaleType(ImageView.ScaleType.FIT_XY);
Bitmap originalBitmap = null;
try {
originalBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Constants.getDefaultAlbumUri(
String.valueOf(songList.get(songIndex).getPlayerAlbumId())
));
} catch (IOException e) {
e.printStackTrace();
}
if ((originalBitmap == null || originalBitmap.equals(""))) {
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo);
RoundedBitmapDrawable roundDrawable = RoundedBitmapDrawableFactory.create(getResources(), largeIcon);
roundDrawable.setCircular(true);
songImage.setImageDrawable(roundDrawable);
} else {
RoundedBitmapDrawable roundDrawable = RoundedBitmapDrawableFactory.create(getResources(), originalBitmap);
roundDrawable.setCircular(true);
songImage.setImageDrawable(roundDrawable);
}
}
}
我必须将模糊的图像显示为背景,并用圆形图像显示。
模糊是CPU密集型过程。当ViewPager为相邻的视图创建视图时,它花费了太多时间来处理和创建滞后。特别是在较旧的DEIVCES和RAM较少的情况下,您将面临更多问题。
对于圆形图像,如果您需要使用圆形图像使用CircleImageView,否则仅适用于圆角,则可以使用此