Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Mar 17, 2013
1 parent 1fe37d8 commit e15e893
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ImageViewTouch extends ImageViewTouchBase {
private OnImageViewTouchDoubleTapListener mDoubleTapListener;
private OnImageViewTouchSingleTapListener mSingleTapListener;

public ImageViewTouch( Context context, AttributeSet attrs ) {
public ImageViewTouch ( Context context, AttributeSet attrs ) {
super( context, attrs );
}

Expand Down Expand Up @@ -82,8 +82,8 @@ protected OnScaleGestureListener getScaleListener() {
}

@Override
protected void onLayoutChanged() {
super.onLayoutChanged();
protected void onLayoutChanged( boolean changed, int left, int top, int right, int bottom ) {
super.onLayoutChanged( changed, left, top, right, bottom );

float v[] = new float[9];
mSuppMatrix.getValues( v );
Expand Down Expand Up @@ -179,7 +179,8 @@ public boolean onFling( MotionEvent e1, MotionEvent e2, float velocityX, float v
* Determines whether this ImageViewTouch can be scrolled.
*
* @param direction
* - positive direction value means scroll from right to left, negative value means scroll from left to right
* - positive direction value means scroll from right to left,
* negative value means scroll from left to right
*
* @return true if there is some more place to scroll, false - otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public abstract class ImageViewTouchBase extends ImageView implements IDisposable {

public interface OnDrawableChangedListener {
public interface OnDrawableChangeListener {

/**
* Callback invoked when a new drawable has been
Expand All @@ -33,13 +33,26 @@ public interface OnDrawableChangedListener {
*/
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 );
};

public enum DisplayType {
NONE, FIT_TO_SCREEN,
};


public static final String LOG_TAG = "image";
protected static final boolean LOG_ENABLED = false;
protected static final boolean LOG_ENABLED = true;

public static final float ZOOM_INVALID = -1f;

Expand Down Expand Up @@ -74,7 +87,8 @@ public enum DisplayType {
protected RectF mCenterRect = new RectF();
protected RectF mScrollRect = new RectF();

private OnDrawableChangedListener mDrawableChangedListener;
private OnDrawableChangeListener mDrawableChangeListener;
private OnLayoutChangeListener mOnLayoutChangeListener;

public ImageViewTouchBase( Context context ) {
super( context );
Expand All @@ -86,8 +100,12 @@ public ImageViewTouchBase( Context context, AttributeSet attrs ) {
init();
}

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

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

protected void init() {
Expand Down Expand Up @@ -174,8 +192,9 @@ protected void onLayout( boolean changed, int left, int top, int right, int bott
}

final Drawable drawable = getDrawable();

if ( drawable != null ) {

if ( changed || mScaleTypeChanged || mBitmapChanged ) {

float scale = 1;
Expand Down Expand Up @@ -230,11 +249,22 @@ protected void onLayout( boolean changed, int left, int top, int right, int bott
}
center( true, true );

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

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

onLayoutChanged();
}
} else {
// drawable is null
if( mBitmapChanged ) {
onDrawableChanged( drawable );
}

mBitmapChanged = false;
mScaleTypeChanged = false;

onLayoutChanged( changed || mBitmapChanged || mScaleTypeChanged, left, top, right, bottom );
}
}

Expand Down Expand Up @@ -356,7 +386,7 @@ protected void _setImageDrawable( final Drawable drawable, final Matrix initial_
mNextMatrix = new Matrix( initial_matrix );
}

onDrawableChanged( drawable );
mBitmapChanged = true;
requestLayout();
}

Expand All @@ -366,19 +396,41 @@ protected void _setImageDrawable( final Drawable drawable, final Matrix initial_
* @param bitmap
*/
protected void onDrawableChanged( final Drawable drawable ) {
mBitmapChanged = true;

if ( mDrawableChangedListener != null ) {
mDrawableChangedListener.onDrawableChanged( drawable );
if( LOG_ENABLED ) {
Log.i( LOG_TAG, "onDrawableChanged" );
}
fireOnDrawableChangeListener( drawable );
}

protected void fireOnLayoutChangeListener( boolean changed, int left, int top, int right, int bottom ) {
if( null != mOnLayoutChangeListener ) {
mOnLayoutChangeListener.onLayoutChanged( changed, 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 bounds/bitmap changed
* @param left
* @param top
* @param right
* @param bottom
*/
protected void onLayoutChanged() {}
protected void onLayoutChanged( boolean changed, int left, int top, int right, int bottom ) {
if( LOG_ENABLED ) {
Log.i( LOG_TAG, "onLayoutChanged" );
}
fireOnLayoutChangeListener( changed, left, top, right, bottom );
}

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

0 comments on commit e15e893

Please sign in to comment.