handle orientation changes manually to prevent video glitches

This commit is contained in:
tibbi 2016-10-03 23:27:56 +02:00
parent 48fd0c9b8e
commit 53c7c78801
7 changed files with 77 additions and 40 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.simplemobiletools.gallery"
xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemobiletools.gallery">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
@ -54,6 +54,7 @@
<activity
android:name=".activities.ViewPagerActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:parentActivityName=".activities.MediaActivity"
android:theme="@style/FullScreenTheme">
@ -101,6 +102,7 @@
<activity
android:name=".activities.VideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/FullScreenTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.gallery.activities;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
@ -20,6 +21,7 @@ import java.io.File;
public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragment.FragmentClickListener {
private static ActionBar mActionbar;
private static Uri mUri;
private static ViewPagerFragment mFragment;
private static boolean mIsFullScreen;
@ -44,15 +46,21 @@ public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragm
bundle.putSerializable(Constants.MEDIUM, medium);
if (savedInstanceState == null) {
final ViewPagerFragment fragment = (mIsVideo ? new VideoFragment() : new PhotoFragment());
fragment.setListener(this);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).commit();
mFragment = (mIsVideo ? new VideoFragment() : new PhotoFragment());
mFragment.setListener(this);
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, mFragment).commit();
}
hideSystemUI();
setTitle(Utils.getFilename(mUri.toString()));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mFragment.confChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.photo_video_menu, menu);

View File

@ -164,6 +164,13 @@ public class ViewPagerActivity extends SimpleActivity
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
adapter.confChanged(mPos);
}
private void setAsWallpaper() {
final Bitmap bitmap = BitmapFactory.decodeFile(getCurrentFile().getAbsolutePath());
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

View File

@ -67,6 +67,15 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter {
}
}
public void confChanged(int pos) {
for (int i = -1; i <= 1; i++) {
final ViewPagerFragment fragment = mFragments.get(pos + i);
if (fragment != null) {
fragment.confChanged();
}
}
}
public void updateItems(List<Medium> newPaths) {
mMedia.clear();
mMedia.addAll(newPaths);

View File

@ -15,36 +15,33 @@ import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.models.Medium;
public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener {
private View mView;
private SubsamplingScaleImageView mSubsamplingView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.pager_photo_item, container, false);
mView = inflater.inflate(R.layout.pager_photo_item, container, false);
final Medium medium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (medium == null)
return view;
return mView;
final SubsamplingScaleImageView subsamplingView = (SubsamplingScaleImageView) view.findViewById(R.id.photo_view);
mSubsamplingView = (SubsamplingScaleImageView) mView.findViewById(R.id.photo_view);
if (medium.isGif()) {
subsamplingView.setVisibility(View.GONE);
final ImageView imageView = (ImageView) view.findViewById(R.id.gif_view);
mSubsamplingView.setVisibility(View.GONE);
final ImageView imageView = (ImageView) mView.findViewById(R.id.gif_view);
imageView.setVisibility(View.VISIBLE);
Glide.with(getContext()).load(medium.getPath()).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
imageView.setOnClickListener(this);
} else {
subsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
subsamplingView.setImage(ImageSource.uri(medium.getPath()));
subsamplingView.setMaxScale(4f);
subsamplingView.setMinimumTileDpi(200);
subsamplingView.setOnClickListener(this);
mSubsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
mSubsamplingView.setImage(ImageSource.uri(medium.getPath()));
mSubsamplingView.setMaxScale(4f);
mSubsamplingView.setMinimumTileDpi(200);
mSubsamplingView.setOnClickListener(this);
}
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
return mView;
}
@Override
@ -57,6 +54,11 @@ public class PhotoFragment extends ViewPagerFragment implements View.OnClickList
}
@Override
public void confChanged() {
}
@Override
public void onClick(View v) {
photoClicked();

View File

@ -47,6 +47,7 @@ public class VideoFragment extends ViewPagerFragment
private SeekBar mSeekBar;
private Medium mMedium;
private View mTimeHolder;
private View mView;
private boolean mIsPlaying;
private boolean mIsDragged;
@ -56,7 +57,7 @@ public class VideoFragment extends ViewPagerFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.pager_video_item, container, false);
mView = inflater.inflate(R.layout.pager_video_item, container, false);
mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (savedInstanceState != null) {
@ -65,25 +66,25 @@ public class VideoFragment extends ViewPagerFragment
mIsFullscreen = (getActivity().getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) ==
View.SYSTEM_UI_FLAG_FULLSCREEN;
setupPlayer(view);
view.setOnClickListener(this);
setupPlayer();
mView.setOnClickListener(this);
return view;
return mView;
}
private void setupPlayer(View view) {
private void setupPlayer() {
if (getActivity() == null)
return;
mPlayOutline = (ImageView) view.findViewById(R.id.video_play_outline);
mPlayOutline = (ImageView) mView.findViewById(R.id.video_play_outline);
mPlayOutline.setOnClickListener(this);
mSurfaceView = (SurfaceView) view.findViewById(R.id.video_surface);
mSurfaceView = (SurfaceView) mView.findViewById(R.id.video_surface);
mSurfaceView.setOnClickListener(this);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
initTimeHolder(view);
initTimeHolder();
}
public void itemDragged() {
@ -98,14 +99,20 @@ public class VideoFragment extends ViewPagerFragment
}
}
private void initTimeHolder(View view) {
mTimeHolder = view.findViewById(R.id.video_time_holder);
@Override
public void confChanged() {
setVideoSize();
initTimeHolder();
}
private void initTimeHolder() {
mTimeHolder = mView.findViewById(R.id.video_time_holder);
final Resources res = getResources();
final int height = Utils.getNavBarHeight(res);
final int left = mTimeHolder.getPaddingLeft();
final int top = mTimeHolder.getPaddingTop();
int right = mTimeHolder.getPaddingRight();
int bottom = mTimeHolder.getPaddingBottom();
int right = (int) getResources().getDimension(R.dimen.timer_padding);
int bottom = 0;
if (Utils.hasNavBar(getActivity())) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
@ -116,9 +123,9 @@ public class VideoFragment extends ViewPagerFragment
mTimeHolder.setPadding(left, top, right, bottom);
}
mCurrTimeView = (TextView) view.findViewById(R.id.video_curr_time);
mDurationView = (TextView) view.findViewById(R.id.video_duration);
mSeekBar = (SeekBar) view.findViewById(R.id.video_seekbar);
mCurrTimeView = (TextView) mView.findViewById(R.id.video_curr_time);
mDurationView = (TextView) mView.findViewById(R.id.video_duration);
mSeekBar = (SeekBar) mView.findViewById(R.id.video_seekbar);
mSeekBar.setOnSeekBarChangeListener(this);
if (mIsFullscreen)
@ -296,14 +303,14 @@ public class VideoFragment extends ViewPagerFragment
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
setVideoSize(width, height);
setVideoSize();
}
private void setVideoSize(int videoWidth, int videoHeight) {
private void setVideoSize() {
if (getActivity() == null)
return;
final float videoProportion = (float) videoWidth / (float) videoHeight;
final float videoProportion = (float) mMediaPlayer.getVideoWidth() / (float) mMediaPlayer.getVideoHeight();
final Display display = getActivity().getWindowManager().getDefaultDisplay();
int screenWidth;
int screenHeight;

View File

@ -13,6 +13,8 @@ public abstract class ViewPagerFragment extends Fragment {
public abstract void systemUiVisibilityChanged(boolean toFullscreen);
public abstract void confChanged();
public interface FragmentClickListener {
void fragmentClicked();
}