Skip to content

Commit

Permalink
Add pinch to zoom
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
moneytoo committed Feb 3, 2021
1 parent 1d8399f commit b4600b7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
43 changes: 42 additions & 1 deletion app/src/main/java/com/brouken/player/CustomStyledPlayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.TextView;

import androidx.core.view.GestureDetectorCompat;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.SeekParameters;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.StyledPlayerView;

public final class CustomStyledPlayerView extends StyledPlayerView implements GestureDetector.OnGestureListener {
public final class CustomStyledPlayerView extends StyledPlayerView implements GestureDetector.OnGestureListener, ScaleGestureDetector.OnScaleGestureListener {

private final GestureDetectorCompat mDetector;

Expand All @@ -38,6 +41,9 @@ public final class CustomStyledPlayerView extends StyledPlayerView implements Ge

private boolean restorePlayState;

private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;

public final Runnable textClearRunnable = () -> {
setCustomErrorMessage(null);
clearIcon();
Expand All @@ -62,6 +68,8 @@ public CustomStyledPlayerView(Context context, AttributeSet attrs, int defStyleA
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

exoErrorMessage = findViewById(R.id.exo_error_message);

mScaleDetector = new ScaleGestureDetector(context, this);
}

public void clearIcon() {
Expand All @@ -71,6 +79,8 @@ public void clearIcon() {

@Override
public boolean onTouchEvent(MotionEvent ev) {
mScaleDetector.onTouchEvent(ev);

switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
if (PlayerActivity.snackbar != null && PlayerActivity.snackbar.isShown()) {
Expand Down Expand Up @@ -137,6 +147,9 @@ public boolean onSingleTapUp(MotionEvent motionEvent) {

@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {
if (mScaleDetector.isInProgress())
return false;

// Exclude edge areas
if (motionEvent.getY() < IGNORE_BORDER || motionEvent.getX() < IGNORE_BORDER ||
motionEvent.getY() > getHeight() - IGNORE_BORDER || motionEvent.getX() > getWidth() - IGNORE_BORDER)
Expand Down Expand Up @@ -228,6 +241,27 @@ public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float
return false;
}

@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
mScaleFactor *= scaleGestureDetector.getScaleFactor();
mScaleFactor = Math.max(0.25f, Math.min(mScaleFactor, 2.0f));
setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
setScale(mScaleFactor);
clearIcon();
setCustomErrorMessage((int)(mScaleFactor * 100) + "%");
return true;
}

@Override
public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
mScaleFactor = getVideoSurfaceView().getScaleX();
return true;
}

@Override
public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
}

private enum Orientation {
HORIZONTAL, VERTICAL, UNKNOWN
}
Expand All @@ -247,4 +281,11 @@ public void setIconBrightness() {
public void setIconBrightnessAuto() {
exoErrorMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_brightness_auto_24dp, 0, 0, 0);
}

public void setScale(final float scale) {
final View videoSurfaceView = getVideoSurfaceView();
videoSurfaceView.setScaleX(scale);
videoSurfaceView.setScaleY(scale);
//videoSurfaceView.animate().setStartDelay(0).setDuration(0).scaleX(scale).scaleY(scale).start();
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/brouken/player/PlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ else if (rational.floatValue() < rationalLimitTall.floatValue())
buttonAspectRatio = new ImageButton(this, null, 0, R.style.ExoStyledControls_Button_Bottom);
buttonAspectRatio.setImageResource(R.drawable.ic_aspect_ratio_24dp);
buttonAspectRatio.setOnClickListener(view -> {
playerView.setScale(1.f);
if (playerView.getResizeMode() == AspectRatioFrameLayout.RESIZE_MODE_FIT) {
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
Utils.showText(playerView, getString(R.string.video_resize_crop));
Expand Down Expand Up @@ -441,6 +442,7 @@ public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Conf

if (isInPictureInPictureMode) {
setSubtitleTextSizePiP();
playerView.setScale(1.f);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -461,6 +463,9 @@ public void onReceive(Context context, Intent intent) {
registerReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
} else {
setSubtitleTextSize();
if (mPrefs.resizeMode == AspectRatioFrameLayout.RESIZE_MODE_ZOOM) {
playerView.setScale(mPrefs.scale);
}
if (mReceiver != null) {
unregisterReceiver(mReceiver);
mReceiver = null;
Expand Down Expand Up @@ -576,6 +581,12 @@ private void initializePlayer() {

playerView.setResizeMode(mPrefs.resizeMode);

if (mPrefs.resizeMode == AspectRatioFrameLayout.RESIZE_MODE_ZOOM) {
playerView.setScale(mPrefs.scale);
} else {
playerView.setScale(1.f);
}

MediaItem.Builder mediaItemBuilder = new MediaItem.Builder()
.setUri(mPrefs.mediaUri)
.setMimeType(mPrefs.mediaType);
Expand Down Expand Up @@ -645,6 +656,7 @@ private void releasePlayer() {
mPrefs.updateAudioTrack(getSelectedTrack(C.TRACK_TYPE_AUDIO));
mPrefs.updateResizeMode(playerView.getResizeMode());
mPrefs.updateOrientation();
mPrefs.updateScale(playerView.getVideoSurfaceView().getScaleX());

if (player.isPlaying()) {
restorePlayState = true;
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/brouken/player/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Prefs {
private static final String PREF_KEY_SUBTITLE_TRACK = "subtitleTrack";
private static final String PREF_KEY_RESIZE_MODE = "resizeMode";
private static final String PREF_KEY_ORIENTATION = "orientation";
private static final String PREF_KEY_SCALE = "scale";

final Context mContext;
final SharedPreferences mSharedPreferences;
Expand All @@ -33,6 +34,7 @@ class Prefs {
public String mediaType;
public int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT;
public Utils.Orientation orientation = Utils.Orientation.VIDEO;
public float scale = 1.f;

public int subtitleTrack = -1;
public int audioTrack = -1;
Expand Down Expand Up @@ -65,6 +67,7 @@ private void loadSavedPreferences() {
if (mSharedPreferences.contains(PREF_KEY_RESIZE_MODE))
resizeMode = mSharedPreferences.getInt(PREF_KEY_RESIZE_MODE, resizeMode);
orientation = Utils.Orientation.values()[mSharedPreferences.getInt(PREF_KEY_ORIENTATION, 1)];
scale = mSharedPreferences.getFloat(PREF_KEY_SCALE, scale);
}

public void updateMedia(final Uri uri, final String type) {
Expand All @@ -74,6 +77,7 @@ public void updateMedia(final Uri uri, final String type) {
updateAudioTrack(-1);
updateSubtitleTrack(-1);
updateResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
updateScale(1.f);

final SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
if (uri == null)
Expand Down Expand Up @@ -189,4 +193,11 @@ public void updateOrientation() {
sharedPreferencesEditor.putInt(PREF_KEY_ORIENTATION, orientation.value);
sharedPreferencesEditor.commit();
}

public void updateScale(final float scale) {
this.scale = scale;
final SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
sharedPreferencesEditor.putFloat(PREF_KEY_SCALE, scale);
sharedPreferencesEditor.commit();
}
}

0 comments on commit b4600b7

Please sign in to comment.