Exemplo n.º 1
0
  private void animateIn(final Dialog dialog) {
    RelativeLayout.LayoutParams params =
        (RelativeLayout.LayoutParams) zoomableImageView.getLayoutParams();
    params.width = rect.right;
    params.height = rect.bottom;
    zoomableImageView.setLayoutParams(params);

    zoomableImageView.setX(rect.left);
    zoomableImageView.setY(rect.top - statusBarHeightCorrection);
    zoomableImageView.setAlpha(0.0f);
    zoomableImageView.setImageBitmap(bitmap);

    WindowManager win = getActivity().getWindowManager();
    Display d = win.getDefaultDisplay();
    int displayWidth = d.getWidth(); // Width of the actual device
    int displayHeight = d.getHeight() + statusBarHeightCorrection;

    ValueAnimator animWidth = ValueAnimator.ofInt(rect.right, displayWidth);
    animWidth.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = zoomableImageView.getLayoutParams();
            layoutParams.width = val;
            zoomableImageView.setLayoutParams(layoutParams);
          }
        });
    animWidth.setDuration(500);
    animWidth.setInterpolator(new LinearOutSlowInInterpolator());
    animWidth.start();

    ValueAnimator animHeight = ValueAnimator.ofInt(rect.bottom, displayHeight);
    animHeight.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = zoomableImageView.getLayoutParams();
            layoutParams.height = val;
            zoomableImageView.setLayoutParams(layoutParams);
          }
        });
    animHeight.setDuration(500);
    animHeight.setInterpolator(new LinearOutSlowInInterpolator());

    animHeight.start();

    if (statusBarHeightCorrection > 0) {
      zoomableImageView.animate().y(0.0f).setDuration(300).start();
    }

    ValueAnimator animDim = ValueAnimator.ofFloat(0.0f, 0.5f);
    animDim.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            layoutParams.copyFrom(dialog.getWindow().getAttributes());
            layoutParams.dimAmount = (Float) valueAnimator.getAnimatedValue();
            dialog.getWindow().setAttributes(layoutParams);
          }
        });
    animDim.setDuration(300);
    animDim.setStartDelay(300);
    animDim.start();
    zoomableImageView.animate().alpha(1.0f).setDuration(300).start();
  }
  @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);
        }
      }.executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR);

      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);
                  return true;
                }

                final Rect startBounds = new Rect(rect.scaledBitmapRect);
                final Rect finalBounds = AnimationUtility.getBitmapRectFromImageView(photoView);

                if (finalBounds == null) {
                  photoView.getViewTreeObserver().removeOnPreDrawListener(this);
                  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;
  }
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

      View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
      final PhotoView pvComic = (PhotoView) itemView.findViewById(R.id.ivComic);
      itemView.setTag(position);

      final TextView tvAlt = (TextView) itemView.findViewById(R.id.tvAlt);

      if (PrefHelper.altByDefault()) {
        tvAlt.setVisibility(View.VISIBLE);
      }
      tvAlt.setText(PrefHelper.getAlt(sFavorites[position]));

      // fix for issue #2
      pvComic.setOnDoubleTapListener(
          new GestureDetector.OnDoubleTapListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
              if (pvComic.getScale() < 0.5f * pvComic.getMaximumScale()) {
                pvComic.setScale(0.5f * pvComic.getMaximumScale(), true);
              } else if (pvComic.getScale() < pvComic.getMaximumScale()) {
                pvComic.setScale(pvComic.getMaximumScale(), true);
              } else {
                pvComic.setScale(1.0f, true);
              }
              return true;
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
              if (!PrefHelper.altLongTap()) {
                if (PrefHelper.classicAltStyle()) {
                  toggleVisibility(tvAlt);
                } else {
                  android.support.v7.app.AlertDialog.Builder mDialog =
                      new android.support.v7.app.AlertDialog.Builder(getActivity());
                  mDialog.setMessage(tvAlt.getText());
                  mDialog.show();
                }
              }
              return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
              if (e.getAction() == MotionEvent.ACTION_UP) {
                fingerLifted = true;
              }
              if (e.getAction() == MotionEvent.ACTION_DOWN) {
                fingerLifted = false;
              }
              return false;
            }
          });

      // Setup alt text and LongClickListener
      pvComic.setOnLongClickListener(
          new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
              if (fingerLifted && PrefHelper.altLongTap()) {
                if (PrefHelper.altVibration()) {
                  Vibrator vi =
                      (Vibrator) getActivity().getSystemService(MainActivity.VIBRATOR_SERVICE);
                  vi.vibrate(10);
                }
                // tvAlt.setText(PrefHelper.getAlt(sFavorites[sFavoriteIndex]));
                if (PrefHelper.classicAltStyle()) {
                  toggleVisibility(tvAlt);
                } else {
                  android.support.v7.app.AlertDialog.Builder mDialog =
                      new android.support.v7.app.AlertDialog.Builder(getActivity());
                  mDialog.setMessage(tvAlt.getText());
                  mDialog.show();
                }
              }
              return true;
            }
          });
      // setup the title text view
      TextView tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
      tvTitle.setText(PrefHelper.getTitle(sFavorites[position]));

      if (PrefHelper.invertColors()) {
        float[] colorMatrix_Negative = {
          -1.0f,
          0,
          0,
          0,
          255, // red
          0,
          -1.0f,
          0,
          0,
          255, // green
          0,
          0,
          -1.0f,
          0,
          255, // blue
          0,
          0,
          0,
          1.0f,
          0 // alpha
        };
        ColorFilter cf = new ColorMatrixColorFilter(colorMatrix_Negative);
        pvComic.setColorFilter(cf);
      }

      // load the image
      pvComic.setImageBitmap(mComicMap.get(position).getBitmap());
      if (Arrays.binarySearch(
              mContext.getResources().getIntArray(R.array.large_comics), sFavorites[sFavoriteIndex])
          >= 0) {
        pvComic.setMaximumScale(7.0f);
      }
      // Disable ViewPager scrolling when the user zooms into an image
      pvComic.setOnMatrixChangeListener(
          new PhotoViewAttacher.OnMatrixChangedListener() {
            @Override
            public void onMatrixChanged(RectF rectF) {
              if (pvComic.getScale() > 1.4) {
                mPager.setLocked(true);
              } else {
                mPager.setLocked(false);
              }
            }
          });

      container.addView(itemView);
      return itemView;
    }