Skip to content

Commit

Permalink
remove thumbnail animation
Browse files Browse the repository at this point in the history
  • Loading branch information
donglua committed Dec 22, 2017
1 parent a51b5a3 commit 092df3d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class PhotoPickerActivity extends AppCompatActivity {
private boolean menuIsInflated = false;

private boolean showGif = false;
private int columnNumber = DEFAULT_COLUMN_NUMBER;
private ArrayList<String> originalPhotos = null;


Expand Down Expand Up @@ -69,7 +68,7 @@ public class PhotoPickerActivity extends AppCompatActivity {
}

maxCount = getIntent().getIntExtra(EXTRA_MAX_COUNT, DEFAULT_MAX_COUNT);
columnNumber = getIntent().getIntExtra(EXTRA_GRID_COLUMN, DEFAULT_COLUMN_NUMBER);
int columnNumber = getIntent().getIntExtra(EXTRA_GRID_COLUMN, DEFAULT_COLUMN_NUMBER);
originalPhotos = getIntent().getStringArrayListExtra(EXTRA_ORIGINAL_PHOTOS);

pickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentByTag("tag");
Expand Down Expand Up @@ -140,13 +139,9 @@ public void updateTitleDoneItem(){
*/
@Override public void onBackPressed() {
if (imagePagerFragment != null && imagePagerFragment.isVisible()) {
imagePagerFragment.runExitAnimation(new Runnable() {
public void run() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
}
});
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
} else {
super.onBackPressed();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package me.iwf.photopicker.fragment;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import com.bumptech.glide.Glide;

Expand All @@ -26,6 +19,7 @@
import me.iwf.photopicker.adapter.PhotoPagerAdapter;

/**
* Image Pager Fragment
* Created by donglua on 15/6/21.
*/
public class ImagePagerFragment extends Fragment {
Expand All @@ -37,23 +31,6 @@ public class ImagePagerFragment extends Fragment {
private ViewPager mViewPager;
private PhotoPagerAdapter mPagerAdapter;

public final static long ANIM_DURATION = 200L;

public final static String ARG_THUMBNAIL_TOP = "THUMBNAIL_TOP";
public final static String ARG_THUMBNAIL_LEFT = "THUMBNAIL_LEFT";
public final static String ARG_THUMBNAIL_WIDTH = "THUMBNAIL_WIDTH";
public final static String ARG_THUMBNAIL_HEIGHT = "THUMBNAIL_HEIGHT";
public final static String ARG_HAS_ANIM = "HAS_ANIM";

private int thumbnailTop = 0;
private int thumbnailLeft = 0;
private int thumbnailWidth = 0;
private int thumbnailHeight = 0;

private boolean hasAnim = false;

private final ColorMatrix colorizerMatrix = new ColorMatrix();

private int currentItem = 0;


Expand All @@ -64,27 +41,12 @@ public static ImagePagerFragment newInstance(List<String> paths, int currentItem
Bundle args = new Bundle();
args.putStringArray(ARG_PATH, paths.toArray(new String[paths.size()]));
args.putInt(ARG_CURRENT_ITEM, currentItem);
args.putBoolean(ARG_HAS_ANIM, false);

f.setArguments(args);

return f;
}


public static ImagePagerFragment newInstance(List<String> paths, int currentItem, int[] screenLocation, int thumbnailWidth, int thumbnailHeight) {

ImagePagerFragment f = newInstance(paths, currentItem);

f.getArguments().putInt(ARG_THUMBNAIL_LEFT, screenLocation[0]);
f.getArguments().putInt(ARG_THUMBNAIL_TOP, screenLocation[1]);
f.getArguments().putInt(ARG_THUMBNAIL_WIDTH, thumbnailWidth);
f.getArguments().putInt(ARG_THUMBNAIL_HEIGHT, thumbnailHeight);
f.getArguments().putBoolean(ARG_HAS_ANIM, true);

return f;
}

@Override
public void onResume() {
super.onResume();
Expand Down Expand Up @@ -119,12 +81,7 @@ public void setPhotos(List<String> paths, int currentItem) {
paths = new ArrayList<>(Arrays.asList(pathArr));
}

hasAnim = bundle.getBoolean(ARG_HAS_ANIM);
currentItem = bundle.getInt(ARG_CURRENT_ITEM);
thumbnailTop = bundle.getInt(ARG_THUMBNAIL_TOP);
thumbnailLeft = bundle.getInt(ARG_THUMBNAIL_LEFT);
thumbnailWidth = bundle.getInt(ARG_THUMBNAIL_WIDTH);
thumbnailHeight = bundle.getInt(ARG_THUMBNAIL_HEIGHT);
}

mPagerAdapter = new PhotoPagerAdapter(Glide.with(this), paths);
Expand All @@ -136,161 +93,15 @@ public void setPhotos(List<String> paths, int currentItem) {

View rootView = inflater.inflate(R.layout.__picker_picker_fragment_image_pager, container, false);

mViewPager = (ViewPager) rootView.findViewById(R.id.vp_photos);
mViewPager = rootView.findViewById(R.id.vp_photos);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItem);
mViewPager.setOffscreenPageLimit(5);

// Only run the animation if we're coming from the parent activity, not if
// we're recreated automatically by the window manager (e.g., device rotation)
if (savedInstanceState == null && hasAnim) {
ViewTreeObserver observer = mViewPager.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {

mViewPager.getViewTreeObserver().removeOnPreDrawListener(this);

// Figure out where the thumbnail and full size versions are, relative
// to the screen and each other
int[] screenLocation = new int[2];
mViewPager.getLocationOnScreen(screenLocation);
thumbnailLeft = thumbnailLeft - screenLocation[0];
thumbnailTop = thumbnailTop - screenLocation[1];

runEnterAnimation();

return true;
}
});
}


mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override public void onPageSelected(int position) {
hasAnim = currentItem == position;
}

@Override public void onPageScrollStateChanged(int state) {

}
});

return rootView;
}


/**
* The enter animation scales the picture in from its previous thumbnail
* size/location, colorizing it in parallel. In parallel, the background of the
* activity is fading in. When the pictue is in place, the text description
* drops down.
*/
private void runEnterAnimation() {
final long duration = ANIM_DURATION;

// Set starting values for properties we're going to animate. These
// values scale and position the full size version down to the thumbnail
// size/location, from which we'll animate it back up
mViewPager.setPivotX(0);
mViewPager.setPivotY(0);
mViewPager.setScaleX((float) thumbnailWidth / mViewPager.getWidth());
mViewPager.setScaleY((float) thumbnailHeight / mViewPager.getHeight());
mViewPager.setTranslationX(thumbnailLeft);
mViewPager.setTranslationY(thumbnailTop);

// Animate scale and translation to go from thumbnail to full size
mViewPager.animate()
.setDuration(duration)
.scaleX(1)
.scaleY(1)
.translationX(0)
.translationY(0)
.setInterpolator(new DecelerateInterpolator());

// Fade in the black background
ObjectAnimator bgAnim = ObjectAnimator.ofInt(mViewPager.getBackground(), "alpha", 0, 255);
bgAnim.setDuration(duration);
bgAnim.start();

// Animate a color filter to take the image from grayscale to full color.
// This happens in parallel with the image scaling and moving into place.
ObjectAnimator colorizer = ObjectAnimator.ofFloat(ImagePagerFragment.this,
"saturation", 0, 1);
colorizer.setDuration(duration);
colorizer.start();

}


/**
* The exit animation is basically a reverse of the enter animation, except that if
* the orientation has changed we simply scale the picture back into the center of
* the screen.
*
* @param endAction This action gets run after the animation completes (this is
* when we actually switch activities)
*/
public void runExitAnimation(final Runnable endAction) {

if (!getArguments().getBoolean(ARG_HAS_ANIM, false) || !hasAnim) {
endAction.run();
return;
}

final long duration = ANIM_DURATION;

// Animate image back to thumbnail size/location
mViewPager.animate()
.setDuration(duration)
.setInterpolator(new AccelerateInterpolator())
.scaleX((float) thumbnailWidth / mViewPager.getWidth())
.scaleY((float) thumbnailHeight / mViewPager.getHeight())
.translationX(thumbnailLeft)
.translationY(thumbnailTop)
.setListener(new Animator.AnimatorListener() {
@Override public void onAnimationStart(Animator animation) {
}
@Override public void onAnimationEnd(Animator animation) {
endAction.run();
}
@Override public void onAnimationCancel(Animator animation) {
}
@Override public void onAnimationRepeat(Animator animation) {
}
});

// Fade out background
ObjectAnimator bgAnim = ObjectAnimator.ofInt(mViewPager.getBackground(), "alpha", 0);
bgAnim.setDuration(duration);
bgAnim.start();

// Animate a color filter to take the image back to grayscale,
// in parallel with the image scaling and moving into place.
ObjectAnimator colorizer =
ObjectAnimator.ofFloat(ImagePagerFragment.this, "saturation", 1, 0);
colorizer.setDuration(duration);
colorizer.start();
}


/**
* This is called by the colorizing animator. It sets a saturation factor that is then
* passed onto a filter on the picture's drawable.
* @param value saturation
*/
public void setSaturation(float value) {
colorizerMatrix.setSaturation(value);
ColorMatrixColorFilter colorizerFilter = new ColorMatrixColorFilter(colorizerMatrix);
mViewPager.getBackground().setColorFilter(colorizerFilter);
}


public ViewPager getViewPager() {
return mViewPager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -174,11 +175,8 @@ public void onResume() {

List<String> photos = photoGridAdapter.getCurrentPhotoPaths();

int[] screenLocation = new int[2];
v.getLocationOnScreen(screenLocation);
ImagePagerFragment imagePagerFragment =
ImagePagerFragment.newInstance(photos, index, screenLocation, v.getWidth(),
v.getHeight());
ImagePagerFragment.newInstance(photos, index);

((PhotoPickerActivity) getActivity()).addImagePagerFragment(imagePagerFragment);
}
Expand Down Expand Up @@ -232,8 +230,7 @@ private void openCamera() {
} catch (IOException e) {
e.printStackTrace();
} catch (ActivityNotFoundException e) {
// TODO No Activity Found to handle Intent
e.printStackTrace();
Log.e("PhotoPickerFragment", "No Activity Found to handle Intent", e);
}
}

Expand Down

1 comment on commit 092df3d

@donglua
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #226

Please sign in to comment.