我正在尝试构建一个使用手势控制音量和亮度的应用。 我看过一些视频,这些视频讲述了如何使用手势,但没有讲述如何控制音量和亮度。我的主要目标是,当我在屏幕左侧滑动时,它可以控制音量;如果我在屏幕右侧滑动,则可以控制亮度。 我发现使用手势的唯一代码是:
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d("TAG","onDown: ");
// don't return false here or else none of the other
// gestures will work
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("TAG", "onSingleTapConfirmed: ");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("TAG", "onLongPress: ");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("TAG", "onDoubleTap: ");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("TAG", "onScroll: ");
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d("TAG", "onFling: ");
return true;
}
}
}
有什么想法可以从哪里开始?
答案 0 :(得分:0)
这是我的第一个答案。我在这里发布了一些用于控制音量的代码。这段代码使用OnTouchListener进行触摸事件,而不使用GestureDetector。
可能对您有帮助。.
//variable
private RelativeLayout mRlTouch;
private boolean intLeft, intRight;
private int sWidth, sHeight;
private long diffX, diffY;
private Display display;
private Point size;
private float downX, downY;
private AudioManager mAudioManager;
//onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volume_and_brightness_prac);
///THIS LAYOUT IMPLEMENT TOUCH EVENT
mRlTouch = findViewById(R.id.rl_volume_touch);
mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
///THIS METHOD FOR GET SCREEN SIZE
/// USE FOR DETECT SCREEN SIDE
private void getScreenSize() {
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
sWidth = size.x;
sHeight = size.y;
}
/////onTouch method
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//touch is start
downX = event.getX();
downY = event.getY();
if (event.getX() < (sWidth / 2)) {
//here check touch is screen left or right side
intLeft = true;
intRight = false;
} else if (event.getX() > (sWidth / 2)) {
//here check touch is screen left or right side
intLeft = false;
intRight = true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
//finger move to screen
float x2 = event.getX();
float y2 = event.getY();
diffX = (long) (Math.ceil(event.getX() - downX));
diffY = (long) (Math.ceil(event.getY() - downY));
if (Math.abs(diffY) > Math.abs(diffX)) {
if (intLeft) {
//if left its for brightness
if (downY < y2) {
//down swipe brightness decrease
} else if (downY > y2) {
//up swipe brightness increase
}
} else if (intRight) {
//if right its for audio
if (downY < y2) {
//down swipe volume decrease
mAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
} else if (downY > y2) {
//up swipe volume increase
mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
}
}
}
return true;
}