2016-04-24 123 views
0

我正在嘗試製作兩個(或多個)活動TextViews,並使整個集合使用捏縮放縮放。我有自定義佈局從FrameLayout裏延伸:使用手勢縮放FrameLayout

public class BublinkyLayout extends FrameLayout { 
private ScaleGestureDetector mScaleDetector; 
private float mScaleFactor = 1.f; 
private float scale = 1; 

public BublinkyLayout(Context context) { 
    this(context, null, 0); 
} 

public BublinkyLayout(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public BublinkyLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setWillNotDraw(false); 
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
} 

public void setScale(float scale) { 
    this.scale = scale; 
    invalidate(); 
} 

public float getScale() { 
    return this.scale; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.scale(scale, scale); 
    super.onDraw(canvas); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    mScaleDetector.onTouchEvent(event); 
    final int action = MotionEventCompat.getActionMasked(event); 
    switch (action) { 
     case MotionEvent.ACTION_MOVE: 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.getLayoutParams(); 
      this.setScale(mScaleFactor); 
      this.setLayoutParams(layoutParams); 
      this.invalidate(); 
      break; 
    } 
    return true; 
} 
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     mScaleFactor *= detector.getScaleFactor(); 

     mScaleFactor = Math.max(0.3f, Math.min(mScaleFactor, 1.5f)); 

     invalidate(); 
     return true; 
    } 
} 
} 

但是,當我使用「雙指放大」和縮小,孩子TextViews不具備「觸摸區」(在區,在那裏我可以觸摸他們並移動他們)與他們合作。它看起來像佈局縮放,但該區域仍然是相同的,這意味着我不能將它們從原始框中取出。

這裏是OnTouchListener我用它來左右移動TextViews:

public class BublinkaOnTouchListener implements View.OnTouchListener { 
private int _xDelta; 
private int _yDelta; 

@Override 
public boolean onTouch(View view, MotionEvent event) { 
    final int X = (int) event.getRawX(); 
    final int Y = (int) event.getRawY(); 
    switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) view.getLayoutParams(); 
      _xDelta = X - lParams.leftMargin; 
      _yDelta = Y - lParams.topMargin; 
      break; 
     case MotionEvent.ACTION_UP: 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      break; 
     case MotionEvent.ACTION_MOVE: 
      FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams(); 
      layoutParams.leftMargin = X - _xDelta; 
      layoutParams.topMargin = Y - _yDelta; 
      layoutParams.rightMargin = -250; 
      layoutParams.bottomMargin = -250; 
      view.setLayoutParams(layoutParams); 
      view.invalidate(); 
      break; 
    } 
    return true; 
} 
} 

感謝每一個答案。

PS:英語不是我的母語失誤:)

回答

0

很遺憾的發現是answer主要解決了我的問題。

這不是我最終的解決方案,但它確實有助於解決這個問題。