/*******************************************************************************
 * Copyright 2011, 2012 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.senab.photoview;

import uk.co.senab.photoview.PhotoViewAttacher.OnMatrixChangedListener;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
import uk.co.senab.photoview.PhotoViewAttacher.OnViewTapListener;
import android.content.Context;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;

public class PhotoView extends ImageView {

	private final PhotoViewAttacher mAttacher;

	public PhotoView(Context context) {
		super(context);
		super.setScaleType(ScaleType.MATRIX);
		mAttacher = new PhotoViewAttacher(this);
	}

	public PhotoView(Context context, AttributeSet attr) {
		super(context, attr);
		super.setScaleType(ScaleType.MATRIX);
		mAttacher = new PhotoViewAttacher(this);
	}
	
	public PhotoView(Context context, AttributeSet attr, int defStyle) {
		super(context, attr, defStyle);
		super.setScaleType(ScaleType.MATRIX);
		mAttacher = new PhotoViewAttacher(this);
	}

	/**
	 * Returns true if the PhotoView is set to allow zooming of Photos.
	 * 
	 * @return true if the PhotoView allows zooming.
	 */
	public boolean canZoom() {
		return mAttacher.canZoom();
	}

	/**
	 * Gets the Display Rectangle of the currently displayed Drawable. The
	 * Rectangle is relative to this View and includes all scaling and
	 * translations.
	 * 
	 * @return - RectF of Displayed Drawable
	 */
	public RectF getDisplayRect() {
		return mAttacher.getDisplayRect();
	}

	/**
	 * Returns the current scale value
	 * 
	 * @return float - current scale value
	 */
	public float getScale() {
		return mAttacher.getScale();
	}

	@Override
	public ScaleType getScaleType() {
		return mAttacher.getScaleType();
	}

	@Override
	// setImageBitmap calls through to this method
	public void setImageDrawable(Drawable drawable) {
		super.setImageDrawable(drawable);
		if (null != mAttacher) {
			mAttacher.update();
		}
	}

	@Override
	public void setImageResource(int resId) {
		super.setImageResource(resId);
		if (null != mAttacher) {
			mAttacher.update();
		}
	}

	@Override
	public void setImageURI(Uri uri) {
		super.setImageURI(uri);
		if (null != mAttacher) {
			mAttacher.update();
		}
	}

	/**
	 * Register a callback to be invoked when the Matrix has changed for this
	 * View. An example would be the user panning or scaling the Photo.
	 * 
	 * @param listener
	 *            - Listener to be registered.
	 */
	public void setOnMatrixChangeListener(OnMatrixChangedListener listener) {
		mAttacher.setOnMatrixChangeListener(listener);
	}

	@Override
	public void setOnLongClickListener(OnLongClickListener l) {
		// delegate to the attacher
		mAttacher.setOnLongClickListener(l);
	}

	/**
	 * Register a callback to be invoked when the Photo displayed by this View
	 * is tapped with a single tap.
	 * 
	 * @param listener
	 *            - Listener to be registered.
	 */
	public void setOnPhotoTapListener(OnPhotoTapListener listener) {
		mAttacher.setOnPhotoTapListener(listener);
	}

	/**
	 * Register a callback to be invoked when the View
	 * is tapped with a single tap.
	 * 
	 * @param listener
	 *            - Listener to be registered.
	 */
	public void setOnViewTapListener(OnViewTapListener listener)
	{
		mAttacher.setOnPhotoTapListener(listener);
	}

	@Override
	public void setScaleType(ScaleType scaleType) {
		// setScaleType can be called from within ImageView constructor if there is a scaleType attribute set in a PhotoView embedded in a layout.
		// Avoid NPE in that situation, we're overriding the scaletype to MATRIX on creation anyway so we could log a warning that the resource definition is
		// unused.
		if (null != mAttacher) {
			mAttacher.setScaleType(scaleType);
		}
	}

	/**
	 * Allows you to enable/disable the zoom functionality on the ImageView.
	 * When disable the ImageView reverts to using the FIT_CENTER matrix.
	 * 
	 * @param zoomable
	 *            - Whether the zoom functionality is enabled.
	 */
	public void setZoomable(boolean zoomable) {
		mAttacher.setZoomable(zoomable);
	}

	/**
	 * Zooms to the specified scale, around the focal point given.
	 * 
	 * @param scale
	 *            - Scale to zoom to
	 * @param focalX
	 *            - X Focus Point
	 * @param focalY
	 *            - Y Focus Point
	 */
	public void zoomTo(float scale, float focalX, float focalY) {
		mAttacher.zoomTo(scale, focalX, focalY);
	}

	@Override
	protected void onDetachedFromWindow() {
		mAttacher.cleanup();
		super.onDetachedFromWindow();
	}

}
@@ -1271,16 +1271,55 @@
 acher;%0A%0A
+%09private ScaleType mPendingScaleType;%0A%0A
 %09public 
@@ -1349,29 +1349,28 @@
 xt) %7B%0A%09%09
-super
+this
 (context
 );%0A%09%09sup
@@ -1365,91 +1365,14 @@
 text
-);%0A%09%09super.setScaleType(ScaleType.MATRIX);%0A%09%09mAttacher = new PhotoViewAttacher(this
+, null
 );%0A%09
@@ -1428,29 +1428,28 @@
 t attr) %7B%0A%09%09
-super
+this
 (context, at
@@ -1454,91 +1454,11 @@
 attr
-);%0A%09%09super.setScaleType(ScaleType.MATRIX);%0A%09%09mAttacher = new PhotoViewAttacher(this
+, 0
 );%0A%09
@@ -1647,16 +1647,121 @@
 r(this);
+%0A%0A%09%09if (null != mPendingScaleType) %7B%0A%09%09%09setScaleType(mPendingScaleType);%0A%09%09%09mPendingScaleType = null;%0A%09%09%7D
 %0A%09%7D%0A%0A%09/*
@@ -4101,314 +4101,8 @@
 ) %7B%0A
-%09%09// setScaleType can be called from within ImageView constructor if there is a scaleType attribute set in a PhotoView embedded in a layout.%0A%09%09// Avoid NPE in that situation, we're overriding the scaletype to MATRIX on creation anyway so we could log a warning that the resource definition is%0A%09%09// unused.%0A
 %09%09if
@@ -4162,16 +4162,61 @@
 eType);%0A
+%09%09%7D else %7B%0A%09%09%09mPendingScaleType = scaleType;%0A
 %09%09%7D%0A%09%7D%0A%0A

Properly fix #25