Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
add attr land scape orientation on zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveiradev committed Aug 7, 2016
1 parent 7cf4409 commit 880fe9c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.v4.util.Pair;
import android.support.v7.widget.ContentFrameLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
Expand All @@ -20,25 +22,29 @@
*/
public final class ZoomAnimation {

private static final int ROTATION_DELAY = 500;
private static Animator mCurrentAnimator;
private static ImageView mImageZoom;
private static boolean isLandScapeScreen;
private static ContentFrameLayout container;

public static void zoom(View view , Activity activity){
zoomImageFromThumb(view,activity);
public static void zoom(View view, Activity activity, boolean isLandScape) {
zoomImageFromThumb(view, activity, isLandScape);
}

private static void zoomImageFromThumb(final View thumbView, final Activity activity, boolean isLandScape) {
isLandScapeScreen = isLandScape;

private static void zoomImageFromThumb(final View thumbView, Activity activity) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}

ContentFrameLayout container = (ContentFrameLayout)activity.findViewById(android.R.id.content);
container = (ContentFrameLayout) activity.findViewById(android.R.id.content);

mImageZoom = new ImageView(thumbView.getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mImageZoom.setLayoutParams(params);
mImageZoom.setImageDrawable(((ImageButton)thumbView).getDrawable());
mImageZoom.setImageDrawable(((ImageButton) thumbView).getDrawable());

container.addView(mImageZoom);

Expand All @@ -51,7 +57,7 @@ private static void zoomImageFromThumb(final View thumbView, Activity activity)
startBounds.offset(-globalOffset.x, -globalOffset.y);
finalBounds.offset(-globalOffset.x, -globalOffset.y);

float startScale;
final float startScale;
if ((float) finalBounds.width() / finalBounds.height()
> (float) startBounds.width() / startBounds.height()) {
startScale = (float) startBounds.height() / finalBounds.height();
Expand All @@ -68,20 +74,40 @@ private static void zoomImageFromThumb(final View thumbView, Activity activity)
startBounds.bottom += deltaHeight;
}

final Pair<Rect, Rect> bounds = new Pair<>(startBounds, finalBounds);
thumbView.setAlpha(0f);
mImageZoom.setVisibility(View.VISIBLE);

mImageZoom.setPivotX(0f);
mImageZoom.setPivotY(0f);
final float startScaleFinal = startScale;

zoomIn(mImageZoom, bounds, startScale);

mImageZoom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zoomOut(thumbView, (ImageView) v, bounds, startScaleFinal);
}
});

}


private static void zoomIn(final ImageView imageZoom, Pair<Rect, Rect> bounds, float startScale) {
if (isLandScapeScreen)
rotate(imageZoom, 90);

imageZoom.setVisibility(View.VISIBLE);

imageZoom.setPivotX(0f);
imageZoom.setPivotY(0f);

AnimatorSet set = new AnimatorSet();
set
.play(ObjectAnimator.ofFloat(mImageZoom, View.X, startBounds.left,
finalBounds.left))
.with(ObjectAnimator.ofFloat(mImageZoom, View.Y, startBounds.top,
finalBounds.top))
.with(ObjectAnimator.ofFloat(mImageZoom, View.SCALE_X, startScale, 1f))
.with(ObjectAnimator.ofFloat(mImageZoom, View.SCALE_Y, startScale, 1f));
.play(ObjectAnimator.ofFloat(imageZoom, View.X, bounds.first.left,
bounds.second.left))
.with(ObjectAnimator.ofFloat(imageZoom, View.Y, bounds.first.top,
bounds.second.top))
.with(ObjectAnimator.ofFloat(imageZoom, View.SCALE_X, startScale, 1f))
.with(ObjectAnimator.ofFloat(imageZoom, View.SCALE_Y, startScale, 1f));
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
Expand All @@ -96,44 +122,58 @@ public void onAnimationCancel(Animator animation) {
});
set.start();
mCurrentAnimator = set;
}

private static void zoomOut(final View thumb, final ImageView imageZoom, final Pair<Rect, Rect> bounds, final float scale) {
if (isLandScapeScreen)
rotate(imageZoom, 0);

final float startScaleFinal = startScale;
mImageZoom.setOnClickListener(new View.OnClickListener() {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}

imageZoom.setBackgroundColor(0);
AnimatorSet set = new AnimatorSet();
set
.play(ObjectAnimator.ofFloat(imageZoom, View.X, bounds.first.left))
.with(ObjectAnimator.ofFloat(imageZoom, View.Y, bounds.first.top))
.with(ObjectAnimator
.ofFloat(imageZoom, View.SCALE_X, scale))
.with(ObjectAnimator
.ofFloat(imageZoom, View.SCALE_Y, scale));
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onClick(View view) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}

mImageZoom.setBackgroundColor(0);
AnimatorSet set = new AnimatorSet();
set
.play(ObjectAnimator.ofFloat(mImageZoom, View.X, startBounds.left))
.with(ObjectAnimator.ofFloat(mImageZoom, View.Y, startBounds.top))
.with(ObjectAnimator
.ofFloat(mImageZoom, View.SCALE_X, startScaleFinal))
.with(ObjectAnimator
.ofFloat(mImageZoom, View.SCALE_Y, startScaleFinal));
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
thumbView.setAlpha(1f);
mImageZoom.setVisibility(View.GONE);
mCurrentAnimator = null;
}

@Override
public void onAnimationCancel(Animator animation) {
thumbView.setAlpha(1f);
mImageZoom.setVisibility(View.GONE);
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;
public void onAnimationEnd(Animator animation) {
thumb.setAlpha(1f);
imageZoom.setVisibility(View.GONE);
mCurrentAnimator = null;
removeView(container.getChildCount() - 1);
}

@Override
public void onAnimationCancel(Animator animation) {
thumb.setAlpha(1f);
imageZoom.setVisibility(View.GONE);
mCurrentAnimator = null;
removeView(container.getChildCount() - 1);
}
});
set.start();
mCurrentAnimator = set;
}

private static void rotate(ImageView imageView, float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);

rotateAnim.setDuration(ROTATION_DELAY);
rotateAnim.setFillAfter(true);
imageView.startAnimation(rotateAnim);
}

private static void removeView(int index){
container.removeViewAt(index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.widget.ImageButton;

import com.github.oliveiradev.image_zoom.lib.R;
import com.github.oliveiradev.image_zoom.lib.animation.ZoomAnimation;

import java.util.Timer;
import java.util.TimerTask;

/**
* Created by felipe on 22/04/16.
*/
Expand All @@ -21,6 +28,7 @@ public class ImageZoom extends ImageButton {

private Drawable mForegroundDrawable;
private Context context;
private boolean landScapeZoom = false;

private Rect mCachedBounds = new Rect();

Expand All @@ -34,12 +42,14 @@ public ImageZoom(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
processAttributes(context, attrs);
}

public ImageZoom(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
processAttributes(context, attrs);
}

private void init() {
Expand Down Expand Up @@ -76,18 +86,31 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCachedBounds.set(0, 0, w, h);
}

public void setLandScapeZoom(boolean isLandScape) {
this.landScapeZoom = isLandScape;
}

private void processAttributes(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ImageZoom);

boolean defaultLandScapeRenderValue = array.getBoolean(R.styleable.ImageZoom_landScapeZoom, false);
setLandScapeZoom(defaultLandScapeRenderValue);

array.recycle();
}

@Override
public boolean performClick() {
if (((ContextThemeWrapper)this.context).getBaseContext() instanceof Activity)
performZoom(((ContextThemeWrapper)this.context).getBaseContext());
if (((ContextThemeWrapper) this.context).getBaseContext() instanceof Activity)
performZoom(((ContextThemeWrapper) this.context).getBaseContext());
else
performZoom(context);

return super.performClick();
}


private void performZoom(Context context){
ZoomAnimation.zoom(this, (Activity) context);
private void performZoom(final Context context) {
ZoomAnimation.zoom(ImageZoom.this, (Activity) context, landScapeZoom);
}
}
6 changes: 6 additions & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ImageZoom">
<attr name="landScapeZoom" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit 880fe9c

Please sign in to comment.