Java tutorial
/******************************************************************************* * Copyright 2011-2013 Sergey Tarasevich * * 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 com.fd.colac.activity.picture; import com.fd.colac.R; import com.fd.colac.customview.scaleimage.PhotoView; import com.fd.colac.customview.scaleimage.PhotoViewAttacher.OnPhotoTapListener; import com.fd.colac.customview.scaleimage.PhotoViewAttacher.OnViewTapListener; import com.fd.colac.customview.scaleimage.pager.CirclePageIndicator; import com.fd.colac.customview.scaleimage.pager.HackyViewPager; import com.fd.colac.customview.scaleimage.pager.PageIndicator; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import com.nostra13.universalimageloader.core.assist.QueueProcessingType; import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra13.universalimageloader.core.assist.FailReason; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.Toast; /** * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) */ public class ImagePagerActivity extends Activity { protected ImageLoader imageLoader = ImageLoader.getInstance(); DisplayImageOptions options; private static final String STATE_POSITION = "STATE_POSITION"; public static final String IMAGES = "images"; public static final String IMAGE_POSITION = "image_index"; HackyViewPager pager; PageIndicator mIndicator; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_pager); initImageLoader(this); Bundle bundle = getIntent().getExtras(); String[] imageUrls = bundle.getStringArray(IMAGES); int pagerPosition = bundle.getInt(IMAGE_POSITION, 0); if (savedInstanceState != null) { pagerPosition = savedInstanceState.getInt(STATE_POSITION); } options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error).resetViewBeforeLoading(true).cacheOnDisc(true) .imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.ARGB_8888) .displayer(new FadeInBitmapDisplayer(300)).build(); pager = (HackyViewPager) findViewById(R.id.pager); pager.setAdapter(new ImagePagerAdapter(imageUrls, this)); pager.setCurrentItem(pagerPosition); mIndicator = (CirclePageIndicator) findViewById(R.id.indicator); mIndicator.setViewPager(pager); } public void initImageLoader(Context context) { // This configuration tuning is custom. You can tune every option, you may tune some of them, // or you can create default configuration by // ImageLoaderConfiguration.createDefault(this); // method. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove for release app .build(); // Initialize ImageLoader with configuration. ImageLoader.getInstance().init(config); } @Override public void onSaveInstanceState(Bundle outState) { outState.putInt(STATE_POSITION, pager.getCurrentItem()); } private class ImagePagerAdapter extends PagerAdapter { private String[] images; private LayoutInflater inflater; private Context mContext; ImagePagerAdapter(String[] images, Context context) { this.images = images; this.mContext = context; inflater = getLayoutInflater(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((View) object); } @Override public void finishUpdate(View container) { } @Override public int getCount() { return images.length; } @Override public Object instantiateItem(ViewGroup view, int position) { View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false); PhotoView imageView = (PhotoView) imageLayout.findViewById(R.id.image); imageView.setOnViewTapListener(new OnViewTapListener() {// @Override public void onViewTap(View view, float x, float y) { finish(); } }); imageView.setOnPhotoTapListener(new OnPhotoTapListener() {// @Override public void onPhotoTap(View view, float x, float y) { finish(); } }); final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading); imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { spinner.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { String message = null; switch (failReason.getType()) { case IO_ERROR: message = "Input/Output error"; break; case DECODING_ERROR: message = "Image can't be decoded"; break; case NETWORK_DENIED: message = "Downloads are denied"; break; case OUT_OF_MEMORY: message = "Out Of Memory error"; break; case UNKNOWN: message = "Unknown error"; break; } Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show(); spinner.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { spinner.setVisibility(View.GONE); } }); ((ViewPager) view).addView(imageLayout, 0); return imageLayout; } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); } @Override public void restoreState(Parcelable state, ClassLoader loader) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View container) { } } }