使用GestureDetector优化触控处理
This commit is contained in:
parent
3cc08fc13d
commit
6de3eeeb2f
@ -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<View, String> pImage = Pair.create((View) album_image, "main_album_image");
|
||||
Pair<View, String> pPrevious = Pair.create((View) previous, "previous");
|
||||
Pair<View, String> pPlay = Pair.create((View) play, "play");
|
||||
Pair<View, String> pNext = Pair.create((View) next, "next");
|
||||
Pair<View, String> pTitle = Pair.create((View) title, "title");
|
||||
Pair<View, String> pArtist = Pair.create((View) artist, "artist");
|
||||
Pair<View, String> 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<View, String> pImage = Pair.create((View) album_image, "main_album_image");
|
||||
Pair<View, String> pPrevious = Pair.create((View) previous, "previous");
|
||||
Pair<View, String> pPlay = Pair.create((View) play, "play");
|
||||
Pair<View, String> pNext = Pair.create((View) next, "next");
|
||||
Pair<View, String> pTitle = Pair.create((View) title, "title");
|
||||
Pair<View, String> pArtist = Pair.create((View) artist, "artist");
|
||||
Pair<View, String> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user