Search code examples
androidgesture

SimpleOnScaleGestureListener doesn't detect movement


I've a question for you. I'm trying to use SimpleGestureDetector to do a pinch/zoom feature. What I get from my logs is this: the touch is received by the listener, it's sent to the scalegesturedetector but the listener doesn't recognize it as a scale movement. Here's the code (I can't get the 'Scala' log).

Thank you in advance

public class MainActivity extends Activity {

TextView per;
TextView finalword;
LinearLayout wrapper;
RelativeLayout tutto;
ImageView pulsante;
String parola ="per esempio";
boolean touchable = false;
ScaleGestureDetector mySGD;
Context context;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();
    setContentView(R.layout.activity_main);
    per = (TextView) findViewById(R.id.per);
    finalword = (TextView) findViewById(R.id.finalword);
    wrapper = (LinearLayout) findViewById(R.id.wrapper);
    tutto = (RelativeLayout) findViewById(R.id.tutto);
    pulsante = (ImageView) findViewById(R.id.pulsante);
    finalword.setVisibility(View.INVISIBLE);
    mySGD = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener(){

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            Log.v("TAG","Scala");
            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            if(detector.getScaleFactor() > 0f){
                Log.v("TAG","Rimpicciolito");
            }
            else
            {
                Log.v("TAG","Ingrandito");
            }
            Log.v("TAG","Finito");
        }

    });



    pulsante.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            muovi();
        }

    });


    tutto.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub      
            return mySGD.onTouchEvent(event);
        }


    });
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void muovi(){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics( dm );
    int originalPos[] = new int[2];
    wrapper.getLocationOnScreen( originalPos );
    int baroffset = (dm.heightPixels - tutto.getMeasuredHeight());
    int yDelta = (dm.heightPixels/2 - wrapper.getMeasuredHeight()/2) - baroffset;
    AnimationSet animSet = new AnimationSet(true);
    animSet.setFillAfter(true);
    animSet.setDuration(1000); 
    TranslateAnimation translate = new TranslateAnimation( 0, 0 , 0, yDelta) ;
    translate.setAnimationListener(new Animation.AnimationListener(){

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            touchable = true;
            per.setVisibility(View.INVISIBLE);
            finalword.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

    });
    animSet.addAnimation(translate);
    wrapper.startAnimation(animSet);


}





}

XML here:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tutto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="0dp"
tools:context="com.example.ortografia.MainActivity" >

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/per"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="per esempio" 
        android:textSize="48dp" />

</LinearLayout>

<View
    android:id="@+id/view1"
    android:layout_width="fill_parent"
    android:layout_height="10dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true" />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="20dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/view1"
    android:layout_marginTop="20dp"
    android:src="@drawable/nastro" />

<ImageView
    android:id="@+id/pulsante"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="36dp"
    android:layout_marginTop="24dp"
    android:src="@drawable/pulsante" />

<TextView
    android:id="@+id/finalword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="per esempio"
    android:textSize="48dp" />

</RelativeLayout>

Solution

  • If you want to handle you need to return true in your onScaleBegin.

    The return value of onScaleBegin matters, quoted from doc

    Whether or not the detector should continue recognizing this gesture. For example, if a gesture is beginning with a focal point outside of a region where it makes sense, onScaleBegin() may return false to ignore the rest of the gesture.