Skip to content

Commit

Permalink
Check in Gesture example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonostrander committed Feb 23, 2012
1 parent e604ecb commit 3519aa7
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Chapter8/GestureThrowaway/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".GestureExampleActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
40 changes: 40 additions & 0 deletions Chapter8/GestureThrowaway/proguard.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
native <methods>;
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
11 changes: 11 additions & 0 deletions Chapter8/GestureThrowaway/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "ant.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-14
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Chapter8/GestureThrowaway/res/drawable/circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#80FF00FF"/>

<size
android:height="100dp"
android:width="100dp" />

</shape>
8 changes: 8 additions & 0 deletions Chapter8/GestureThrowaway/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" />

12 changes: 12 additions & 0 deletions Chapter8/GestureThrowaway/res/layout/row.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
7 changes: 7 additions & 0 deletions Chapter8/GestureThrowaway/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, GestureExampleActivity!</string>
<string name="app_name">GestureExample</string>

</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example;

import android.app.Activity;
import android.os.Bundle;

public class GestureExampleActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TouchExample view = new TouchExample(this);
setContentView(view);
}
}
113 changes: 113 additions & 0 deletions Chapter8/GestureThrowaway/src/com/example/TouchExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.example;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class TouchExample extends View {
private static final int MAX_POINTERS = 5;
private float mScale = 1f;
private GestureDetector mGestureDetector;
private ScaleGestureDetector mScaleGestureDetector;

private Pointer[] mPointers = new Pointer[MAX_POINTERS];
private Paint mPaint;
private float mFontSize;

class Pointer {
float x = 0;
float y = 0;
int index = -1;
int id = -1;
}

public TouchExample(Context context) {
super(context);
for (int i = 0; i<MAX_POINTERS; i++) {
mPointers[i] = new Pointer();
}

mFontSize = 16 * getResources().getDisplayMetrics().density;
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(mFontSize);

mGestureDetector = new GestureDetector(context, new ZoomGesture());
mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGesture());
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Pointer p : mPointers) {
if (p.index != -1) {
String text = "Index: " + p.index + " ID: " + p.id;
canvas.drawText(text, p.x, p.y, mPaint);
}
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
mScaleGestureDetector.onTouchEvent(event);

int pointerCount = Math.min(event.getPointerCount(), MAX_POINTERS);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_MOVE:
// clear previous pointers
for (int id = 0; id<MAX_POINTERS; id++)
mPointers[id].index = -1;

// Now fill in the current pointers
for (int i = 0; i<pointerCount; i++) {
int id = event.getPointerId(i);
Pointer pointer = mPointers[id];
pointer.index = i;
pointer.id = id;
pointer.x = event.getX(i);
pointer.y = event.getY(i);
}
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
for (int i = 0; i<pointerCount; i++) {
int id = event.getPointerId(i);
mPointers[id].index = -1;
}
invalidate();
break;
}
return true;
}

public class ZoomGesture extends GestureDetector.SimpleOnGestureListener {
private boolean normal = true;

@Override
public boolean onDoubleTap(MotionEvent e) {
mScale = normal ? 3f : 1f;
mPaint.setTextSize(mScale*mFontSize);
normal = !normal;
invalidate();
return true;
}
}

public class ScaleGesture extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScale *= detector.getScaleFactor();
mPaint.setTextSize(mScale*mFontSize);
invalidate();
return true;
}
}
}

0 comments on commit 3519aa7

Please sign in to comment.