ScaleGestureDetector

android.view.ScaleGestureDetector (Since API Level 8, Android 2.2) detects transformation gestures involving more than one pointer ("multitouch") using the supplied MotionEvents. The ScaleGestureDetector.OnScaleGestureListener callback will notify users when a particular gesture event has occurred. This class should only be used with MotionEvents reported via touch.



ScaleGestureDetector
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>

0 Response to "ScaleGestureDetector"

Posting Komentar