我非常擅长开发应用,所以目前我只是想学习基础知识。
到目前为止我的代码运行了,如果我滑动屏幕,图像会移动(这就是我想要的)。 但是,如果我在屏幕上的任何位置滑动,图像移动的那一刻,但我只想在我滑动图像时移动它。
这是我的主要活动代码:
package com.colourdrop.swipeattempt;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MyActivity extends AppCompatActivity implements
GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{
private TextView text;
private ImageView image;
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
text = (TextView) findViewById(R.id.textView);
image = (ImageView) findViewById(R.id.image);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void update(View v){
text.setText("You pressed the button!");
}
////// BEGIN GESTURES //////
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
text.setText("onSingleTapConfirmed");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
text.setText("onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
text.setText("onDoubleTapEvent");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
text.setText("onDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
text.setText("onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
text.setText("onSingleTapUp");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
text.setText("onScroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
text.setText("onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
text.setText("onFling");
image.setX(image.getX()+200);
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
答案 0 :(得分:0)
不要将活动的onTouchEvent
委托给GestureDetector
,只需委托您的imageView onTouch
。
image.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});