当查看大图时要注意点击哪张图就加载哪张图
-
在activity通过viewpager的滑动监听来实现
//选择进入的位置 pager.setCurrentItem(imgPostion); pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //在适配器里写个方法来实现滑动加载即时图的效果 adapter.imgPostion(position); } @Override public void onPageScrollStateChanged(int state) { } });
2.适配器
public class ImgInfoAdapter extends PagerAdapter {
private static int ifClean=0;
protected Context context;
private static ArrayList<String> list;
private View view;
private PhotoView photoView;
static int star;
private ProgressImageView progressImageView;
static HashMap<Integer,ImageView> Imap = new HashMap<Integer,ImageView>();
static HashMap<Integer,ProgressImageView> Imappr = new HashMap<Integer,ProgressImageView>();
static HashMap<Integer,ProgressBar> Imapor = new HashMap<Integer,ProgressBar>();
public ImgInfoAdapter(Context context, ArrayList<String> list,int star){
this.context = context;
this.list = list;
this.star=star;
}
public void imgPostion(int star1){
newPhoto(star1);
star=star1;
}
public Find find;
public interface Find{
void itemFind();
void itemXiaZai(String url,View view);
}
public void setFind(Find find){
this.find=find;
}
@Override
public int getCount() {
return list.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_imginfo,null);
photoView = (PhotoView) view.findViewById(R.id.img);
progressImageView = (ProgressImageView) view.findViewById(R.id.progress_imageview);
ProgressBar progressBar=view.findViewById(R.id.progress);
photoView.enable();
progressBar = (ProgressBar)view.findViewById(R.id.progress);
Imap.put(position,photoView);
Imappr.put(position,progressImageView);
Imapor.put(position,progressBar);
//判断第一次显示视图
if (star==position){
if (list.get(position).substring(list.get(position).length()-3,list.get(position).length()).equals("gif")){
try {
Glide.with(context)
.using(new ProgressModelLoader(new ProgressHandler(context,progressImageView )))
.load(BaseMeUrl.BigImgUrl + list.get(position))
.asGif()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(0.1f)
.fitCenter()
.into(Imap.get(position));
}catch (OutOfMemoryError e){
//Log.e("OOM的位置","加载GIF图OOM="+e.toString());
}
}else {
try {
Glide.with(context)
.using(new ProgressModelLoader(new ProgressHandler(context,progressImageView )))
.load(BaseMeUrl.BigImgUrl + list.get(star))
.asBitmap()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.into(Imap.get(star));
}catch (OutOfMemoryError e){
//Log.e("OOM的位置","加载PNG图OOM"+e.toString());
}
}
}
photoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
find.itemFind();
}
});
photoView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
find.itemXiaZai(BaseAPIManager.BASE_BIG_IMG_URL+list.get(position),view);
return true;
}
});
view.findViewById(R.id.imaginfo_re).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
find.itemFind();
}
});
photoView=null;
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
public void newPhoto(final int starPostion){
//判断自己滑动的位置
if (list.get(starPostion).substring(list.get(starPostion).length()-3,list.get(starPostion).length()).equals("gif")){
try {
Glide.with(context)
.using(new ProgressModelLoader(new ProgressHandler(context, Imappr.get(starPostion))))
.load(BaseAPIManager.BASE_BIG_IMG_URL+list.get(starPostion))
.asGif()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.into(Imap.get(starPostion));
}catch (OutOfMemoryError e){
//Log.e("OOM的位置","加载GIF图OOM"+e.toString());
}
}else {
try {
Glide.with(context)
.using(new ProgressModelLoader(new ProgressHandler(context,Imappr.get(starPostion))))
.load(BaseMeUrl.BigImgUrl + list.get(starPostion))
.asBitmap()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.into(Imap.get(starPostion));
}catch (OutOfMemoryError e){
//Log.e("OOM的位置","加载GIF图OOM"+e.toString());
}
}
if (ifClean!=star){
Glide.clear(Imap.get(star));
ifClean=starPostion;
}
}
//加载进度条的显示
private static class ProgressHandler extends Handler {
private final WeakReference<Context> mActivity;
private final ProgressImageView mProgressImageView;
public ProgressHandler(Context activity, ProgressImageView progressImageView) {
super(Looper.getMainLooper());
mActivity = new WeakReference<>(activity);
mProgressImageView = progressImageView;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
final Context activity = mActivity.get();
if (activity != null) {
switch (msg.what) {
case 1:
int percent = msg.arg1*100/msg.arg2;
try {
Imapor.get(star).setVisibility(View.VISIBLE);
}catch (Exception e){
}
mProgressImageView.setProgress(percent);
if (percent==100){
if (Imappr.get(star).getVisibility()==View.VISIBLE){
try {
Imappr.get(star).setVisibility(View.GONE);
}catch (Exception e){
}
}
Imapor.get(star).setVisibility(View.GONE);
}
break;
default:
break;
}
}
}
}
//页面的清理,必须有,因为当图片过大时系统就会OOM,这个就是为了防止OOM的
public void cleanMap(){
for (int i = 0; i < Imap.size(); i++) {
if (Imap.get(i)!=null){
if (list.get(i).substring(list.get(i).length()-3,list.get(i).length()).equals("gif")){
Glide.clear(Imap.get(i));
Imap.get(i).setImageDrawable(null);
}else {
Glide.clear(Imap.get(i));
BitmapDrawable bitmapDrawable = (BitmapDrawable) Imap.get(i).getDrawable();
if (bitmapDrawable != null) {
Bitmap bm = bitmapDrawable.getBitmap();
if (bm!=null && !bm.isRecycled()) {
Imap.get(i).setImageResource(0);
bm.recycle();
}
}
}
}
}
Imap.clear();
Imappr.clear();
Imapor.clear();
}
}
网友评论