Android ViewGroup.setScaleX () causes view cropping

I use NineOldAndroids 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 tried FrameLayout and AbsoluteLayout. They all have the same effect. When mScale < 1.0 scaling / scaling works , but part of the layout is cropped .

mScale = 1.0:

mScale = 1.0

mScale < 1.0: scaling / scaling works , but the layout is cropped

mScale <1.0

How can I fix this problem?

Edit : Image taken at ICS. Therefore, I do not think this is a NineOldAndroids problem.

+7
source share
5 answers

In case someone is in the same situation as me. I used 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); } 
+2
source

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

But with this method, you will not get touch events outside the bounds of the view clip. You can work by sending them from your activity or by creating a custom ViewGroup parent.

I am using another hack that seems to work in my case, the trick is to save my own transformation matrix. Then you need to overload many ViewGroup methods 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; } 
+3
source

If I understand your problem correctly, you scale the view group and expect the included views to scale accordingly. It doesn’t work: you scale the group of performances, and it changes its size, but its representations of children do not.

Just scale all the sub. However, I'm not sure that texts and images will automatically scale. You want to zoom, not zoom. Try this link .

+1
source

Starting at API level 11, the View class has setScaleX() and setScaleY() methods that work as expected, and also scale the scaled view submenu. So, if that is the way for you, drop the library and just do

 v.setScaleX(mScale); v.setScaleY(mScale); 
+1
source

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

0
source

All Articles