viewpager 查看大图卡顿
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网开户

viewpager 查看大图卡顿

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

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

        public SamplePagerAdapterPreviewImgFragment activity {
            mActivity = new WeakReference<>activity;
            theActivity = mActivity.get;
            saveBuilder = new AlertDialog.BuildertheActivity.activity;
            saveBuilder.setMessage"保存图片";
            saveBuilder.setPositiveButton"确定", new DialogInterface.OnClickListener {
                @Override
                public void onClickDialogInterface dialog, int which {
                    dialog.dismiss;
                    AppUtils.saveImageToGallerybitmap;
                    ToastUtil.showToast"保存成功";
                }
            };
            warningBuilder = new AlertDialog.BuildertheActivity.activity;
            warningBuilder.setMessage"图片还没有准备好!";
            warningBuilder.setPositiveButton"确定", new DialogInterface.OnClickListener {
                @Override
                public void onClickDialogInterface dialog, int which {
                    dialog.dismiss;
                }
            };
            saveBuilder.setOnDismissListeneronDismissListener;
            warningBuilder.setOnDismissListeneronDismissListener;
        }

        private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener {
            @Override
            public void onDismissDialogInterface dialog {
                if null != cacheView {
                    cacheView.setEnabledtrue;
                }
            }
        };

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

        private View cacheView = null;
        View.OnLongClickListener onLongClickListener = new View.OnLongClickListener {
            @Override
            public boolean onLongClickView v {
                cacheView = v;
                v.setEnabledfalse;
                Uri uri = Uri v.getTag;
                bitmap = AppUtils.returnBitmapuri;
                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 instantiateItemViewGroup container, int position {
            Log.e"instantiateItem","instantiateItem";
            View convertView = View.inflateSmartApplication.application, R.layout.loading_layout, null;
            PhotoDraweeView photo = PhotoDraweeView convertView.findViewByIdR.id.each_photo;
            Uri uri = null;
            if theActivity.list != null {
                String url = AppUtils.getImgtheActivity.list.getposition instanceof ImageInfo ? ImageInfo theActivity.list.getposition.getBigImageUrl : String theActivity.list.getposition;
                if url.startsWith"http" {
                    uri = Uri.parseurl;
                } else {
                    uri = new Uri.Builder
                            .scheme"file"
                            .pathurl
                            .build;
                }
            } else {
                if theActivity.img.startsWith"http" {
                    uri = Uri.parsetheActivity.img;
                } else {
                    uri = new Uri.Builder
                            .scheme"file"
                            .pathAppUtils.getImgtheActivity.img
                            .build;
                }
            }
            GenericDraweeHierarchy hierarchy = photo.getHierarchy;
            if null == photo.getTag {
                hierarchy.setProgressBarImagenew CustomProgressBarphoto;
            } else {
//                Log.e"previewLLL", photo.getTag.toString;
                hierarchy = null;
            }
            photo.setPhotoUriuri;
            photo.setOnLongClickListeneronLongClickListener;
            photo.setOnPhotoTapListenernew OnPhotoTapListener {
                @Override
                public void onPhotoTapView view, float x, float y {
                    theActivity.back.callOnClick;
                }
            };
            container.addViewconvertView;
            return convertView;
        }

        @Override
        public void destroyItemViewGroup container, int position, Object object {
            Log.e"destroyItem","destroyItem";
            View contentView = View object;
            container.removeViewcontentView;
        }

        @Override
        public boolean isViewFromObjectView view, Object object {
            return view == object;
        }
    }
    
    
    

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

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

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

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

广告位

澳门银河官网