Tusky-App-Android/app/src/main/java/com/keylesspalace/tusky/fragment/ViewMediaFragment.java

106 lines
3.7 KiB
Java
Raw Normal View History

2017-01-20 09:09:10 +01:00
/* Copyright 2017 Andrew Dawson
*
2017-04-10 02:12:31 +02:00
* This file is a part of Tusky.
2017-01-20 09:09:10 +01:00
*
2017-04-10 02:12:31 +02:00
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
2017-01-20 09:09:10 +01:00
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-04-10 02:12:31 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
2017-01-20 09:09:10 +01:00
*
2017-04-10 02:12:31 +02:00
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
2017-01-20 09:09:10 +01:00
2017-05-05 00:55:34 +02:00
package com.keylesspalace.tusky.fragment;
2017-04-07 12:12:26 +02:00
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
2017-05-04 16:16:24 +02:00
import android.widget.ImageView;
2017-05-04 16:16:24 +02:00
import com.github.chrisbanes.photoview.OnOutsidePhotoTapListener;
import com.github.chrisbanes.photoview.OnSingleFlingListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.keylesspalace.tusky.R;
2017-03-09 21:21:55 +01:00
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
public class ViewMediaFragment extends BaseFragment {
public interface OnDismissListener {
void onDismiss();
}
2017-04-07 12:12:26 +02:00
2017-03-10 18:38:49 +01:00
private PhotoViewAttacher attacher;
private OnDismissListener onDismissListener;
2017-03-10 18:38:49 +01:00
public static ViewMediaFragment newInstance(String url) {
Bundle arguments = new Bundle();
ViewMediaFragment fragment = new ViewMediaFragment();
arguments.putString("url", url);
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
onDismissListener = (OnDismissListener) context;
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_view_media, container, false);
2017-06-22 20:59:12 +02:00
PhotoView photoView = (PhotoView) rootView.findViewById(R.id.view_media_image);
Bundle arguments = getArguments();
String url = arguments.getString("url");
2017-03-09 21:21:55 +01:00
2017-03-10 18:38:49 +01:00
attacher = new PhotoViewAttacher(photoView);
// Clicking outside the photo closes the viewer.
2017-05-04 16:16:24 +02:00
attacher.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
@Override
2017-05-04 16:16:24 +02:00
public void onOutsidePhotoTap(ImageView imageView) {
onDismissListener.onDismiss();
}
});
/* A vertical swipe motion also closes the viewer. This is especially useful when the photo
* mostly fills the screen so clicking outside is difficult. */
2017-05-04 16:16:24 +02:00
attacher.setOnSingleFlingListener(new OnSingleFlingListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
2017-05-04 16:16:24 +02:00
float velocityY) {
if (Math.abs(velocityY) > Math.abs(velocityX)) {
onDismissListener.onDismiss();
return true;
}
return false;
}
});
2017-03-09 21:21:55 +01:00
Picasso.with(getContext())
.load(url)
.into(photoView, new Callback() {
@Override
public void onSuccess() {
rootView.findViewById(R.id.view_media_progress).setVisibility(View.GONE);
2017-03-09 21:21:55 +01:00
attacher.update();
}
@Override
2017-06-22 20:59:12 +02:00
public void onError() {}
2017-03-09 21:21:55 +01:00
});
return rootView;
}
}