SimpleOnScaleGestureListener不检测移动

时间:2015-07-12 10:04:11

标签: android gesture

我有一个问题要问你。我正在尝试使用SimpleGestureDetector来执行捏合/缩放功能。我从日志中获得的是:听众接收到触摸,它被发送到scalegesturedetector,但是听众不会将其识别为缩放移动。这是代码(我无法获得'Scala'日志)。

提前谢谢

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:

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

1 个答案:

答案 0 :(得分:0)

如果您想要处理,则需要在onScaleBegin中返回true。

onScaleBegin的返回值很重要,引自doc

  

探测器是否应继续识别此手势。例如,如果手势以一个有意义的区域之外的焦点开始,onScaleBegin()可能会返回false以忽略手势的其余部分。