Search in sources :

Example 16 with PhotoView

use of uk.co.senab.photoview.PhotoView in project weiciyuan by qii.

the class GeneralPictureFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.gallery_general_layout, container, false);
    photoView = (PhotoView) view.findViewById(R.id.animation);
    if (SettingUtility.allowClickToCloseGallery()) {
        photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {

            @Override
            public void onViewTap(View view, float x, float y) {
                getActivity().onBackPressed();
            }
        });
    }
    LongClickListener longClickListener = ((ContainerFragment) getParentFragment()).getLongClickListener();
    photoView.setOnLongClickListener(longClickListener);
    final String path = getArguments().getString("path");
    boolean animateIn = getArguments().getBoolean("animationIn");
    final AnimationRect rect = getArguments().getParcelable("rect");
    if (!animateIn) {
        new MyAsyncTask<Void, Bitmap, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap bitmap = ImageUtility.decodeBitmapFromSDCard(path, IMAGEVIEW_SOFT_LAYER_MAX_WIDTH, IMAGEVIEW_SOFT_LAYER_MAX_HEIGHT);
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                photoView.setImageBitmap(bitmap);
            }
        }.executeOnIO();
        return view;
    }
    final Bitmap bitmap = ImageUtility.decodeBitmapFromSDCard(path, IMAGEVIEW_SOFT_LAYER_MAX_WIDTH, IMAGEVIEW_SOFT_LAYER_MAX_HEIGHT);
    photoView.setImageBitmap(bitmap);
    final Runnable endAction = new Runnable() {

        @Override
        public void run() {
            Bundle bundle = getArguments();
            bundle.putBoolean("animationIn", false);
        }
    };
    photoView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            if (rect == null) {
                photoView.getViewTreeObserver().removeOnPreDrawListener(this);
                endAction.run();
                return true;
            }
            final Rect startBounds = new Rect(rect.scaledBitmapRect);
            final Rect finalBounds = AnimationUtility.getBitmapRectFromImageView(photoView);
            if (finalBounds == null) {
                photoView.getViewTreeObserver().removeOnPreDrawListener(this);
                endAction.run();
                return true;
            }
            float startScale = (float) finalBounds.width() / startBounds.width();
            if (startScale * startBounds.height() > finalBounds.height()) {
                startScale = (float) finalBounds.height() / startBounds.height();
            }
            int deltaTop = startBounds.top - finalBounds.top;
            int deltaLeft = startBounds.left - finalBounds.left;
            photoView.setPivotY((photoView.getHeight() - finalBounds.height()) / 2);
            photoView.setPivotX((photoView.getWidth() - finalBounds.width()) / 2);
            photoView.setScaleX(1 / startScale);
            photoView.setScaleY(1 / startScale);
            photoView.setTranslationX(deltaLeft);
            photoView.setTranslationY(deltaTop);
            photoView.animate().translationY(0).translationX(0).scaleY(1).scaleX(1).setDuration(ANIMATION_DURATION).setInterpolator(new AccelerateDecelerateInterpolator()).withEndAction(endAction);
            AnimatorSet animationSet = new AnimatorSet();
            animationSet.setDuration(ANIMATION_DURATION);
            animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
            animationSet.playTogether(ObjectAnimator.ofFloat(photoView, "clipBottom", AnimationRect.getClipBottom(rect, finalBounds), 0));
            animationSet.playTogether(ObjectAnimator.ofFloat(photoView, "clipRight", AnimationRect.getClipRight(rect, finalBounds), 0));
            animationSet.playTogether(ObjectAnimator.ofFloat(photoView, "clipTop", AnimationRect.getClipTop(rect, finalBounds), 0));
            animationSet.playTogether(ObjectAnimator.ofFloat(photoView, "clipLeft", AnimationRect.getClipLeft(rect, finalBounds), 0));
            animationSet.start();
            photoView.getViewTreeObserver().removeOnPreDrawListener(this);
            return true;
        }
    });
    return view;
}
Also used : Rect(android.graphics.Rect) AnimationRect(org.qii.weiciyuan.support.lib.AnimationRect) Bundle(android.os.Bundle) AnimationRect(org.qii.weiciyuan.support.lib.AnimationRect) AnimatorSet(android.animation.AnimatorSet) PhotoView(uk.co.senab.photoview.PhotoView) View(android.view.View) PhotoViewAttacher(uk.co.senab.photoview.PhotoViewAttacher) Bitmap(android.graphics.Bitmap) AccelerateDecelerateInterpolator(android.view.animation.AccelerateDecelerateInterpolator) ViewTreeObserver(android.view.ViewTreeObserver)

Aggregations

PhotoView (uk.co.senab.photoview.PhotoView)16 View (android.view.View)12 PhotoViewAttacher (uk.co.senab.photoview.PhotoViewAttacher)8 Bitmap (android.graphics.Bitmap)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 AnimatorSet (android.animation.AnimatorSet)2 Rect (android.graphics.Rect)2 BitmapDrawable (android.graphics.drawable.BitmapDrawable)2 Bundle (android.os.Bundle)2 Nullable (android.support.annotation.Nullable)2 ViewTreeObserver (android.view.ViewTreeObserver)2 AccelerateDecelerateInterpolator (android.view.animation.AccelerateDecelerateInterpolator)2 WebView (android.webkit.WebView)2 File (java.io.File)2 AnimationRect (org.qii.weiciyuan.support.lib.AnimationRect)2 CircleProgressView (org.qii.weiciyuan.support.lib.CircleProgressView)2 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1