ReaderPageFragment.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package eu.kanade.mangafeed.ui.fragment;
  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import com.bumptech.glide.Glide;
  9. import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
  10. import eu.kanade.mangafeed.R;
  11. import eu.kanade.mangafeed.data.models.Page;
  12. import eu.kanade.mangafeed.ui.activity.ReaderActivity;
  13. import eu.kanade.mangafeed.util.PageFileTarget;
  14. public class ReaderPageFragment extends Fragment {
  15. public static final String URL_ARGUMENT_KEY = "UrlArgumentKey";
  16. private SubsamplingScaleImageView imageView;
  17. private String mUrl;
  18. public static ReaderPageFragment newInstance(Page page) {
  19. ReaderPageFragment newInstance = new ReaderPageFragment();
  20. Bundle arguments = new Bundle();
  21. arguments.putString(URL_ARGUMENT_KEY, page.getImageUrl());
  22. newInstance.setArguments(arguments);
  23. return newInstance;
  24. }
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setRetainInstance(true);
  29. Bundle arguments = getArguments();
  30. if (arguments != null) {
  31. if (arguments.containsKey(URL_ARGUMENT_KEY)) {
  32. mUrl = arguments.getString(URL_ARGUMENT_KEY);
  33. }
  34. }
  35. }
  36. public void setPage(Page page) {
  37. if (!page.getImageUrl().equals(mUrl)) {
  38. mUrl = page.getImageUrl();
  39. loadImage();
  40. }
  41. }
  42. private void loadImage() {
  43. Glide.with(getActivity())
  44. .load(mUrl)
  45. .downloadOnly(new PageFileTarget(imageView));
  46. }
  47. @Override
  48. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  49. imageView = (SubsamplingScaleImageView)inflater.inflate(R.layout.fragment_page, container, false);
  50. imageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_FIXED);
  51. imageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
  52. imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE);
  53. imageView.setOnTouchListener((view, motionEvent) ->
  54. ((ReaderActivity) getActivity()).getViewPager().onImageTouch(motionEvent));
  55. imageView.setOnImageEventListener(new SubsamplingScaleImageView.OnImageEventListener() {
  56. @Override
  57. public void onReady() {
  58. imageView.setVisibility(View.VISIBLE);
  59. }
  60. @Override
  61. public void onImageLoaded() {
  62. }
  63. @Override
  64. public void onPreviewLoadError(Exception e) {
  65. }
  66. @Override
  67. public void onImageLoadError(Exception e) {
  68. }
  69. @Override
  70. public void onTileLoadError(Exception e) {
  71. }
  72. });
  73. return imageView;
  74. }
  75. @Override
  76. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  77. super.onActivityCreated(savedInstanceState);
  78. loadImage();
  79. }
  80. }