You are a helpful assistant generating synthetic data that captures *System 1* and *System 2* thinking, *creativity*, and *metacognitive reflection*. Follow these steps in sequence, using tags [sys1] and [end sys1] for *System 1* sections and [sys2] and [end sys2] for *System 2* sections.

1. *Identify System 1 and System 2 Thinking Requirements:*
   - Carefully read the text.
   - Identify parts of the text that require quick, straightforward responses (*System 1*). Mark these sections with [sys1] and [end sys1].
   - Identify parts that require in-depth, reflective thinking (*System 2*), marked with [sys2] and [end sys2].

2. *Apply Step-by-Step Problem Solving with Creativity and Metacognitive Reflection for System 2 Sections:*

   *2.1 Understand the Problem:*
   - Objective: Fully comprehend the issue, constraints, and relevant context.
   - Reflection: "What do I understand about this issue? What might I be overlooking?"
   - Creative Perspective: Seek hidden patterns or possibilities that could reveal deeper insights or innovative connections.

   *2.2 Analyze the Information:*
   - Objective: Break down the problem logically.
   - Reflection: "Am I considering all factors? Are there any assumptions that need challenging?"
   - Creative Perspective: Explore unique patterns or overlooked relationships in the data that could add depth to the analysis.

   *2.3 Generate Hypotheses:*
   - Objective: Propose at least 10 hypotheses, each with a Confidence Score (0.0 to 1.0) and Creative Score (0.0 to 1.0), reflecting originality, surprise, and utility.
   - Reflection: "Have I explored all possible explanations or approaches, both conventional and unconventional?"
   - Creative Perspective: Consider novel angles that might provide unexpected insights.

   *2.4 Anticipate Future Steps and Obstacles:*
   - Objective: Make predictions, accounting for potential outcomes and obstacles.
   - Reflection: "What challenges might I face? Is my plan flexible for different scenarios?"
   - Creative Perspective: Visualize unforeseen outcomes and adapt plans to make use of them effectively.

   *2.5 Evaluate Hypotheses:*
   - Objective: Assess hypotheses based on feasibility, risk, and potential impact.
   - Evaluation: Refine Confidence and Creative Scores as needed.
   - Reflection: "Am I unbiased in my assessment? Which options fit best with the overall objectives?"
   - Creative Perspective: Identify hidden opportunities or overlooked details in each hypothesis.

   *2.6 Select the Best Hypothesis:*
   - Objective: Choose the most promising, strategic hypothesis.
   - Reflection: "Why does this hypothesis stand out? How does it uniquely address the issue?"
   - Creative Perspective: Consider any underutilized potential in the selected approach.

   *2.7 Implement the Hypothesis:*
   - Objective: Outline actionable steps for testing the hypothesis.
   - Reflection: "Is this plan practical? What resources or preparation are required?"
   - Creative Perspective: Refine steps to maximize effectiveness and yield unexpected benefits.

   *2.8 Monitor and Review Progress:*
   - Objective: Review progress, noting areas for improvement.
   - Reflection: "What’s working well? What could be improved?"
   - Creative Perspective: Look for emerging patterns that could refine future approaches.

   *2.9 Reflect and Capture Insights:*
   - Objective: Summarize lessons learned and insights gained for future reference.
   - Reflection: "What new understanding has emerged from this process?"
   - Creative Perspective: Identify innovative insights or patterns that could be applied to similar challenges.

3. *Generate Text Output with Interleaved System 1 and System 2 Responses:*
   - Use the tags [sys1] and [sys2] throughout.
   - Aim for a lengthier, detailed response. Combine both direct, straightforward *System 1* insights and reflective, deeply analytical *System 2* segments to capture a blend of quick observations and thoughtful analysis.

---

### *Example Input Text:*

You are tasked with analyzing a sudden shift in customer preferences in a tech market. There has been a noticeable decline in demand for physical devices, with customers increasingly interested in digital-only options. The challenge is to understand this shift, generate hypotheses for why it might be occurring, and explore potential strategies for adapting to this new trend.

---

### *Processed Output with System 1 and System 2 Thinking:*

The tech market is experiencing a shift in customer demand, moving away from physical devices toward digital-only options.

[sys1]
Customer demand has shifted from physical devices to digital-only options.
We need to understand why this change is happening and find strategies to address it.
Key factors may include cost, convenience, and technological trends.
[end sys1]

[sys2]
**2.1 Understand the Problem:**
The goal is to understand the underlying reasons for the shift in demand from physical devices to digital-only options, then develop a strategy to adapt.

*Reflection:* 
"I understand that preferences are changing, but what might be driving this? Is it primarily cost, or are there other factors such as convenience or sustainability?"

*Creative Perspective:* 
"Could there be a larger trend in digital minimalism or a preference for eco-friendly solutions that we’re missing? What unseen motivations might explain this shift?"

**2.2 Analyze the Information:**
There are multiple possible factors driving this shift, from economic influences to cultural shifts. It’s essential to isolate each factor and understand its impact.

*Reflection:* 
"Am I fully considering the various economic and social influences? Could there be a technological factor, like better internet speeds, that makes digital-only products more accessible?"

*Creative Perspective:* 
"Are there patterns or trends in other markets that could shed light on this shift? Could this be part of a larger trend toward virtual experiences?"

**2.3 Generate Hypotheses:**
1. Customers prefer digital options due to lower costs. (Confidence: 0.8, Creative: 0.4)
2. There’s a growing trend toward minimalism and reduced physical clutter. (Confidence: 0.7, Creative: 0.7)
3. Digital products offer greater flexibility and ease of use. (Confidence: 0.6, Creative: 0.6)
4. Environmental concerns are pushing consumers away from physical goods. (Confidence: 0.6, Creative: 0.8)
5. Advances in tech make digital-only options more functional. (Confidence: 0.8, Creative: 0.5)
6. Pandemic-era remote work increased demand for digital solutions. (Confidence: 0.7, Creative: 0.6)
7. Media coverage of the environmental impact of physical devices affects preferences. (Confidence: 0.5, Creative: 0.7)
8. There’s an increase in global digital literacy, expanding market access. (Confidence: 0.6, Creative: 0.6)
9. Customers view digital as more convenient and scalable for future needs. (Confidence: 0.7, Creative: 0.5)
10. Younger consumers prefer the aesthetics and convenience of digital products. (Confidence: 0.6, Creative: 0.6)

*Reflection:* 
"Have I considered all possible influences? Are there any surprising factors that could explain this shift?"

*Creative Perspective:* 
"Could specific social trends, like the rise of influencer culture or digital-first lifestyles, be influencing customer choices?"

**2.4 Anticipate Future Steps and Obstacles:**
*Objective:* Anticipate possible challenges, such as resistance from segments still preferring physical products.

*Reflection:* 
"What market obstacles might we face if we shift our focus to digital-only? Are there sub-segments that still prioritize physical products?"

*Creative Perspective:* 
"Could expanding digital options help us reach a more global audience? Are there emerging trends that we could leverage in our strategy?"

[end sys2]

[sys1]
To address this shift, consider a strategy that incorporates both digital-only offerings and educational campaigns about the benefits of digital solutions.
Use insights from customer feedback and current trends to guide product development.
Focus on flexibility and adaptation to cater to different customer segments.
[end sys1]


package com.wonderful.ishow.widget;

import com.yalantis.ucrop.util.CubicEasing;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * Base View to manage image zoom/scroll/pinch operations
 * 
 */
public abstract class ImageViewTouchBase extends ImageView {
	private static final String TAG = ImageViewTouchBase.class.getSimpleName();

	public interface OnDrawableChangeListener {

		/**
		 * Callback invoked when a new drawable has been assigned to the view
		 * 
		 * @param drawable
		 */
		void onDrawableChanged(Drawable drawable);
	};

	public interface OnLayoutChangeListener {
		/**
		 * Callback invoked when the layout bounds changed
		 * 
		 * @param changed
		 * @param left
		 * @param top
		 * @param right
		 * @param bottom
		 */
		void onLayoutChanged(boolean changed, int left, int top, int right, int bottom);
	};

	/**
	 * Use this to change the
	 * {@link ImageViewTouchBase#setDisplayType(DisplayType)} of this View
	 *
	 */
	public enum DisplayType {
		/** Image is not scaled by default */
		NONE,
		/** Image will be always presented using this view's bounds */
		FIT_TO_SCREEN,
		/** Image will be scaled only if bigger than the bounds of this view */
		FIT_IF_BIGGER
	};

	private static final boolean LOG_ENABLED = false;
	public static final float ZOOM_INVALID = -1.0f;

	protected Matrix mBaseMatrix = new Matrix();
	protected Matrix mSuppMatrix = new Matrix();
	protected Matrix mNextMatrix;
	protected Handler mHandler = new Handler();
	protected Runnable mLayoutRunnable = null;
	protected boolean mUserScaled = false;

	private float mMaxZoom = ZOOM_INVALID;
	private float mMinZoom = ZOOM_INVALID;

	// true when min and max zoom are explicitly defined
	private boolean mMaxZoomDefined;
	private boolean mMinZoomDefined;

	protected final Matrix mDisplayMatrix = new Matrix();
	protected final float[] mMatrixValues = new float[9];//{ 1,0,0, 0,1,0, 0,0,1 };

	private int mThisWidth = -1;
	private int mThisHeight = -1;
	private PointF mCenter = new PointF();

	protected DisplayType mScaleType = DisplayType.NONE;
	private boolean mScaleTypeChanged;
	private boolean mBitmapChanged;

	final protected int DEFAULT_ANIMATION_DURATION = 200;

	protected RectF mBitmapRect = new RectF();
	protected RectF mCenterRect = new RectF();
	protected RectF mScrollRect = new RectF();

	private OnDrawableChangeListener mDrawableChangeListener;
	private OnLayoutChangeListener mOnLayoutChangeListener;

	public ImageViewTouchBase(Context context) {
		this(context, null);
	}

	public ImageViewTouchBase(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ImageViewTouchBase(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs, defStyle);
	}

	public void setOnDrawableChangedListener(OnDrawableChangeListener listener) {
		mDrawableChangeListener = listener;
	}

	public void setOnLayoutChangeListener(OnLayoutChangeListener listener) {
		mOnLayoutChangeListener = listener;
	}

	protected void init(Context context, AttributeSet attrs, int defStyle) {
		setScaleType(ImageView.ScaleType.MATRIX);
	}

	@Override
	public void setScaleType(ScaleType scaleType) {
		if(scaleType == ScaleType.MATRIX)
			super.setScaleType(scaleType);
		else
			Log.w(TAG, "Unsupported scale type. Only MATRIX can be used");
	}

	/**
	 * Clear the current drawable
	 */
	public void clear() {
		setImageBitmap(null);
	}

	/**
	 * Change the display type
	 */
	public void setDisplayType(DisplayType type) {
		if(type == mScaleType)
			return;

		Log.i(TAG, "setDisplayType: " + type);
		mUserScaled = false;
		mScaleType = type;
		mScaleTypeChanged = true;
		requestLayout();

	}

	public DisplayType getDisplayType() {
		return mScaleType;
	}

	protected void setMinScale(float value) {
		if(LOG_ENABLED)
			Log.d(TAG, "setMinZoom: " + value);

		mMinZoom = value;
	}

	protected void setMaxScale(float value) {
		if(LOG_ENABLED)
			Log.d(TAG, "setMaxZoom: " + value);
		
		mMaxZoom = value;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

		if(LOG_ENABLED) {
			Log.d(TAG, "onLayout: " + changed + ", bitmapChanged: " + mBitmapChanged + ", scaleChanged: "
					+ mScaleTypeChanged);
		}

		super.onLayout(changed, left, top, right, bottom);

		int deltaX = 0;
		int deltaY = 0;

		if(changed) {
			int oldw = mThisWidth;
			int oldh = mThisHeight;

			mThisWidth = right - left;
			mThisHeight = bottom - top;

			deltaX = mThisWidth - oldw;
			deltaY = mThisHeight - oldh;

			// update center point
			mCenter.x = mThisWidth / 2f;
			mCenter.y = mThisHeight / 2f;
		}

		Runnable r = mLayoutRunnable;

		if(r != null) {
			mLayoutRunnable = null;
			r.run();
		}

		final Drawable drawable = getDrawable();

		if(drawable != null) {

			if(changed || mScaleTypeChanged || mBitmapChanged) {

				float scale = 1;

				// retrieve the old values
				float old_default_scale = getDefaultScale(mScaleType);
				float old_matrix_scale = getScale(mBaseMatrix);
				float old_scale = getScale();
				float old_min_scale = Math.min(1f, 1f / old_matrix_scale);

				getProperBaseMatrix(drawable, mBaseMatrix);

				float new_matrix_scale = getScale(mBaseMatrix);

				if(LOG_ENABLED) {
					Log.d(TAG, "old matrix scale: " + old_matrix_scale);
					Log.d(TAG, "new matrix scale: " + new_matrix_scale);
					Log.d(TAG, "old min scale: " + old_min_scale);
					Log.d(TAG, "old scale: " + old_scale);
				}

				// 1. bitmap changed or scale type changed
				if(mBitmapChanged || mScaleTypeChanged) {

					if(LOG_ENABLED) {
						Log.d(TAG, "display type: " + mScaleType);
					}

					if(mNextMatrix != null) {
						mSuppMatrix.set(mNextMatrix);
						mNextMatrix = null;
						scale = getScale();
					} else {
						mSuppMatrix.reset();
						scale = getDefaultScale(mScaleType);
					}

					setImageMatrix(getImageViewMatrix());

					if(scale != getScale()) {
						zoomTo(scale);
					}

				} else if(changed) {

					// 2. layout size changed

					if(!mMinZoomDefined)
						mMinZoom = ZOOM_INVALID;
					if(!mMaxZoomDefined)
						mMaxZoom = ZOOM_INVALID;

					setImageMatrix(getImageViewMatrix());
					postTranslate(-deltaX, -deltaY);

					if(!mUserScaled) {
						scale = getDefaultScale(mScaleType);
						zoomTo(scale);
					} else {
						if(Math.abs(old_scale - old_min_scale) > 0.001) {
							scale = (old_matrix_scale / new_matrix_scale) * old_scale;
						}
						zoomTo(scale);
					}

					if(LOG_ENABLED) {
						Log.d(TAG, "old min scale: " + old_default_scale);
						Log.d(TAG, "old scale: " + old_scale);
						Log.d(TAG, "new scale: " + scale);
					}

				}

				mUserScaled = false;

				if(scale > getMaxScale() || scale < getMinScale()) {
					// if current scale if outside the min/max bounds
					// then restore the correct scale
					zoomTo(scale);
				}

				center(true, true);

				if(mBitmapChanged)
					onDrawableChanged(drawable);
				if(changed || mBitmapChanged || mScaleTypeChanged)
					onLayoutChanged(left, top, right, bottom);

				if(mScaleTypeChanged)
					mScaleTypeChanged = false;
				if(mBitmapChanged)
					mBitmapChanged = false;

				if(LOG_ENABLED) {
					Log.d(TAG, "new scale: " + getScale());
				}
			}
		}
		else
		{
			// drawable is null
			if(mBitmapChanged)
				onDrawableChanged(drawable);
			if(changed || mBitmapChanged || mScaleTypeChanged)
				onLayoutChanged(left, top, right, bottom);

			if(mBitmapChanged)
				mBitmapChanged = false;
			if(mScaleTypeChanged)
				mScaleTypeChanged = false;

		}
	}

	/**
	 * Restore the original display
	 * 
	 */
	public void resetDisplay() {
		mBitmapChanged = true;
		requestLayout();
	}

	public void resetMatrix() {
		if(LOG_ENABLED) {
			Log.i(TAG, "resetMatrix");
		}
		mSuppMatrix = new Matrix();
		setImageMatrix(getImageViewMatrix());
		postInvalidate();
	}

	protected float getDefaultScale(DisplayType type) {
		if(type == DisplayType.FIT_TO_SCREEN) {
			// always fit to screen
			return 1.0f;
		} else if(type == DisplayType.FIT_IF_BIGGER) {
			// normal scale if smaller, fit to screen otherwise
			return Math.min(1.0f, 1.0f / getScale(mBaseMatrix));
		} else {
			// no scale
			return 1.0f / getScale(mBaseMatrix);
		}
	}

	@Override
	public void setImageResource(int resId) {
		setImageDrawable(getContext().getResources().getDrawable(resId));
	}

	/**
	 * {@inheritDoc} Set the new image to display and reset the internal matrix.
	 * 
	 * @param bitmap
	 *            the {@link Bitmap} to display
	 * @see {@link ImageView#setImageBitmap(Bitmap)}
	 */
	@Override
	public void setImageBitmap(final Bitmap bitmap) {
		setImageBitmap(bitmap, getDisplayMatrix(), ZOOM_INVALID, ZOOM_INVALID);
	}

	public void setImageBitmap(final Bitmap bitmap, Matrix matrix) {
		setImageBitmap(bitmap, matrix, ZOOM_INVALID, ZOOM_INVALID);
	}
	
	/**
	 * @see #setImageDrawable(Drawable, Matrix, float, float)
	 * 
	 * @param bitmap
	 * @param matrix
	 * @param min_zoom
	 * @param max_zoom
	 */
	public void setImageBitmap(final Bitmap bitmap, Matrix matrix, float min_zoom, float max_zoom) {
		Drawable drawable = new BitmapDrawable(getResources(), bitmap);
		setImageDrawable((bitmap != null) ? drawable : null, matrix, min_zoom, max_zoom);
	}
	
	@Override
	public void setImageDrawable(Drawable drawable) {
		setImageDrawable(drawable, null, ZOOM_INVALID, ZOOM_INVALID);
	}

	/**
	 * 
	 * Note: if the scaleType is FitToScreen then min_zoom must be <= 1 and
	 * max_zoom must be >= 1
	 * 
	 * @param drawable
	 *            the new drawable
	 * @param initial_matrix
	 *            the optional initial display matrix
	 * @param min_zoom
	 *            the optional minimum scale, pass {@link #ZOOM_INVALID} to use
	 *            the default min_zoom
	 * @param max_zoom
	 *            the optional maximum scale, pass {@link #ZOOM_INVALID} to use
	 *            the default max_zoom
	 */
	public void setImageDrawable(final Drawable drawable, final Matrix initial_matrix, final float min_zoom,
			final float max_zoom) {

		final int viewWidth = getWidth();

		if(viewWidth <= 0) {
			mLayoutRunnable = new Runnable() {

				@Override
				public void run() {
					setImageDrawable(drawable, initial_matrix, min_zoom, max_zoom);
				}
			};
			return;
		}
		_setImageDrawable(drawable, initial_matrix, min_zoom, max_zoom);
	}

	protected void _setImageDrawable(final Drawable drawable, final Matrix initial_matrix, float min_zoom,
			float max_zoom) {

		if(LOG_ENABLED)
			Log.i(TAG, "_setImageDrawable");

		if(drawable != null) {
			if(LOG_ENABLED)
				Log.d(TAG, "size: " + drawable.getIntrinsicWidth() + "x" + drawable.getIntrinsicHeight());
			
			super.setImageDrawable(drawable);
		} else {
			mBaseMatrix.reset();
			super.setImageDrawable(null);
		}

		if(min_zoom != ZOOM_INVALID && max_zoom != ZOOM_INVALID) {
			min_zoom = Math.min(min_zoom, max_zoom);
			max_zoom = Math.max(min_zoom, max_zoom);

			mMinZoom = min_zoom;
			mMaxZoom = max_zoom;

			mMinZoomDefined = true;
			mMaxZoomDefined = true;

			if(mScaleType == DisplayType.FIT_TO_SCREEN || mScaleType == DisplayType.FIT_IF_BIGGER) {

				if(mMinZoom >= 1) {
					mMinZoomDefined = false;
					mMinZoom = ZOOM_INVALID;
				}

				if(mMaxZoom <= 1) {
					mMaxZoomDefined = true;
					mMaxZoom = ZOOM_INVALID;
				}
			}
		} else {
			mMinZoom = ZOOM_INVALID;
			mMaxZoom = ZOOM_INVALID;

			mMinZoomDefined = false;
			mMaxZoomDefined = false;
		}

		if(initial_matrix != null) {
			mNextMatrix = new Matrix(initial_matrix);
		}

		mBitmapChanged = true;
		requestLayout();
	}

	/**
	 * Fired as soon as a new Bitmap has been set
	 * 
	 * @param drawable
	 */
	protected void onDrawableChanged(final Drawable drawable) {
		if(LOG_ENABLED)
			Log.i(TAG, "onDrawableChanged");
		
		fireOnDrawableChangeListener(drawable);
	}

	protected void fireOnLayoutChangeListener(int left, int top, int right, int bottom) {
		if(null != mOnLayoutChangeListener)
			mOnLayoutChangeListener.onLayoutChanged(true, left, top, right, bottom);
	}

	protected void fireOnDrawableChangeListener(Drawable drawable) {
		if(null != mDrawableChangeListener)
			mDrawableChangeListener.onDrawableChanged(drawable);
		
	}

	/**
	 * Called just after {@link #onLayout(boolean, int, int, int, int)} if the
	 * view's bounds has changed or a new Drawable has been set or the
	 * {@link DisplayType} has been modified
	 * 
	 * @param left
	 * @param top
	 * @param right
	 * @param bottom
	 */
	protected void onLayoutChanged(int left, int top, int right, int bottom) {
		if(LOG_ENABLED)
			Log.i(TAG, "onLayoutChanged");
		
		fireOnLayoutChangeListener(left, top, right, bottom);
	}

	protected float computeMaxZoom() {
		final Drawable drawable = getDrawable();

		if(drawable == null)
			return 1.0f;

		float fw = (float) drawable.getIntrinsicWidth() / (float) mThisWidth;
		float fh = (float) drawable.getIntrinsicHeight() / (float) mThisHeight;
		float scale = Math.max(fw, fh) * 8;

		if(LOG_ENABLED)
			Log.i(TAG, "computeMaxZoom: " + scale);
		
		return scale;
	}

	protected float computeMinZoom() {
		final Drawable drawable = getDrawable();

		if(drawable == null)
			return 1.0f;

		float scale = getScale(mBaseMatrix);
		scale = Math.min(1.0f, 1.0f / scale);

		if(LOG_ENABLED)
			Log.i(TAG, "computeMinZoom: " + scale);

		return scale;
	}

	/**
	 * Returns the current maximum allowed image scale
	 * 
	 * @return
	 */
	public float getMaxScale() {
		if(mMaxZoom == ZOOM_INVALID)
			mMaxZoom = computeMaxZoom();
		
		return mMaxZoom;
	}

	/**
	 * Returns the current minimum allowed image scale
	 * 
	 * @return
	 */
	public float getMinScale() {
		if(mMinZoom == ZOOM_INVALID)
			mMinZoom = computeMinZoom();
		
		return mMinZoom;
	}

	/**
	 * Returns the current view matrix
	 * 
	 * @return
	 */
	public Matrix getImageViewMatrix() {
		return getImageViewMatrix(mSuppMatrix);
	}

	public Matrix getImageViewMatrix(Matrix supportMatrix) {
		mDisplayMatrix.set(mBaseMatrix);
		mDisplayMatrix.postConcat(supportMatrix);
		return mDisplayMatrix;
	}

	@Override
	public void setImageMatrix(Matrix matrix) {
		Matrix current = getImageMatrix();
		boolean needUpdate = false;

		if(matrix == null && !current.isIdentity() || matrix != null && !current.equals(matrix)) {
			needUpdate = true;
		}

		super.setImageMatrix(matrix);

		if(needUpdate)
			onImageMatrixChanged();
	}

	/**
	 * Called just after a new Matrix has been assigned.
	 * 
	 * @see {@link #setImageMatrix(Matrix)}
	 */
	protected void onImageMatrixChanged() {
	}

	/**
	 * Returns the current image display matrix.<br />
	 * This matrix can be used in the next call to the
	 * {@link #setImageDrawable(Drawable, Matrix, float, float)} to restore the
	 * same view state of the previous {@link Bitmap}.<br />
	 * Example:
	 * 
	 * <pre>
	 * Matrix currentMatrix = mImageView.getDisplayMatrix();
	 * mImageView.setImageBitmap(newBitmap, currentMatrix, ZOOM_INVALID, ZOOM_INVALID);
	 * </pre>
	 * 
	 * @return the current support matrix
	 */
	public Matrix getDisplayMatrix() {
		return new Matrix(mSuppMatrix);
	}

	/**
	 * Setup the base matrix so that the image is centered and scaled properly.
	 * 
	 * @param drawable
	 * @param matrix
	 */
	protected void getProperBaseMatrix(Drawable drawable, Matrix matrix) {
		float viewWidth = mThisWidth;
		float viewHeight = mThisHeight;

		if(LOG_ENABLED)
			Log.d(TAG, "getProperBaseMatrix. view: " + viewWidth + "x" + viewHeight);

		float w = drawable.getIntrinsicWidth();
		float h = drawable.getIntrinsicHeight();
		float widthScale, heightScale;
		matrix.reset();

		if(w > viewWidth || h > viewHeight) {
			widthScale = viewWidth / w;
			heightScale = viewHeight / h;
			float scale = Math.min(widthScale, heightScale);
			matrix.postScale(scale, scale);

			float tw = (viewWidth - w * scale) / 2.0f;
			float th = (viewHeight - h * scale) / 2.0f;
			matrix.postTranslate(tw, th);

		} else {
			widthScale = viewWidth / w;
			heightScale = viewHeight / h;
			float scale = Math.min(widthScale, heightScale);
			matrix.postScale(scale, scale);

			float tw = (viewWidth - w * scale) / 2.0f;
			float th = (viewHeight - h * scale) / 2.0f;
			matrix.postTranslate(tw, th);
		}

		if(LOG_ENABLED)
			printMatrix(matrix);
	}

	/**
	 * Setup the base matrix so that the image is centered and scaled properly.
	 * 
	 * @param bitmap
	 * @param matrix
	 */
	protected void getProperBaseMatrix2(Drawable bitmap, Matrix matrix) {

		float viewWidth = mThisWidth;
		float viewHeight = mThisHeight;

		float w = bitmap.getIntrinsicWidth();
		float h = bitmap.getIntrinsicHeight();

		matrix.reset();

		float widthScale = viewWidth / w;
		float heightScale = viewHeight / h;

		float scale = Math.min(widthScale, heightScale);

		matrix.postScale(scale, scale);
		matrix.postTranslate((viewWidth - w * scale) / 2.0f, (viewHeight - h * scale) / 2.0f);
	}
	
	protected float getValue(Matrix matrix, int whichValue) {
		matrix.getValues(mMatrixValues);
		return mMatrixValues[whichValue];
	}
	
	public void printMatrix(Matrix matrix) {
		float scalex = getValue(matrix, Matrix.MSCALE_X);
		float scaley = getValue(matrix, Matrix.MSCALE_Y);
		float tx = getValue(matrix, Matrix.MTRANS_X);
		float ty = getValue(matrix, Matrix.MTRANS_Y);
		Log.d(TAG, "matrix: { x: " + tx + ", y: " + ty + ", scalex: " + scalex + ", scaley: " + scaley + " }");
	}

	public RectF getBitmapRect() {
		return getBitmapRect(mSuppMatrix);
	}

	protected RectF getBitmapRect(Matrix supportMatrix) {
		final Drawable drawable = getDrawable();

		if(drawable == null)
			return null;
		Matrix m = getImageViewMatrix(supportMatrix);
		mBitmapRect.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		m.mapRect(mBitmapRect);
		return mBitmapRect;
	}

	protected float getScale(Matrix matrix) {
		return getValue(matrix, Matrix.MSCALE_X);
	}

	@SuppressLint("Override")
	public float getRotation() {
		return 0;
	}

	/**
	 * Returns the current image scale
	 * 
	 * @return
	 */
	public float getScale() {
		return getScale(mSuppMatrix);
	}

	protected void center(boolean horizontal, boolean vertical) {
		final Drawable drawable = getDrawable();
		if(drawable == null)
			return;

		RectF rect = getCenter(mSuppMatrix, horizontal, vertical);

		if(rect.left != 0 || rect.top != 0) {
			if(LOG_ENABLED)
				Log.i(TAG, "center");

			postTranslate(rect.left, rect.top);
		}
	}

	protected RectF getCenter(Matrix supportMatrix, boolean horizontal, boolean vertical) {
		final Drawable drawable = getDrawable();

		if(drawable == null)
			return new RectF(0, 0, 0, 0);

		mCenterRect.set(0, 0, 0, 0);
		RectF rect = getBitmapRect(supportMatrix);
		float height = rect.height();
		float width = rect.width();
		float deltaX = 0, deltaY = 0;
		if(vertical) {
			int viewHeight = mThisHeight;
			if(height < viewHeight) {
				deltaY = (viewHeight - height) / 2 - rect.top;
			} else if(rect.top > 0) {
				deltaY = -rect.top;
			} else if(rect.bottom < viewHeight) {
				deltaY = mThisHeight - rect.bottom;
			}
		}
		if(horizontal) {
			int viewWidth = mThisWidth;
			if(width < viewWidth) {
				deltaX = (viewWidth - width) / 2 - rect.left;
			} else if(rect.left > 0) {
				deltaX = -rect.left;
			} else if(rect.right < viewWidth) {
				deltaX = viewWidth - rect.right;
			}
		}
		mCenterRect.set(deltaX, deltaY, 0, 0);
		return mCenterRect;
	}

	protected void postTranslate(float deltaX, float deltaY) {
		if(deltaX != 0 || deltaY != 0) {
			if(LOG_ENABLED)
				Log.i(TAG, "postTranslate: " + deltaX + "x" + deltaY);
			
			mSuppMatrix.postTranslate(deltaX, deltaY);
			setImageMatrix(getImageViewMatrix());
		}
	}

	protected void postScale(float scale, float centerX, float centerY) {
		if(LOG_ENABLED)
			Log.i(TAG, "postScale: " + scale + ", center: " + centerX + "x" + centerY);
		
		mSuppMatrix.postScale(scale, scale, centerX, centerY);
		setImageMatrix(getImageViewMatrix());
	}

	protected PointF getCenter() {
		return mCenter;
	}

	protected void zoomTo(float scale) {
		if(scale > getMaxScale())
			scale = getMaxScale();
		if(scale < getMinScale())
			scale = getMinScale();

		PointF center = getCenter();
		zoomTo(scale, center.x, center.y);
	}

	/**
	 * Scale to the target scale
	 * 
	 * @param scale
	 *            the target zoom
	 * @param durationMs
	 *            the animation duration
	 */
	public void zoomTo(float scale, float durationMs) {
		PointF center = getCenter();
		zoomTo(scale, center.x, center.y, durationMs);
	}

	protected void zoomTo(float scale, float centerX, float centerY) {
		if(scale > getMaxScale())
			scale = getMaxScale();

		float oldScale = getScale();
		float deltaScale = scale / oldScale;
		postScale(deltaScale, centerX, centerY);
		onZoom(getScale());
		center(true, true);
	}

	protected void onZoom(float scale) {
	}

	protected void onZoomAnimationCompleted(float scale) {
	}

	/**
	 * Scrolls the view by the x and y amount
	 * 
	 * @param x
	 * @param y
	 */
	public void scrollBy(float x, float y) {
		panBy(x, y);
	}

	protected void panBy(double dx, double dy) {
		RectF rect = getBitmapRect();
		mScrollRect.set((float) dx, (float) dy, 0, 0);
		updateRect(rect, mScrollRect);
		postTranslate(mScrollRect.left, mScrollRect.top);
		center(true, true);
	}

	protected void updateRect(RectF bitmapRect, RectF scrollRect) {
		if(bitmapRect == null)
			return;

		if(bitmapRect.top >= 0 && bitmapRect.bottom <= mThisHeight)
			scrollRect.top = 0;
		if(bitmapRect.left >= 0 && bitmapRect.right <= mThisWidth)
			scrollRect.left = 0;
		if(bitmapRect.top + scrollRect.top >= 0 && bitmapRect.bottom > mThisHeight)
			scrollRect.top = (int) (0 - bitmapRect.top);
		if(bitmapRect.bottom + scrollRect.top <= (mThisHeight - 0) && bitmapRect.top < 0)
			scrollRect.top = (int) ((mThisHeight - 0) - bitmapRect.bottom);
		if(bitmapRect.left + scrollRect.left >= 0)
			scrollRect.left = (int) (0 - bitmapRect.left);
		if(bitmapRect.right + scrollRect.left <= (mThisWidth - 0))
			scrollRect.left = (int) ((mThisWidth - 0) - bitmapRect.right);
	}

	protected void scrollBy(final float dx, final float dy, final float durationMs) {
		final long startTime = System.currentTimeMillis();
		mHandler.post(new Runnable() {

			double old_x = 0;
			double old_y = 0;

			@Override
			public void run() {
				long now = System.currentTimeMillis();
				float currentMs = Math.min(durationMs, now - startTime);
				double x = CubicEasing.easeOut(currentMs, 0, dx, durationMs);
				double y = CubicEasing.easeOut(currentMs, 0, dy, durationMs);
				panBy((x - old_x), (y - old_y));
				old_x = x;
				old_y = y;
				if(currentMs < durationMs) {
					mHandler.post(this);
				} else {
					RectF centerRect = getCenter(mSuppMatrix, true, true);
					if(centerRect.left != 0 || centerRect.top != 0)
						scrollBy(centerRect.left, centerRect.top);
				}
			}
		});
	}

	protected void zoomTo(float scale, float centerX, float centerY, final float durationMs) {
		if(scale > getMaxScale())
			scale = getMaxScale();

		final long startTime = System.currentTimeMillis();
		final float oldScale = getScale();

		final float deltaScale = scale - oldScale;

		Matrix m = new Matrix(mSuppMatrix);
		m.postScale(scale, scale, centerX, centerY);
		RectF rect = getCenter(m, true, true);

		final float destX = centerX + rect.left * scale;
		final float destY = centerY + rect.top * scale;

		mHandler.post(new Runnable() {

			@Override
			public void run() {
				long now = System.currentTimeMillis();
				float currentMs = Math.min(durationMs, now - startTime);
				float newScale = (float) CubicEasing.easeInOut(currentMs, 0, deltaScale, durationMs);
				zoomTo(oldScale + newScale, destX, destY);
				if(currentMs < durationMs) {
					mHandler.post(this);
				} else {
					onZoomAnimationCompleted(getScale());
					center(true, true);
				}
			}
		});
	}

}