ViewMediaFragment.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright 2017 Andrew Dawson
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. package com.keylesspalace.tusky;
  16. import android.os.Bundle;
  17. import android.support.annotation.Nullable;
  18. import android.support.v4.app.DialogFragment;
  19. import android.view.LayoutInflater;
  20. import android.view.MotionEvent;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.view.WindowManager;
  24. import com.squareup.picasso.Callback;
  25. import com.squareup.picasso.Picasso;
  26. import butterknife.BindView;
  27. import butterknife.ButterKnife;
  28. import uk.co.senab.photoview.PhotoView;
  29. import uk.co.senab.photoview.PhotoViewAttacher;
  30. public class ViewMediaFragment extends DialogFragment {
  31. private PhotoViewAttacher attacher;
  32. @BindView(R.id.view_media_image) PhotoView photoView;
  33. public static ViewMediaFragment newInstance(String url) {
  34. Bundle arguments = new Bundle();
  35. ViewMediaFragment fragment = new ViewMediaFragment();
  36. arguments.putString("url", url);
  37. fragment.setArguments(arguments);
  38. return fragment;
  39. }
  40. @Override
  41. public void onCreate(@Nullable Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog_FullScreen);
  44. }
  45. @Override
  46. public void onResume() {
  47. ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
  48. params.width = WindowManager.LayoutParams.MATCH_PARENT;
  49. params.height = WindowManager.LayoutParams.MATCH_PARENT;
  50. getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
  51. super.onResume();
  52. }
  53. @Override
  54. public View onCreateView(LayoutInflater inflater, final ViewGroup container,
  55. Bundle savedInstanceState) {
  56. View rootView = inflater.inflate(R.layout.fragment_view_media, container, false);
  57. ButterKnife.bind(this, rootView);
  58. Bundle arguments = getArguments();
  59. String url = arguments.getString("url");
  60. attacher = new PhotoViewAttacher(photoView);
  61. // Clicking outside the photo closes the viewer.
  62. attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
  63. @Override
  64. public void onPhotoTap(View view, float x, float y) {
  65. }
  66. @Override
  67. public void onOutsidePhotoTap() {
  68. dismiss();
  69. }
  70. });
  71. /* A vertical swipe motion also closes the viewer. This is especially useful when the photo
  72. * mostly fills the screen so clicking outside is difficult. */
  73. attacher.setOnSingleFlingListener(new PhotoViewAttacher.OnSingleFlingListener() {
  74. @Override
  75. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  76. float velocityY) {
  77. if (Math.abs(velocityY) > Math.abs(velocityX)) {
  78. dismiss();
  79. return true;
  80. }
  81. return false;
  82. }
  83. });
  84. Picasso.with(getContext())
  85. .load(url)
  86. .into(photoView, new Callback() {
  87. @Override
  88. public void onSuccess() {
  89. attacher.update();
  90. }
  91. @Override
  92. public void onError() {
  93. }
  94. });
  95. return rootView;
  96. }
  97. @Override
  98. public void onDestroyView() {
  99. attacher.cleanup();
  100. super.onDestroyView();
  101. }
  102. }