使用ViewPager延迟模糊和圆形图像

时间:2017-04-24 05:40:18

标签: android android-viewpager blur roundedbitmapdrawable

  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);
            }


        }
    }

我必须将模糊的图像显示为带有圆形图像的背景。我已经像上面那样做了。当我滑动viewpager时,它会滞后。我用滑动来显示images.pls任何人都给我一个soln.Thanks提前

1 个答案:

答案 0 :(得分:1)

模糊是一个CPU密集型过程。由于viewpager为相邻的视图创建视图,因此处理和创建延迟需要花费太多时间。特别是在较旧的设备和较少的RAM中,您将面临更多问题。

对于圆形图像,如果需要使用圆形图像,请使用CircleImageView,否则仅使用圆角,可以使用this