Skip to content

Commit

Permalink
adding listener for zoom and ended events
Browse files Browse the repository at this point in the history
  • Loading branch information
Viven committed Jan 20, 2017
1 parent 409ac0f commit e8bb7be
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 19 additions & 32 deletions app/src/main/java/com/viven/imagezoom/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.viven.imagezoom.ImageZoomHelper;

public class MainActivity extends AppCompatActivity {

ImageZoomHelper imageZoomHelper;
ImageView imgLogo;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -28,46 +35,26 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imgLogo = (ImageView) findViewById(R.id.imgLogo);

imageZoomHelper = new ImageZoomHelper(this);

// set zoomable tag on views that is to be zoomed
ImageZoomHelper.setViewZoomable(findViewById(R.id.imgLogo));

findViewById(R.id.btnDialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final View image = findViewById(R.id.imgLogo);

final FrameLayout frameLayout = new FrameLayout(MainActivity.this);
ImageZoomHelper.setViewZoomable(imgLogo);

int[] originalXY = new int[2];
image.getLocationInWindow(originalXY);
Glide.with(this)
.load(R.drawable.bigimage)
.into(imgLogo);

final ViewGroup.LayoutParams layoutParams = image.getLayoutParams();
final ViewGroup parent = (ViewGroup) image.getParent();
parent.removeView(image);
frameLayout.addView(image);
imageZoomHelper.addOnZoomListener(new ImageZoomHelper.OnZoomListener() {
@Override
public void onImageZoomStarted(final View view) {

ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) image.getLayoutParams();
marginLayoutParams.leftMargin = originalXY[0];
marginLayoutParams.topMargin = originalXY[1];
// image.setX(originalXY[0]);
// image.setY(originalXY[1]);
}

Dialog dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.addContentView(frameLayout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
dialog.show();
@Override
public void onImageZoomEnded(View view) {

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
frameLayout.removeView(image);
parent.addView(image, layoutParams);
}
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.viven.imagezoom.ImageZoomHelper;

public class RecyclerViewActivity extends AppCompatActivity {
Expand All @@ -21,6 +24,7 @@ public class RecyclerViewActivity extends AppCompatActivity {
SwipeRefreshLayout swipeRefreshLayout;

ImageZoomHelper imageZoomHelper;
ImageZoomHelper.OnZoomListener onZoomListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -41,15 +45,18 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.setAdapter(new RecyclerView.Adapter() {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
String url = "https://media.giphy.com/media/3oz8xRF0v9WMAUVLNK/giphy.gif";

// Wrap ImageView with FrameLayout to avoid RecyclerView issue
FrameLayout frameLayout = new FrameLayout(parent.getContext());
ImageView imageView = new ImageView(RecyclerViewActivity.this);
imageView.setTag(R.id.imageUrl, url);
imageView.setImageResource(R.mipmap.ic_launcher);
ImageZoomHelper.setViewZoomable(imageView);
imageView.setMinimumHeight(400);
frameLayout.addView(imageView);
Glide.with(RecyclerViewActivity.this)
.load("https://media.giphy.com/media/3oz8xRF0v9WMAUVLNK/giphy.gif")
.load(url)
.asGif()
.centerCrop()
.into(imageView);
Expand Down Expand Up @@ -83,10 +90,36 @@ public void run() {
});

imageZoomHelper = new ImageZoomHelper(this);

onZoomListener = new ImageZoomHelper.OnZoomListener() {
@Override
public void onImageZoomStarted(final View view) {

}

@Override
public void onImageZoomEnded(View view) {

}
};
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return imageZoomHelper.onDispatchTouchEvent(ev) || super.dispatchTouchEvent(ev);
}

@Override
protected void onResume() {
super.onResume();

imageZoomHelper.addOnZoomListener(onZoomListener);
}

@Override
protected void onPause() {
super.onPause();

imageZoomHelper.removeOnZoomListener(onZoomListener);
}
}
Binary file added app/src/main/res/drawable/bigimage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 2 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@
android:fitsSystemWindows="true"
tools:context="com.viven.imagezoom.sample.MainActivity">

<Button
android:text="Dialog"
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imgLogo"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
android:layout_width="200dp"
android:layout_height="200dp" />
android:layout_width="300dp"
android:layout_height="300dp" />

</RelativeLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="imageUrl" type="id" />
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.3.0-beta2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Sat Jan 14 21:14:53 MYT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
68 changes: 60 additions & 8 deletions library/src/main/java/com/viven/imagezoom/ImageZoomHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;
import android.view.animation.LayoutAnimationController;
import android.widget.FrameLayout;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
* Created by viventhraarao on 25/11/2016.
Expand All @@ -38,6 +38,8 @@ public class ImageZoomHelper {

private boolean isAnimatingDismiss = false;

private List<OnZoomListener> zoomListeners = new ArrayList<>();

public ImageZoomHelper(Activity activity) {
this.activityWeakReference = new WeakReference<>(activity);
}
Expand Down Expand Up @@ -96,9 +98,15 @@ public boolean onDispatchTouchEvent(MotionEvent ev) {
// setting placeholderView's background to zoomableView's drawingCache
// this avoids flickering when adding/removing views
zoomableView.setDrawingCacheEnabled(true);
placeholderView.setBackgroundDrawable(
new BitmapDrawable(activity.getResources(),
Bitmap.createBitmap(zoomableView.getDrawingCache())));

BitmapDrawable placeholderDrawable = new BitmapDrawable(
activity.getResources(),
Bitmap.createBitmap(zoomableView.getDrawingCache()));
if (Build.VERSION.SDK_INT >= 16) {
placeholderView.setBackground(placeholderDrawable);
} else {
placeholderView.setBackgroundDrawable(placeholderDrawable);
}

// placeholderView takes the place of zoomableView temporarily
parentOfZoomableView.addView(placeholderView, zoomableViewLP.width,
Expand All @@ -114,7 +122,12 @@ public boolean onDispatchTouchEvent(MotionEvent ev) {
@Override
public void run() {
if (dialog != null) {
placeholderView.setBackgroundDrawable(null);
if (Build.VERSION.SDK_INT >= 16) {
placeholderView.setBackground(null);
} else {
placeholderView.setBackgroundDrawable(null);
}

zoomableView.setDrawingCacheEnabled(false);
}
}
Expand All @@ -139,6 +152,7 @@ public void run() {
(int) ((pointerCoords2.y + pointerCoords1.y) / 2)
};

sendZoomEventToListeners(zoomableView, true);
return true;
}
} else {
Expand Down Expand Up @@ -185,7 +199,7 @@ public void run() {
int leftMarginEnd = originalXY[0];
int topMarginEnd = originalXY[1];
float alphaEnd = 0f;

@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float animatedFraction = valueAnimator.getAnimatedFraction();
Expand Down Expand Up @@ -229,14 +243,27 @@ void updateZoomableViewMargins(float left, float top) {
* Dismiss dialog and set views to null for garbage collection
*/
private void dismissDialogAndViews() {
sendZoomEventToListeners(zoomableView, false);

if (zoomableView != null) {
zoomableView.setVisibility(View.VISIBLE);
zoomableView.setDrawingCacheEnabled(true);

BitmapDrawable placeholderDrawable = new BitmapDrawable(
zoomableView.getResources(),
Bitmap.createBitmap(zoomableView.getDrawingCache()));
if (Build.VERSION.SDK_INT >= 16) {
placeholderView.setBackground(placeholderDrawable);
} else {
placeholderView.setBackgroundDrawable(placeholderDrawable);
}

ViewGroup parent = (ViewGroup) zoomableView.getParent();
parent.removeView(zoomableView);
this.parentOfZoomableView.addView(zoomableView, viewIndex, zoomableViewLP);
this.parentOfZoomableView.removeView(placeholderView);

zoomableView.setDrawingCacheEnabled(false);
zoomableView.post(new Runnable() {
@Override
public void run() {
Expand All @@ -252,6 +279,23 @@ public void run() {
isAnimatingDismiss = false;
}

public void addOnZoomListener(OnZoomListener onZoomListener) {
zoomListeners.add(onZoomListener);
}

public void removeOnZoomListener(OnZoomListener onZoomListener) {
zoomListeners.remove(onZoomListener);
}

private void sendZoomEventToListeners(View zoomableView, boolean zoom) {
for (OnZoomListener onZoomListener : zoomListeners) {
if (zoom)
onZoomListener.onImageZoomStarted(zoomableView);
else
onZoomListener.onImageZoomEnded(zoomableView);
}
}

private void dismissDialog() {
if (dialog != null) {
dialog.dismiss();
Expand Down Expand Up @@ -338,4 +382,12 @@ public static void setViewZoomable(View view) {
public static void setZoom(View view, boolean enabled) {
view.setTag(R.id.unzoomable, enabled ? null : new Object());
}

public interface OnZoomListener {

void onImageZoomStarted(View view);

void onImageZoomEnded(View view);

}
}

0 comments on commit e8bb7be

Please sign in to comment.