Permalink
Browse files

Created the PinchZoomDetector project. Implemented this class in the …

…FixedSizeTextColumns, and checked wether a device has a multiple touch screen or not. Also, increased minimum required version to 2.1
  • Loading branch information...
1 parent 981078d commit 506e522ec58567bd1025ca91ba26264c482ea89f @gnorsilva gnorsilva committed Jan 20, 2011
@@ -15,5 +15,5 @@
</activity>
</application>
- <uses-sdk android:minSdkVersion="4" />
+ <uses-sdk android:minSdkVersion="7" />
</manifest>
@@ -4,17 +4,24 @@
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
+import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.View.OnTouchListener;
import android.webkit.WebView;
+import com.novoda.utils.PinchZoomDetector;
+
public class HelloWebView extends Activity{
private static final int DIALOG_CHOOSE_VIEW_ID = 0;
private WebView webView;
private int fontSize;
+ private PinchZoomDetector zoomDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -23,6 +30,11 @@ public void onCreate(Bundle savedInstanceState) {
webView = (WebView) findViewById(R.id.webview_main);
webView.loadUrl("file:///android_asset/two_columns.html");
fontSize = webView.getSettings().getDefaultFontSize();
+
+ if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)) {
+ webView.setOnTouchListener(touchListener);
+ zoomDetector = new PinchZoomDetector();
+ }
}
@Override
@@ -55,11 +67,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
private void fontSizePlus() {
- webView.getSettings().setDefaultFontSize(++fontSize);
+ if (fontSize < 72) {
+ webView.getSettings().setDefaultFontSize(++fontSize);
+ }
}
private void fontSizeMinus() {
- webView.getSettings().setDefaultFontSize(--fontSize);
+ int minimumFont = webView.getSettings().getMinimumFontSize();
+ if (fontSize > minimumFont) {
+ webView.getSettings().setDefaultFontSize(--fontSize);
+ }
}
@Override
@@ -93,4 +110,23 @@ public void onClick(DialogInterface dialog, int item) {
return builder.create();
}
+
+ private OnTouchListener touchListener = new OnTouchListener() {
+ @Override
+ public boolean onTouch(View v, MotionEvent event) {
+ int result = zoomDetector.detectZoom(event);
+
+ switch(result){
+ case PinchZoomDetector.ZOOMING_IN:
+ fontSizePlus();
+ break;
+ case PinchZoomDetector.ZOOMING_OUT:
+ fontSizeMinus();
+ break;
+ }
+
+ return true;
+ }
+ };
+
}
@@ -0,0 +1,84 @@
+package com.novoda.utils;
+
+import android.util.FloatMath;
+import android.view.MotionEvent;
+
+public class PinchZoomDetector {
+ public static final int OTHER = 0;
+ public static final int ZOOMING_STARTED = 1;
+ public static final int ZOOMING_IN = 2;
+ public static final int ZOOMING_OUT = 3;
+ public static final int ZOOMING_STOPPED = 4;
+
+ public float minimumFingerDistance = 20f;
+ public float zoomInterval = 50f;
+
+ private boolean zooming;
+ private float oldDistance;
+
+ public PinchZoomDetector() {
+ }
+
+ public PinchZoomDetector(float minimumFingerDistance, float zoomInterval) {
+ this.minimumFingerDistance = minimumFingerDistance;
+ this.zoomInterval = zoomInterval;
+ }
+
+ public PinchZoomDetector(float zoomInterval) {
+ this.zoomInterval = zoomInterval;
+ }
+
+ public int detectZoom(MotionEvent event) {
+ int result = OTHER;
+ switch (event.getAction() & MotionEvent.ACTION_MASK) {
+ case MotionEvent.ACTION_MOVE:
+ result = moving(event);
+ break;
+ case MotionEvent.ACTION_UP:
+ case MotionEvent.ACTION_POINTER_UP:
+ result = release(event);
+ break;
+ }
+ return result;
+ }
+
+ private int moving(MotionEvent event) {
+ int result = OTHER;
+ float distance = fingerDistance(event);
+ if (zooming == false) {
+ if (event.getPointerCount() > 1) {
+ if (distance > minimumFingerDistance) {
+ zooming = true;
+ oldDistance = distance;
+ result = ZOOMING_STARTED;
+ }
+ }
+ }
+ if (zooming) {
+ if (distance + zoomInterval < oldDistance) {
+ result = ZOOMING_OUT;
+ oldDistance = distance;
+ } else if (distance - zoomInterval > oldDistance) {
+ result = ZOOMING_IN;
+ oldDistance = distance;
+ }
+ }
+ return result;
+ }
+
+ private int release(MotionEvent event) {
+ int result = OTHER;
+ if (zooming) {
+ zooming = false;
+ result = ZOOMING_STOPPED;
+ }
+ return result;
+ }
+
+ private float fingerDistance(MotionEvent event) {
+ float x = event.getX(0) - event.getX(1);
+ float y = event.getY(0) - event.getY(1);
+ return FloatMath.sqrt(x * x + y * y);
+ }
+
+}
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.novoda"
+ android:versionCode="1"
+ android:versionName="1.0">
+ <application android:icon="@drawable/icon" android:label="@string/app_name">
+ <activity android:name=".Main"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+
+ </application>
+ <uses-sdk android:minSdkVersion="7" />
+
+</manifest>
@@ -0,0 +1,34 @@
+-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 com.android.vending.licensing.ILicensingService
+
+-keepclasseswithmembernames class * {
+ native <methods>;
+}
+
+-keepclasseswithmembernames class * {
+ public <init>(android.content.Context, android.util.AttributeSet);
+}
+
+-keepclasseswithmembernames class * {
+ public <init>(android.content.Context, android.util.AttributeSet, int);
+}
+
+-keepclassmembers enum * {
+ public static **[] values();
+ public static ** valueOf(java.lang.String);
+}
+
+-keep class * implements android.os.Parcelable {
+ public static final android.os.Parcelable$Creator *;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:id="@id/activity_main"
+ >
+</LinearLayout>
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <item type="id" name="activity_main"/>
+</resources>
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="app_name">Pinch Zoom Detector</string>
+ <string name="toast_zooming_started">Zooming Started</string>
+ <string name="toast_zooming_in">Zooming In</string>
+ <string name="toast_zooming_out">Zooming Out</string>
+ <string name="toast_zooming_stopped">Zooming Stopped</string>
+</resources>
+
@@ -0,0 +1,64 @@
+package com.novoda;
+
+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.Toast;
+
+import com.novoda.utils.PinchZoomDetector;
+
+public class Main extends Activity implements OnTouchListener {
+ private static final String TAG = "Pinch Zoom Detector";
+ private Toast toast;
+ private PinchZoomDetector zoomDetector;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+ View view = findViewById(R.id.activity_main);
+ view.setOnTouchListener(this);
+ toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
+ zoomDetector = new PinchZoomDetector();
+ }
+
+ @Override
+ public boolean onTouch(View v, MotionEvent event) {
+ int result = zoomDetector.detectZoom(event);
+ int resId = 0;
+
+ switch(result){
+ case PinchZoomDetector.ZOOMING_STARTED:
+ resId = R.string.toast_zooming_started;
+ break;
+ case PinchZoomDetector.ZOOMING_IN:
+ resId = R.string.toast_zooming_in;
+ break;
+ case PinchZoomDetector.ZOOMING_OUT:
+ resId = R.string.toast_zooming_out;
+ break;
+ case PinchZoomDetector.ZOOMING_STOPPED:
+ resId = R.string.toast_zooming_stopped;
+ break;
+ }
+
+ if(resId != 0){
+ String s = getResources().getString(resId);
+ shortToast(s);
+ Log.d(TAG,s);
+ }
+
+ return true;
+ }
+
+ private void shortToast(String s) {
+ toast.setText(s);
+ if(!toast.getView().isShown()){
+ toast.show();
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit 506e522

Please sign in to comment.