android - viewpager 查看大图卡顿
ringa_lee
ringa_lee 2017-04-18 09:14:30
0
2
511

我是用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;
        }
    }
    
    
    

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

ringa_lee
ringa_lee

ringa_lee

全部回覆(2)
小葫芦

大圖要壓縮,例如圖片是2000X2000以上的圖片,但是整個手機螢幕都沒那麼大,就導致bitmap很大了,在需要放大的時候才局部加載。另外viewpager還要考慮緩存,就是加載多少頁緩存,viewpager的PagerAdapter有多種,而且可以設置,如果不當,你設置個100頁,可能都一次加載了,就可能很卡了。

根據你得程式碼滑動的時候加載的是網頁圖片,不知道你這邊怎麼處理的,推薦使用Picasso,或者glide來處理。

伊谢尔伦

沒有看出哪裡卡頓, 建議加個BlockCanary檢查一下, 針對性解決

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板