I'm pretty new to developing apps, so at the moment I'm just trying to learn the basics.
So far my code runs and if I swipe the screen the image moves (which is what I want). However, at the moment the image moves if I swipe anywhere on the screen, but I only want it to move when I swipe the image.
Here's my Main Activity code:
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);
}
}
Instead of delegating the Activity's onTouchEvent
to the GestureDetector
, simply delegate your imageView's onTouch
.
image.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});