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

前端 未结 5 1126
鱼传尺愫
鱼传尺愫 2021-01-02 14:21

I use NineOldAndroids library to scale my custom layout.

public class MyLayout extends FrameLayout {
  // LayoutParams.MATCH_PARENT and all.
  ...
  @Overrid         


        
5条回答
  •  忘掉有多难
    2021-01-02 15:15

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

提交回复
热议问题