/******************************************************************************* * Copyright (c) 2013 Chris Banes. * * 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 uk.co.blatech.blaupload.ui; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.support.v4.view.PagerAdapter; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView.ScaleType; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; import com.squareup.picasso.Picasso; import com.squareup.picasso.Target; import java.io.IOException; import java.util.ArrayList; import uk.co.blatech.blaupload.R; public class ImagePagerAdapter extends PagerAdapter { private final ArrayList mUrls; private final Context mContext; private float[] mWidths; private float[] mHeights; private int position; private ViewGroup container; private TouchImageView imageView; private TouchImageView zoomImageView; private ImageLoader imageLoader; private ZoomViewPager mPager; private ArrayList filenames; public ImagePagerAdapter(Context context, ArrayList urls) { mUrls = urls; mContext = context; mWidths = new float[mUrls.size()]; mHeights = new float[mUrls.size()]; filenames = new ArrayList(); } private Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { zoomImageView.setImageBitmap(bitmap); zoomImageView.setVisibility(View.VISIBLE); imageView.setVisibility(View.GONE); } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }; @Override public int getCount() { return null != mUrls ? mUrls.size() : 0; } @Override public View instantiateItem(ViewGroup con, int pos) { container = con; position = pos; LayoutInflater inflater = (LayoutInflater) container.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.image_pager_item, container, false); imageView = (TouchImageView) view.findViewById(R.id.imageView); zoomImageView = (TouchImageView) view.findViewById(R.id.zoomImageView); final String url = mUrls.get(position); String[] parts = url.split("/"); final String filename = parts[parts.length-1]; imageLoader = ImageLoader.getInstance(); imageLoader.displayImage(url, imageView); imageView.setScaleType(ScaleType.FIT_CENTER); zoomImageView.setScaleType(ScaleType.FIT_CENTER); container.refreshDrawableState(); imageView.setTag("img"+pos); zoomImageView.setTag("zoom"+pos); mWidths[position] = 0; mHeights[position] = 0; filenames.add(pos, filename); mPager = (ZoomViewPager) container.findViewById(R.id.imagePager); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { final TouchImageView thisImage = (TouchImageView) container.findViewWithTag("img" + mPager.getCurrentItem()); final TouchImageView zoomImage = (TouchImageView) container.findViewWithTag("zoom" + mPager.getCurrentItem()); if (thisImage.getCurrentZoom() > 1.0) { imageLoader.displayImage("http://www.blaupload.co.uk/"+ filenames.get(mPager.getCurrentItem()), zoomImage, new SimpleImageLoadingListener() { // @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { zoomImage.setZoom(thisImage.getCurrentZoom()); zoomImage.setVisibility(View.VISIBLE); thisImage.setVisibility(View.GONE); } }); } Log.d(ImagePagerAdapter.class.toString(), "Initial width: "+ mWidths[position] +", height: "+ mHeights[position]); Log.d(ImagePagerAdapter.class.toString(), "Current width: " + thisImage.getMeasuredWidth() + ", height: " + thisImage.getMeasuredHeight()); Log.d(ImagePagerAdapter.class.toString(), "Other width: " + thisImage.getWidth() + ", height: " + thisImage.getHeight()); Log.d(ImagePagerAdapter.class.toString(), "Zoom: " + thisImage.getCurrentZoom()); return true; } }); container.addView(view, 0); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }