Ch16 Listings
satya - Saturday, February 27, 2010 10:23:46 PM
Listing 16-1
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layout1"
android:tag="trueLayoutTop"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<com.androidbook.touch.demo1.TrueButton android:text="returns true"
android:id="@+id/trueBtn1"
android:tag="trueBtnTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.androidbook.touch.demo1.FalseButton android:text="returns false"
android:id="@+id/falseBtn1"
android:tag="falseBtnTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/trueBtn1" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout2"
android:tag="falseLayoutBottom"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF00FF"
>
<com.androidbook.touch.demo1.TrueButton android:text="returns true"
android:id="@+id/trueBtn2"
android:tag="trueBtnBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.androidbook.touch.demo1.FalseButton android:text="returns false"
android:id="@+id/falseBtn2"
android:tag="falseBtnBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/trueBtn2" />
</RelativeLayout>
</LinearLayout>
satya - Saturday, February 27, 2010 10:24:37 PM
Listing 16-2 a
// This file is BooleanButton.java
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
public abstract class BooleanButton extends Button {
protected boolean myValue() {
return false;
}
public BooleanButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
String myTag = this.getTag().toString();
Log.v(myTag, "-----------------------------------");
Log.v(myTag, MainActivity.describeEvent(this, event));
Log.v(myTag, "super onTouchEvent() returns " + super.onTouchEvent(event));
Log.v(myTag, "and I'm returning " + myValue());
event.recycle();
return(myValue());
}
}
satya - Saturday, February 27, 2010 10:24:58 PM
Listing 16-2 b
// This file is TrueButton.java
import android.content.Context;
import android.util.AttributeSet;
public class TrueButton extends BooleanButton {
protected boolean myValue() {
return true;
}
public TrueButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
satya - Saturday, February 27, 2010 10:25:21 PM
Listing 16-2 c
// This file is FalseButton.java
import android.content.Context;
import android.util.AttributeSet;
public class FalseButton extends BooleanButton {
public FalseButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
satya - Saturday, February 27, 2010 10:28:03 PM
Listing 16-3
// This file is MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout1.setOnTouchListener(this);
Button trueBtn1 = (Button)findViewById(R.id.trueBtn1);
trueBtn1.setOnTouchListener(this);
Button falseBtn1 = (Button)findViewById(R.id.falseBtn1);
falseBtn1.setOnTouchListener(this);
RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.layout2);
layout2.setOnTouchListener(this);
Button trueBtn2 = (Button)findViewById(R.id.trueBtn2);
trueBtn2.setOnTouchListener(this);
Button falseBtn2 = (Button)findViewById(R.id.falseBtn2);
falseBtn2.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
String myTag = v.getTag().toString();
Log.v(myTag, "-----------------------------");
Log.v(myTag, "Got view " + myTag + " in onTouch");
Log.v(myTag, describeEvent(v, event));
if( "true".equals(myTag.substring(0, 4))) {
Log.v(myTag, "and I'm returning true");
return true;
}
else {
Log.v(myTag, "and I'm returning false");
return false;
}
}
protected static String describeEvent(View view, MotionEvent event) {
StringBuilder result = new StringBuilder(300);
result.append("Action: ").append(event.getAction()).append("\n");
result.append("Location: ").append(event.getX()).append(" x ").append(event.getY()).append("\n");
if( event.getX() < 0 || event.getX() > view.getWidth() ||
event.getY() < 0 || event.getY() > view.getHeight()) {
result.append(">>> Touch has left the view <<<\n");
}
result.append("Edge flags: ").append(event.getEdgeFlags()).append("\n");
result.append("Pressure: ").append(event.getPressure()).append(" ");
result.append("Size: ").append(event.getSize()).append("\n");
result.append("Down time: ").append(event.getDownTime()).append("ms\n");
result.append("Event time: ").append(event.getEventTime()).append("ms");
result.append(" Elapsed: ").append(event.getEventTime()-event.getDownTime());
result.append(" ms\n");
return result.toString();
}
}
satya - Saturday, February 27, 2010 10:30:34 PM
Listing 16-8
private VelocityTracker vTracker = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action) {
case MotionEvent.ACTION_DOWN:
if(vTracker == null) {
vTracker = VelocityTracker.obtain();
}
else {
vTracker.clear();
}
vTracker.addMovement(event);
break;
case MotionEvent.ACTION_MOVE:
vTracker.addMovement(event);
vTracker.computeCurrentVelocity(1000);
Log.v(TAG, "X velocity is " + vTracker.getXVelocity() + " pixels per second");
Log.v(TAG, "Y velocity is " + vTracker.getYVelocity() + " pixels per second");
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
vTracker.recycle();
break;
}
event.recycle();
return true;
}
satya - Saturday, February 27, 2010 10:32:02 PM
Listing 16-9
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class Dot extends View {
private static final float RADIUS = 20;
private float x = 30;
private float y = 30;
private float initialX;
private float initialY;
private float offsetX;
private float offsetY;
private Paint backgroundPaint;
private Paint myPaint;
public Dot(Context context, AttributeSet attrs) {
super(context, attrs);
backgroundPaint = new Paint();
backgroundPaint.setColor(Color.BLUE);
myPaint = new Paint();
myPaint.setColor(Color.WHITE);
myPaint.setAntiAlias(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action) {
case MotionEvent.ACTION_DOWN:
// Need to remember where the initial starting point
// center is of our Dot and where our touch starts from
initialX = x;
initialY = y;
offsetX = event.getX();
offsetY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
x = initialX + event.getX() - offsetX;
y = initialY + event.getY() - offsetY;
break;
}
event.recycle();
return(true);
}
@Override
public void draw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
canvas.drawRect(0, 0, width, height, backgroundPaint);
canvas.drawCircle(x, y, RADIUS, myPaint);
invalidate();
}
}
satya - Saturday, February 27, 2010 10:32:35 PM
Listing 16-9
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.androidbook.touch.dragdemo1.Dot
android:id="@+id/dot"
android:tag="trueDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
satya - Saturday, February 27, 2010 10:33:04 PM
Listing 16-10
// This file is MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout1.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
String myTag = v.getTag().toString();
Log.v(myTag, "-----------------------------");
Log.v(myTag, "Got view " + myTag + " in onTouch");
Log.v(myTag, describeEvent(event));
if( "true".equals(myTag.substring(0, 4))) {
Log.v(myTag, "and I'm returning true");
return true;
}
else {
Log.v(myTag, "and I'm returning false");
return false;
}
}
protected static String describeEvent(MotionEvent event) {
StringBuilder result = new StringBuilder(500);
result.append("Action: ").append(event.getAction()).append("\n");
int numPointers = event.getPointerCount();
result.append("Number of pointers: ").append(numPointers).append("\n");
int ptrIdx = 0;
while (ptrIdx < numPointers) {
int ptrId = event.getPointerId(ptrIdx);
result.append("Pointer Index: ").append(ptrIdx);
result.append(", Pointer Id: ").append(ptrId).append("\n");
result.append(" Location: ").append(event.getX(ptrIdx));
result.append(" x ").append(event.getY(ptrIdx)).append("\n");
result.append(" Pressure: ").append(event.getPressure(ptrIdx));
result.append(" Size: ").append(event.getSize(ptrIdx)).append("\n");
ptrIdx++;
}
result.append("Downtime: ").append(event.getDownTime()).append("ms\n");
result.append("Event time: ").append(event.getEventTime()).append("ms");
result.append(" Elapsed: ").append(event.getEventTime()-event.getDownTime());
result.append(" ms\n");
return result.toString();
}
}
satya - Saturday, February 27, 2010 10:33:33 PM
Listing 16-10 xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:tag="trueLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TextView android:text="Touch fingers on the screen and look at LogCat"
android:id="@+id/message"
android:tag="trueText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
satya - Saturday, February 27, 2010 10:34:17 PM
Listing 16-12
int action = event.getAction();
int ptrId = event.getPointerId(0);
if(event.getPointerCount() > 1)
ptrId = (action & MotionEvent.ACTION_POINTER_ID_MASK) >>>
MotionEvent.ACTION_POINTER_ID_SHIFT;
action = action & MotionEvent.ACTION_MASK;
if(action < 7 && action > 4)
action = action - 5;
int ptrIndex = event.findPointerIndex(ptrId);
satya - Saturday, February 27, 2010 10:34:56 PM
Listing 16-13 a
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mapView = (MapView)findViewById(R.id.mapview);
ClickReceiver clickRecvr = new ClickReceiver(this);
mapView.getOverlays().add(clickRecvr);
}
satya - Saturday, February 27, 2010 10:44:57 PM
Listing 16-13 b
// This file is ClickReceiver.java
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class ClickReceiver extends Overlay{
private static final String TAG = "ClickReceiver";
private Context context;
public ClickReceiver(Context _context) {
context = _context;
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
Log.v(TAG, "Received a click at this point: " + p);
if(mapView.isStreetView()) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse
("google.streetview:cbll=" +
(float)p.getLatitudeE6() / 1000000f +
"," + (float)p.getLongitudeE6() / 1000000f
+"&cbp=1,180,,0,1.0"
));
context.startActivity(myIntent);
return true;
}
return false;
}
}
satya - Saturday, February 27, 2010 10:45:29 PM
Listing 16-14
import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
private static final String TAG = "Gesture Revealer";
GestureLibrary gestureLib = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureLib = GestureLibraries.fromFile("/sdcard/gestures");
if (!gestureLib.load()) {
Toast.makeText(this, "Could not load /sdcard/gestures", Toast.LENGTH_SHORT).~CCC
show();
finish();
}
// Let's take a look at the gesture library we have work with
Log.v(TAG, "Library features:");
Log.v(TAG, " Orientation style: " + gestureLib.getOrientationStyle());
Log.v(TAG, " Sequence type: " + gestureLib.getSequenceType());
for( String gestureName : gestureLib.getGestureEntries() ) {
Log.v(TAG, "For gesture " + gestureName);
int i = 1;
for( Gesture gesture : gestureLib.getGestures(gestureName) ) {
Log.v(TAG, " " + i + ": ID: " + gesture.getID());
Log.v(TAG, " " + i + ": Strokes count: " + gesture.getStrokesCount());
Log.v(TAG, " " + i + ": Stroke length: " + gesture.getLength());
i++;
}
}
GestureOverlayView gestureView = ~CCC
(GestureOverlayView) findViewById(R.id.gestureOverlay);
gestureView.addOnGesturePerformedListener(this);
}
@Override
public void onGesturePerformed(GestureOverlayView view, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
if (predictions.size() > 0) {
Prediction prediction = (Prediction) predictions.get(0);
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
for(int i=0;i<predictions.size();i++)
Log.v(TAG, "prediction " + predictions.get(i).name +
" - score = " + predictions.get(i).score);
}
}
}
}
satya - Saturday, February 27, 2010 10:46:00 PM
Listing 16-14
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Draw gestures and I'll guess what they are"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestureOverlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
android:fadeOffset="1000" />
</LinearLayout>