Normally, it can be used to detect pinch zoom.
To use this class:
- Create an instance of the ScaleGestureDetector for your View
- In the onTouchEvent(MotionEvent) method ensure you call onTouchEvent(MotionEvent). The methods defined in your callback will be executed when the events occur.
example:
package com.AndroidScaleGestureDetector;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.TextView;
public class AndroidScaleGestureDetectorActivity extends Activity {
TextView textGestureAction;
private ScaleGestureDetector scaleGestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textGestureAction = (TextView)findViewById(R.id.GestureAction);
scaleGestureDetector = new ScaleGestureDetector(this,
new MySimpleOnScaleGestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
public class MySimpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float scaleFactor = detector.getScaleFactor();
if(scaleFactor > 1){
textGestureAction.setText("Scale Out: " + String.valueOf(scaleFactor));
}else{
textGestureAction.setText("Scale In: " + String.valueOf(scaleFactor));
}
return true;
}
}
}
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/GestureAction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
No comments:
Post a Comment