Java tutorial
/* * Copyright 2017 huxizhijian * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.huxizhijian.hhcomicviewer.adapter; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import org.huxizhijian.hhcomicviewer.model.ComicChapter; import org.huxizhijian.hhcomicviewer.ui.entry.GalleryActivity; import org.huxizhijian.hhcomicviewer.utils.CommonUtils; import org.huxizhijian.hhcomicviewer.utils.Constants; import org.huxizhijian.hhcomicviewer.view.ZoomImageView; import org.huxizhijian.hhcomicviewer.view.listener.OnCenterTapListener; import org.huxizhijian.hhcomicviewer.view.listener.OnLeftOrRightTapListener; import org.huxizhijian.sdk.imageloader.ImageLoaderOptions; import org.huxizhijian.sdk.imageloader.listener.ImageLoaderManager; import java.io.File; import java.util.LinkedList; /** * GalleryViewPagerAdapter * Created by wei on 2017/1/21. */ public class GalleryViewPagerAdapter extends PagerAdapter { private GalleryActivity mContext; private ComicChapter mComicChapter; private LinkedList<ZoomImageView> mRecyclerImageViews; private boolean mLoadOnLineFullSizeImage; private OnCenterTapListener onCenterTapListener; private OnLeftOrRightTapListener onLeftOrRightTapListener; // private ImageLoaderManager mImageLoader = ImageLoaderOptions.getImageLoaderManager(); public GalleryViewPagerAdapter(GalleryActivity context, boolean loadOnLineFullSizeImage) { mContext = context; mLoadOnLineFullSizeImage = loadOnLineFullSizeImage; } public void setComicChapter(ComicChapter comicChapter) { mComicChapter = comicChapter; } public void setOnLeftOrRightTapListener(OnLeftOrRightTapListener onLeftOrRightTapListener) { this.onLeftOrRightTapListener = onLeftOrRightTapListener; } public void setOnCenterTapListener(OnCenterTapListener onCenterTapListener) { this.onCenterTapListener = onCenterTapListener; } @Override public Object instantiateItem(ViewGroup container, final int position) { ZoomImageView imageView = null; if (mRecyclerImageViews == null) { mRecyclerImageViews = new LinkedList<>(); } if (mRecyclerImageViews.size() > 0) { //?ImageView imageView = mRecyclerImageViews.removeFirst(); } else { //ImageView imageView = getImageView(); } //??imageView?? setImageViewRecycler(imageView, position); container.addView(imageView); return imageView; } private void setImageViewRecycler(final ZoomImageView imageView, int position) { if (mComicChapter != null && mComicChapter.getDownloadStatus() == Constants.DOWNLOAD_FINISHED) { // File file = new File(mComicChapter.getSavePath(), CommonUtils.getPageName(position)); if (file.exists()) { mImageLoader.displayGallery(mContext, file, imageView); } else { Toast.makeText(mContext, "?~", Toast.LENGTH_SHORT).show(); } } else { // if (mLoadOnLineFullSizeImage) { // if (mComicChapter != null) { mImageLoader.displayGalleryFull(mContext, mComicChapter.getPicList().get(position), imageView); } } else { //?? if (mComicChapter != null) { mImageLoader.displayGalleryFit(mContext, mComicChapter.getPicList().get(position), imageView); } } } } @Override public void destroyItem(ViewGroup container, int position, Object object) { ZoomImageView imageView = (ZoomImageView) object; // // imageView.setImageInCenter(); // imageView.setImageDrawable(null); imageView.setImageBitmap(null); releaseImageViewResource(imageView); //? container.removeView(imageView); //? mRecyclerImageViews.addLast(imageView); } private void releaseImageViewResource(ZoomImageView imageView) { if (imageView == null) return; Drawable drawable = imageView.getDrawable(); if (drawable != null && drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; } } System.gc(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getCount() { return mComicChapter.getPageCount(); } private ZoomImageView getImageView() { ZoomImageView imageView = new ZoomImageView(mContext); if (onCenterTapListener != null) { imageView.setOnCenterTapListener(onCenterTapListener); } if (onLeftOrRightTapListener != null) { imageView.setOnLeftOrRightTapListener(onLeftOrRightTapListener); } return imageView; } }