我是用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;
}
}
现在发现滑动的时候卡顿这是什么原因的大大们 ,求帮忙。谢谢!
大图要压缩,比如图片是2000X2000以上的图片,但是整个手机屏幕都没那么大,就导致bitmap很大了,在需要放大的时候才局部加载。另外viewpager还得考虑缓存,就是加载多少页缓存,viewpager的PagerAdapter有多种,而且可以设置,如果不当,你设置个100页,可能都一次加载了,就可能很卡了。
根据你得代码滑动的时候加载的是网络图片,不知道你这边怎么处理的,推荐使用Picasso,或者glide来处理。
没有看出哪里卡顿, 建议加个BlockCanary检查一下, 针对性解决