From 6de3eeeb2f1bae7a9b947c1bb65ac61a67b25945 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Fri, 27 May 2022 13:55:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8GestureDetector=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=A7=A6=E6=8E=A7=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nas_music_player/views/MainActivity.java | 111 +++++++++--------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java index d6843fa..d7c7ccd 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java @@ -9,6 +9,7 @@ import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; +import android.view.GestureDetector; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; @@ -22,6 +23,7 @@ import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityOptionsCompat; import androidx.core.util.Pair; +import androidx.core.view.GestureDetectorCompat; import com.alibaba.fastjson.JSON; import com.bumptech.glide.Glide; @@ -52,6 +54,9 @@ public class MainActivity extends AppCompatActivity { private TextView positionTime, durationTime, bitRate, playIndex; private SeekBar seekBar; private Handler handler; + + private GestureDetectorCompat touch; + @Override protected void onCreate(Bundle savedInstanceState) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); @@ -131,51 +136,13 @@ public class MainActivity extends AppCompatActivity { public void onClick(View v) { int model = (int) playModel.getTag(); if (model == MusicContainer.PLAY_MODEL_LIST_RANDOM) { - setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM,true); + setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM, true); return; } - setPlayModelButton(model + 1,true); + setPlayModelButton(model + 1, true); } }); - getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() { - float downPosition_x = 0, downPosition_y = 0; - @Override - public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_DOWN) { - downPosition_x = event.getRawX(); - downPosition_y = event.getRawY(); - } else if (event.getAction() == MotionEvent.ACTION_UP) { - if (Math.abs(downPosition_x - event.getRawX()) < 500 && downPosition_y - event.getRawY() > 500) { - Pair pImage = Pair.create((View) album_image, "main_album_image"); - Pair pPrevious = Pair.create((View) previous, "previous"); - Pair pPlay = Pair.create((View) play, "play"); - Pair pNext = Pair.create((View) next, "next"); - Pair pTitle = Pair.create((View) title, "title"); - Pair pArtist = Pair.create((View) artist, "artist"); - Pair pBar = Pair.create((View) seekBar, "bar"); - Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, - pImage, - pPrevious, - pPlay, - pNext, - pBar, - pTitle, - pArtist).toBundle(); - - Intent intent = new Intent( - MainActivity.this, PlayLibsActivity.class); - intent.putExtra("bar_pos", seekBar.getProgress()); - if (bundle != null) { - startActivity(intent, bundle); - } else { - startActivity(intent); - } - } - } - return false; - } - }); browserHelper = new MediaBrowserHelper(this); browserHelper.regPlayListener(new PlayListener()); @@ -271,7 +238,7 @@ public class MainActivity extends AppCompatActivity { AppTools.toast("列表随机"); break; default: - setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM,isButton); + setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM, isButton); return; } ConfigTools.getPreferences().edit().putInt("playModel", model).apply(); @@ -302,6 +269,7 @@ public class MainActivity extends AppCompatActivity { seekBar = findViewById(R.id.seekBar); playModel = findViewById(R.id.playModel); playIndex = findViewById(R.id.playIndex); + touch = new GestureDetectorCompat(this, new MyTouch()); int model = ConfigTools.getPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM); setPlayModelButton(model); } @@ -342,26 +310,35 @@ public class MainActivity extends AppCompatActivity { return super.onKeyUp(keyCode, event); } - float downX = 0; - float downY = 0; @Override public boolean onTouchEvent(MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_DOWN) { - downX = event.getRawX(); - downY = event.getRawY(); - } else if (event.getAction() == MotionEvent.ACTION_UP) { - pass(event.getRawX(), event.getRawY()); - } - return super.onTouchEvent(event); + return touch.onTouchEvent(event); } + private void toPlayList(){ + Pair pImage = Pair.create((View) album_image, "main_album_image"); + Pair pPrevious = Pair.create((View) previous, "previous"); + Pair pPlay = Pair.create((View) play, "play"); + Pair pNext = Pair.create((View) next, "next"); + Pair pTitle = Pair.create((View) title, "title"); + Pair pArtist = Pair.create((View) artist, "artist"); + Pair pBar = Pair.create((View) seekBar, "bar"); + Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, + pImage, + pPrevious, + pPlay, + pNext, + pBar, + pTitle, + pArtist).toBundle(); - private void pass(float upX, float upY) { - boolean isOkY = Math.abs(upY - downY) <= 200; - if (upX - downX > 200 && isOkY) { - previous.callOnClick(); - } else if (upX - downX < -200 && isOkY) { - next.callOnClick(); + Intent intent = new Intent( + MainActivity.this, PlayLibsActivity.class); + intent.putExtra("bar_pos", seekBar.getProgress()); + if (bundle != null) { + startActivity(intent, bundle); + } else { + startActivity(intent); } } @@ -413,4 +390,26 @@ public class MainActivity extends AppCompatActivity { System.out.println("不知道是啥变换了"); } } + + private class MyTouch extends GestureDetector.SimpleOnGestureListener { + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + float x = e1.getX() - e2.getX(); + float y = e1.getY() - e2.getY(); + if (Math.abs(x) < 200 && y > 500) { + System.out.println("上"); + } else if (Math.abs(x) < 200 && y < -500) { + toPlayList(); + System.out.println("下"); + } else if (Math.abs(y) < 200 && x > 300) { + next.callOnClick(); + System.out.println("右"); + } else if (Math.abs(y) < 200 && x < -300) { + previous.callOnClick(); + System.out.println("左"); + } + return super.onFling(e1, e2, velocityX, velocityY); + } + } } \ No newline at end of file