FU Lecture 15
FU Lecture 15
The GestureDetector.OnGestureListener
callback will notify users when a particular
motion event has occurred
GestureDetector
To use this class:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
GestureDetector
Override onFling
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
GestureDetector First 2 args are MotionEvent objects,
which are used to report some kind of
movement, so they store information
about an event involving movement
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
GestureDetector e1 stores information about the
beginning of the movement, e2 stores
the same for the end of the movement
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
GestureDetector velocityX is the speed in the X
direction, velocityY is the speed in
the Y direction
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
GestureDetector
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
GestureDetector
class MyGestureDetector extends SimpleOnGestureListener {
@Override
Checking
public boolean for right-to-left swipe.
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Did the user swipe "long enough"?
"Long enough" is arbitrary
if (Math.abs(e1.getY() - e2.getY()) > 250)
return false;
return false;
}
}
GestureDetector
class MyGestureDetector extends SimpleOnGestureListener {
@Override
Checking
public boolean onFling(MotionEvent for right-to-left
e1, MotionEvent e2,swipe.
float velocityX, float velocityY) {
Did the user swipe "fast enough"?
"Fast enough" is also arbitrary
if (Math.abs(e1.getY() - e2.getY()) > 250)
return false;
return false;
}
}
GestureDetector
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
GestureDetector
• These are gestures anywhere on the Activity
itself
If youevent)
@Override public boolean onTouch(View v, MotionEvent don't {return true here, the double
detector.onTouchEvent(event); tap will not work. We turn true to
return true; indicate that you will handle these
} touch events yourself
});
detector = new GestureDetector(this, new MyGestureDetector());
Custom Gestures
• Browse mnt/sdcard/
You should have something like the XML file on the following slide
Custom Gestures
<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</android.gesture.GestureOverlayView>
Custom Gestures
GestureOverlayView mOverlay;
GestureLibrary gestureLib;
}
Custom Gestures
public class GestureExampleActivity extends Activity implements OnGesturePerformedListener {
GestureOverlayView mOverlay;
We implement
GestureLibrary gestureLib;
OnGesturePerformedListen
er
@Override public void onCreate(Bundle savedInstanceState) { }
}
Custom Gestures
public class GestureExampleActivity extends Activity implements OnGesturePerformedListener {
GestureOverlayView mOverlay;
GestureLibrary gestureLib;
}
Which forces us to add the
onGesturePerformed() callback
method, which we show on the
next slides
Custom Gestures
public class GestureExampleActivity extends Activity implements OnGesturePerformedListener {
GestureOverlayView mOverlay;
GestureLibrary gestureLib; Note these fields, which
we will use later
@Override public void onCreate(Bundle savedInstanceState) { }
}
Custom Gestures
GestureOverlayView mOverlay;
GestureLibrary gestureLib;
if(!gestureLib.load())
Toast.makeText(this, "Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
}
if(!gestureLib.load())
Toast.makeText(this, "Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
}
if(!gestureLib.load())
GestureLibraries simply
Toast.makeText(this,
allows us to"Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
open gesture
} files
if(!gestureLib.load())
byload
Toast.makeText(this, "Couldn't calling
Gesture Library", Toast.LENGTH_SHORT).show();
fromRawResource()
}
if(!gestureLib.load())
and passing the Context
Toast.makeText(this, "Couldn't load Gesture
andLibrary", Toast.LENGTH_SHORT).show();
our gestures file as
} arguments
if(!gestureLib.load())
Toast.makeText(this, "Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
}
@Override
Try to loadpublic void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
the gestures
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for(Prediction prediction : predictions) {
if(prediction.score > 1.0)
Toast.makeText(getApplicationContext(), prediction.name, Toast.LENGTH_SHORT).show();
}
Custom Gestures
GestureOverlayView mOverlay;
GestureLibrary gestureLib;
if(!gestureLib.load())
Toast.makeText(this, "Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
}
This is a shortcut technique
for iterating through the List
@Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for(Prediction prediction : predictions) {
if(prediction.score > 1.0)
Toast.makeText(getApplicationContext(), prediction.name, Toast.LENGTH_SHORT).show();
}
Custom Gestures
GestureOverlayView mOverlay;
GestureLibrary gestureLib;
if(!gestureLib.load())
Toast.makeText(this, "Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
}
Only recognize this as a valid
gesture if we have some kind
@Override public
ofvoid onGesturePerformed(GestureOverlayView
confidence in the prediction overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for(Prediction prediction : predictions) {
if(prediction.score > 1.0)
Toast.makeText(getApplicationContext(), prediction.name, Toast.LENGTH_SHORT).show();
}
Custom Gestures
GestureOverlayView mOverlay;
GestureLibrary gestureLib;
if(!gestureLib.load())
Toast.makeText(this, "Couldn't load Gesture Library", Toast.LENGTH_SHORT).show();
}
To find out which gesture was
made, useoverlay,
@Override public void onGesturePerformed(GestureOverlayView the name field
Gesture in
gesture) {
the Prediction instance
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for(Prediction prediction : predictions) {
if(prediction.score > 1.0)
Toast.makeText(getApplicationContext(), prediction.name,
Toast.LENGTH_SHORT).show();
Google Maps API Key
• In order to use the Google Maps API, you first
need to obtain an API key and configure your
app
@Override
Public View getInfoWindow(Marker marker){ return null;}
Customized Info Window
• In java file:
• map.setInfoWindowAdapter(new PopuoAdapter(getLayoutInflater()));
Find My Location
• In java file:
• map.setMyLocationEnabled(true);
• You must call map.setOnMyLocationChangeListener(Listener)
• The listener must override the method:
• Public void onMyLocationChange(Location lastKnownLocation)
Map Listeners
• setOnCameraChangeListener(this)
o implement OnCameraChangeListener
o Override public void onCameraChange(CameraPosition position)
• setMyLocationChangeListener(this):
o implement OnMyLocationChangeListener
o Override public void onMyLocationChange(Location lastKnownLocation)
• The user can change the center of the map via simple swipe gestures
• The user can change the camera tilt via two-finger vertical swipes, so
instead of a traditional top-down perspective, the user can see things on
an angle
• The user can change the orientation of the map via a two-finger rotating
swipe, to change the typical “north is to the top of the map” to some other
orientation
Control
• You can enable and disable those gestures by:
o setRotateGesturesEnabled()
o setScrollGesturesEnabled() (for panning the map)
o setTiltGesturesEnabled()
o setZoomControlsEnabled() (for the + and - buttons)
o setZoomGesturesEnabled() (for pinch-to-zoom)
o SetCompassEnable()
JSON Values
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
JSON Objects
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/10" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
}
Parsing JSON
JSONObject json = new JSONObject(raw);
mTextMoviePlot.post(new Runnable() {
@Override
public void run() {
mTextMoviePlot.setText(moviePlot);
}
});
Parsing JSON
JSONObject is a set of
JSONObject json = new JSONObject(raw); name/value mappings,
can represent a JSON
document
mTextMoviePlot.post(new Runnable() {
@Override
public void run() {
mTextMoviePlot.setText(moviePlot);
}
});
Parsing JSON
JSONObject json = new JSONObject(raw);
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
}
Parsing JSON
This is a JSONObject
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with
pie"]
}
Parsing JSON
{
"title" : "IT Business Analyst Intern" , You can get title by
calling getString("title")
"organization" : "Medtronic" , on the JSONObject
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
}
Parsing JSON
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" , You can get organization by calling
getString("organization") on the
"city" : "Brooklyn Park" , JSONObject
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
}
Parsing JSON
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
Etcetera, etcetera ...
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
}
Parsing JSON
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with
pie"]
} This however, is not a String,
but an array. Get this by
calling getJSONArray() on the
JSONObject
Parsing JSON
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with
pie"]
} After which you can use the
getters on the JSONArray to
get the desired data
Parsing JSON
{
"title" : "IT Business Analyst Intern" ,
"organization" : "Medtronic" ,
"city" : "Brooklyn Park" ,
"state" : "MN" ,
"start_date" : "05/10" ,
"end_date" : "07/11" ,
"details" : [ "bla bla bla" , "drank some soda" , "hit manager in face with pie"]
}