From 092df3dbae95089a5c3e05fd219db7f161a563b2 Mon Sep 17 00:00:00 2001 From: donglua Date: Fri, 22 Dec 2017 14:52:55 +0800 Subject: [PATCH] remove thumbnail animation --- .../iwf/photopicker/PhotoPickerActivity.java | 13 +- .../fragment/ImagePagerFragment.java | 193 +----------------- .../fragment/PhotoPickerFragment.java | 9 +- 3 files changed, 9 insertions(+), 206 deletions(-) diff --git a/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java b/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java index 37cb63d..4ad78f5 100644 --- a/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java +++ b/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java @@ -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 originalPhotos = null; @@ -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"); @@ -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(); } diff --git a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java index 81deea3..b65a74e 100644 --- a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java +++ b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java @@ -1,9 +1,5 @@ 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; @@ -11,9 +7,6 @@ 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; @@ -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 { @@ -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; @@ -64,27 +41,12 @@ public static ImagePagerFragment newInstance(List 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 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(); @@ -119,12 +81,7 @@ public void setPhotos(List 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); @@ -136,161 +93,15 @@ public void setPhotos(List 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; } diff --git a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java index 1d28e4a..6dcd26b 100644 --- a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java +++ b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java @@ -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; @@ -174,11 +175,8 @@ public void onResume() { List 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); } @@ -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); } }