2017-04-19 55 views
1

我已經使用下面的代碼實現了TextView的縮放。 但是,即使用戶捏住屏幕上的任何位置,使用代碼TextView也可以縮放。我只需要在縮放TextView時使縮放縮放工作。 有人可以幫我嗎?如何識別在Android中捏放大的視圖?

我的現有代碼:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // return super.onTouchEvent(event); 
    if (event.getPointerCount() == 2) 
    { 
     int action = event.getAction(); 
     int pureaction = action & MotionEvent.ACTION_MASK; 

     if (pureaction == MotionEvent.ACTION_POINTER_DOWN) 
     { 
      mBaseDist = getDistance(event); 
      mBaseRatio = mRatio; 
     } 
     else 
     { 
      float delta = (getDistance(event) - mBaseDist)/STEP; 
      float multi = (float) Math.pow(2, delta); 
      mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi)); 
      mTextView.setTextSize(mRatio + 13); 
     } 
    } 
    return true; 

} 

回答

1

嘗試使用setOnTouchListener和覆蓋onTouch方法,這將給視圖被感動。

public boolean onTouch(View v, MotionEvent event) { 
    //v is view touched 
} 
1
TextView YOUR_TEXTVIEW = (TextView) findViewById(R.id.textView);  

YOUR_TEXTVIEW.setOnTouchListener(new OnTouchListener(){ 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      //Your Code 
     } 
    }); 

條件視圖觸摸

 @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if(v.getId() == R.id.textView){ 
      //Your Code 
      } 
     }