Neste Post Vamos aprender a trabalhar com GestureDetector.
Para reconhecer movimentos feitos no touch do celular.
Vamos lá:
[CODE]
package de.scrolltest;
//1ºFazer os imports das classes
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.MotionEvent;//Importante
import android.view.GestureDetector.OnGestureListener;//Importante
import android.view.GestureDetector;
import android.widget.TextView;
import android.graphics.Color;
public class GestureDetectorExemplo extends Activity implements OnGestureListener
{
//Vamos declarar as variaveis que utilizaremos nesse teste
private LinearLayout main;
private TextView viewA; //Texto que mostrará nossa ação
/*Essas variaveis são para reconhecer a distancia mínima que poderemos “percorrer” com o dedo */
static final int SWIPE_MIN_DISTANCE = 120;
static final int SWIPE_MAX_OFF_PATH = 250;
static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
/*Montamos a parte visual do nosso aplicativo/*
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
main.addView(viewA);
setContentView(main);
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e)
{
viewA.setText("-" + "DOWN" + "-");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
viewA.setText("-" + " " + "-");
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
viewA.setText("-" + "Longo Aperto" + "-");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
viewA.setText("-" + "SCROOL Esquerda" + "-");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
viewA.setText("-" + "SCROLL Direita" + "-");
}
return true;
}
@Override
public void onShowPress(MotionEvent e)
{
viewA.setText("-" + " Apertou " + "-");
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
viewA.setText("-" + "Apertou rapido" + "-");
return true;
}
}
[/CODE]
Vitor Yudi Hansen