Skip to content

Commit 625261c

Browse files
committed
back button functionality
1 parent b6e4fb2 commit 625261c

File tree

1 file changed

+72
-42
lines changed

1 file changed

+72
-42
lines changed

app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java

+72-42
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
import android.support.v4.graphics.BitmapCompat;
3535
import android.support.v7.app.AlertDialog;
3636
import android.util.Log;
37+
import android.view.KeyEvent;
3738
import android.view.MenuItem;
3839
import android.view.View;
3940
import android.view.WindowManager;
4041
import android.view.animation.DecelerateInterpolator;
4142
import android.view.inputmethod.InputMethodManager;
43+
import android.widget.ImageView;
4244
import android.widget.TextView;
4345
import android.widget.Toast;
4446

@@ -146,8 +148,13 @@ public class ShareActivity
146148
private FloatingActionButton zoomInButton;
147149
private FloatingActionButton zoomOutButton;
148150
private FloatingActionButton mainFab;
151+
private PhotoView expandedImageView;
149152
private boolean isFABOpen = false;
150-
153+
private Rect startBounds = new Rect();
154+
private Rect finalBounds = new Rect();
155+
private Point globalOffset = new Point();
156+
private float startScale;
157+
private boolean isZoom = false;
151158

152159
/**
153160
* Called when user taps the submit button.
@@ -783,6 +790,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
783790
return super.onOptionsItemSelected(item);
784791
}
785792

793+
794+
786795
/*
787796
* Get SHA1 of file from input stream
788797
*/
@@ -831,6 +840,7 @@ private void zoomImageFromThumb(final View thumbView, Uri imageuri ) {
831840
if (CurrentAnimator != null) {
832841
CurrentAnimator.cancel();
833842
}
843+
isZoom = true;
834844
ViewUtil.hideKeyboard(ShareActivity.this.findViewById(R.id.titleEdit | R.id.descEdit));
835845
closeFABMenu();
836846
mainFab.setVisibility(View.GONE);
@@ -865,17 +875,17 @@ private void zoomImageFromThumb(final View thumbView, Uri imageuri ) {
865875
scaled = bitmap;
866876
}
867877
// Load the high-resolution "zoomed-in" image.
868-
PhotoView expandedImageView = (PhotoView) findViewById(
878+
expandedImageView = (PhotoView) findViewById(
869879
R.id.expanded_image);
870880
expandedImageView.setImageBitmap(scaled);
871881

872882

873883

874884
// Calculate the starting and ending bounds for the zoomed-in image.
875-
// This step involves lots of math. Yay, math.
876-
final Rect startBounds = new Rect();
877-
final Rect finalBounds = new Rect();
878-
final Point globalOffset = new Point();
885+
// This step involves lots of math.
886+
startBounds = new Rect();
887+
finalBounds = new Rect();
888+
globalOffset = new Point();
879889

880890
// The start bounds are the global visible rectangle of the thumbnail,
881891
// and the final bounds are the global visible rectangle of the container
@@ -892,7 +902,7 @@ private void zoomImageFromThumb(final View thumbView, Uri imageuri ) {
892902
// bounds using the "center crop" technique. This prevents undesirable
893903
// stretching during the animation. Also calculate the start scaling
894904
// factor (the end scaling factor is always 1.0).
895-
float startScale;
905+
896906
if ((float) finalBounds.width() / finalBounds.height()
897907
> (float) startBounds.width() / startBounds.height()) {
898908
// Extend start bounds horizontally
@@ -962,46 +972,66 @@ public void onClick(View view) {
962972
if (CurrentAnimator != null) {
963973
CurrentAnimator.cancel();
964974
}
965-
zoomOutButton.setVisibility(View.GONE);
966-
mainFab.setVisibility(View.VISIBLE);
967-
968-
// Animate the four positioning/sizing properties in parallel,
969-
// back to their original values.
970-
AnimatorSet set = new AnimatorSet();
971-
set.play(ObjectAnimator
972-
.ofFloat(expandedImageView, View.X, startBounds.left))
973-
.with(ObjectAnimator
974-
.ofFloat(expandedImageView,
975-
View.Y,startBounds.top))
976-
.with(ObjectAnimator
977-
.ofFloat(expandedImageView,
978-
View.SCALE_X, startScaleFinal))
979-
.with(ObjectAnimator
980-
.ofFloat(expandedImageView,
981-
View.SCALE_Y, startScaleFinal));
982-
set.setDuration(ShortAnimationDuration);
983-
set.setInterpolator(new DecelerateInterpolator());
984-
set.addListener(new AnimatorListenerAdapter() {
985-
@Override
986-
public void onAnimationEnd(Animator animation) {
987-
thumbView.setAlpha(1f);
988-
expandedImageView.setVisibility(View.GONE);
989-
CurrentAnimator = null;
990-
}
975+
zoomOut(thumbView,startScaleFinal,startBounds);
991976

992-
@Override
993-
public void onAnimationCancel(Animator animation) {
994-
thumbView.setAlpha(1f);
995-
expandedImageView.setVisibility(View.GONE);
996-
CurrentAnimator = null;
997-
}
998-
});
999-
set.start();
1000-
CurrentAnimator = set;
977+
}
978+
979+
});
980+
}
981+
982+
@Override
983+
public boolean onKeyDown(int keyCode, KeyEvent event) {
984+
switch (keyCode) {
985+
case KeyEvent.KEYCODE_BACK:
986+
if(isZoom) {
987+
zoomOut(backgroundImageView, startScale, startBounds);
988+
return true;
989+
}
990+
}
991+
return super.onKeyDown(keyCode,event);
992+
993+
}
1001994

995+
void zoomOut(View thumbView, float startScaleFinal,Rect startBounds){
996+
zoomOutButton.setVisibility(View.GONE);
997+
mainFab.setVisibility(View.VISIBLE);
998+
isZoom = false;
999+
1000+
// Animate the four positioning/sizing properties in parallel,
1001+
// back to their original values.
1002+
AnimatorSet set = new AnimatorSet();
1003+
set.play(ObjectAnimator
1004+
.ofFloat(expandedImageView, View.X, startBounds.left))
1005+
.with(ObjectAnimator
1006+
.ofFloat(expandedImageView,
1007+
View.Y,startBounds.top))
1008+
.with(ObjectAnimator
1009+
.ofFloat(expandedImageView,
1010+
View.SCALE_X, startScaleFinal))
1011+
.with(ObjectAnimator
1012+
.ofFloat(expandedImageView,
1013+
View.SCALE_Y, startScaleFinal));
1014+
set.setDuration(ShortAnimationDuration);
1015+
set.setInterpolator(new DecelerateInterpolator());
1016+
set.addListener(new AnimatorListenerAdapter() {
1017+
@Override
1018+
public void onAnimationEnd(Animator animation) {
1019+
thumbView.setAlpha(1f);
1020+
expandedImageView.setVisibility(View.GONE);
1021+
CurrentAnimator = null;
10021022
}
10031023

1024+
@Override
1025+
public void onAnimationCancel(Animator animation) {
1026+
thumbView.setAlpha(1f);
1027+
expandedImageView.setVisibility(View.GONE);
1028+
CurrentAnimator = null;
1029+
}
10041030
});
1031+
set.start();
1032+
CurrentAnimator = set;
10051033
}
10061034

1035+
1036+
10071037
}

0 commit comments

Comments
 (0)