viewpager 查看大图卡顿

发布于 2022-09-04 15:36:25 字数 5310 浏览 36 评论 0

我是用viewpager做的滑动查看大图的功能

static class SamplePagerAdapter extends PagerAdapter {
        WeakReference<PreviewImgFragment> mActivity;
        PreviewImgFragment theActivity;
        AlertDialog.Builder saveBuilder;
        AlertDialog.Builder warningBuilder;
        Bitmap bitmap;

        public SamplePagerAdapter(PreviewImgFragment activity) {
            mActivity = new WeakReference<>(activity);
            theActivity = mActivity.get();
            saveBuilder = new AlertDialog.Builder(theActivity.activity);
            saveBuilder.setMessage("保存图片");
            saveBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    AppUtils.saveImageToGallery(bitmap);
                    ToastUtil.showToast("保存成功");
                }
            });
            warningBuilder = new AlertDialog.Builder(theActivity.activity);
            warningBuilder.setMessage("图片还没有准备好!");
            warningBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            saveBuilder.setOnDismissListener(onDismissListener);
            warningBuilder.setOnDismissListener(onDismissListener);
        }

        private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (null != cacheView) {
                    cacheView.setEnabled(true);
                }
            }
        };

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
            onLongClickListener = null;
        }

        private View cacheView = null;
        View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                cacheView = v;
                v.setEnabled(false);
                Uri uri = (Uri) v.getTag();
                bitmap = AppUtils.returnBitmap(uri);
                if (null != bitmap) {
                    Log.i("bitmap", bitmap.getHeight() + "*********" + bitmap.getWidth() + "");
                    saveBuilder.show();
                } else {
                    warningBuilder.show();
                }
                return true;
            }
        };

        @Override
        public int getCount() {
            return theActivity.list == null ? 1 : theActivity.list.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            Log.e("instantiateItem","instantiateItem");
            View convertView = View.inflate(SmartApplication.application, R.layout.loading_layout, null);
            PhotoDraweeView photo = (PhotoDraweeView) convertView.findViewById(R.id.each_photo);
            Uri uri = null;
            if (theActivity.list != null) {
                String url = AppUtils.getImg(theActivity.list.get(position) instanceof ImageInfo ? ((ImageInfo) theActivity.list.get(position)).getBigImageUrl() : (String) theActivity.list.get(position));
                if (url.startsWith("http")) {
                    uri = Uri.parse(url);
                } else {
                    uri = (new Uri.Builder())
                            .scheme("file")
                            .path(url)
                            .build();
                }
            } else {
                if (theActivity.img.startsWith("http")) {
                    uri = Uri.parse(theActivity.img);
                } else {
                    uri = (new Uri.Builder())
                            .scheme("file")
                            .path(AppUtils.getImg(theActivity.img))
                            .build();
                }
            }
            GenericDraweeHierarchy hierarchy = photo.getHierarchy();
            if (null == photo.getTag()) {
                hierarchy.setProgressBarImage(new CustomProgressBar(photo));
            } else {
//                Log.e("previewLLL", photo.getTag().toString());
                hierarchy = null;
            }
            photo.setPhotoUri(uri);
            photo.setOnLongClickListener(onLongClickListener);
            photo.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    theActivity.back.callOnClick();
                }
            });
            container.addView(convertView);
            return convertView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            Log.e("destroyItem","destroyItem");
            View contentView = (View) object;
            container.removeView(contentView);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
    
    
    

现在发现滑动的时候卡顿这是什么原因的大大们 ,求帮忙。谢谢!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2022-09-11 15:36:25

大图要压缩,比如图片是2000X2000以上的图片,但是整个手机屏幕都没那么大,就导致bitmap很大了,在需要放大的时候才局部加载。另外viewpager还得考虑缓存,就是加载多少页缓存,viewpager的PagerAdapter有多种,而且可以设置,如果不当,你设置个100页,可能都一次加载了,就可能很卡了。

根据你得代码滑动的时候加载的是网络图片,不知道你这边怎么处理的,推荐使用Picasso,或者glide来处理。

千と千尋 2022-09-11 15:36:25

没有看出哪里卡顿, 建议加个BlockCanary检查一下, 针对性解决

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文