Android ViewGroup.setScaleX() cause the view to be clipped

user802421 picture user802421 · Sep 5, 2012 · Viewed 9.1k times · Source

I use NineOldAndroids library to scale my custom layout.

public class MyLayout extends FrameLayout {
  // LayoutParams.MATCH_PARENT and all.
  ...
  @Override
  public boolean setPositionAndScale(ViewGroup v, PositionAndScale pas, PointInfo pi) {
    ...
    mScale = pas.getScale();
    ViewHelper.setScaleX(this, mScale);
    ViewHelper.setScaleY(this, mScale);
  }
}

I have tried FrameLayout and AbsoluteLayout. All have the same effect. When mScale < 1.0 scaling/zooming works but part of the layout is clipped.

mScale = 1.0:

mScale = 1.0

mScale < 1.0: scaling/zooming works but layout is clipped

mScale < 1.0

How can i fix this issue?

Edit: The picture was taken on ICS. So I don't think it's NineOldAndroids problem.

Answer

Piezoid picture Piezoid · May 14, 2014

The parent of your view must have the property android:clipChildren disabled (from layout file or with setClipChildren(false) ).

But with this method you don't get the touch events outside the view clip bounds. You can work around by sending them from your activity or writing a custom ViewGroup parent.

I'm using a different hack which seems to work in my case, the trick is to maintain your own transformation matrix. Then, you have to overload a lot of ViewGroup's method to make it work. For example :

@Override
protected void dispatchDraw(Canvas canvas) {
    Log.d(TAG, "dispatchDraw " + canvas);
    canvas.save();
    canvas.concat(mMatrix);
    super.dispatchDraw(canvas);
    canvas.restore();       
}


@Override   
public boolean dispatchTouchEvent(MotionEvent ev) {
    Log.d(TAG, "dispatchTouchEvent " + ev);
    ev.transform(getInvMatrix()); // 
    return super.dispatchTouchEvent(ev);

}

private Matrix getInvMatrix()
{
    if(!mTmpMatIsInvMat)
        mMatrix.invert(mTmpMat);
    mTmpMatIsInvMat = true;
    return mTmpMat;
}