chinwag-android/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java

125 lines
4.1 KiB
Java
Raw Normal View History

2017-01-20 19:09:10 +11:00
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
2017-03-21 12:44:30 +11:00
* Lesser 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 19:09:10 +11:00
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-03-21 12:44:30 +11:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
2017-01-20 19:09:10 +11:00
*
2017-03-21 12:44:30 +11:00
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If
* not, see <http://www.gnu.org/licenses/>. */
2017-01-20 19:09:10 +11:00
package com.keylesspalace.tusky;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
2017-03-10 07:21:55 +11:00
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import butterknife.BindView;
import butterknife.ButterKnife;
2017-03-10 07:21:55 +11:00
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
public class ViewMediaFragment extends DialogFragment {
2017-03-11 04:38:49 +11:00
private PhotoViewAttacher attacher;
@BindView(R.id.view_media_image) PhotoView photoView;
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 onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog_FullScreen);
}
@Override
public void onResume() {
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
super.onResume();
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_media, container, false);
ButterKnife.bind(this, rootView);
Bundle arguments = getArguments();
String url = arguments.getString("url");
2017-03-10 07:21:55 +11:00
2017-03-11 04:38:49 +11:00
attacher = new PhotoViewAttacher(photoView);
// Clicking outside the photo closes the viewer.
2017-03-10 07:21:55 +11:00
attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
@Override
2017-03-10 07:21:55 +11:00
public void onPhotoTap(View view, float x, float y) {
}
@Override
public void onOutsidePhotoTap() {
dismiss();
}
});
/* A vertical swipe motion also closes the viewer. This is especially useful when the photo
* mostly fills the screen so clicking outside is difficult. */
attacher.setOnSingleFlingListener(new PhotoViewAttacher.OnSingleFlingListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (Math.abs(velocityY) > Math.abs(velocityX)) {
dismiss();
return true;
}
return false;
}
});
2017-03-10 07:21:55 +11:00
Picasso.with(getContext())
.load(url)
.into(photoView, new Callback() {
@Override
public void onSuccess() {
attacher.update();
}
@Override
public void onError() {
}
});
return rootView;
}
2017-03-11 04:38:49 +11:00
@Override
public void onDestroyView() {
attacher.cleanup();
super.onDestroyView();
}
}