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

前端 未结 5 1089
鱼传尺愫
鱼传尺愫 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:06

    If I understand your problem correctly, you are scaling a view group and expect the included views to scale accordingly. It doesn't work that way: you scale the view group and it changes size, but its children views do not.

    Just scale all subviews. Even so, I am not sure that texts and images are going to be automatically scaled. What you want is zoom, not scale. Try this reference.

    0 讨论(0)
  • 2021-01-02 15:11

    In case anyone got in to the same situation as me. I ended up using this approach:

    protected void setScale(float scale, boolean updateView) {
        mScale = scale;
        if (updateView) {
            LayoutParams params = getLayoutParams();
            onUpdateScale(scale, params);
            setLayoutParams(params);
        }
    }
    
    protected void onUpdateScale(float scale, LayoutParams params) {
        params.leftMargin = (int) (mModel.getX() * scale);
        params.topMargin = (int) (mModel.getY() * scale);
        params.width = (int) (mModel.getWidth() * scale);
        params.height = (int) (mModel.getHeight() * scale);
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-02 15:16

    Use ViewGroup.layout. It may be the easiest way to scale(&move) ViewGroup.

    0 讨论(0)
  • 2021-01-02 15:22

    Since API Level 11, the View class has setScaleX() and setScaleY() methods, that work as expected and also scale sub-views of the scaled view. So, if that'd be a way for you, drop the library and just do

    v.setScaleX(mScale);
    v.setScaleY(mScale);
    
    0 讨论(0)
提交回复
热议问题