android动画宽度和高度RelativeLayout

时间:2016-07-05 19:46:02

标签: android android-layout android-animation android-xml

我能得到

吗?
  

RelativeLayout的坐标x和y

虽然我从

动画
  

MATCH_PARENT到WARP_CONTENT

所以我只想让x和y为其中的另一个视图设置动画 因为我有很多RelativeLayout作为孩子,当我将父母从MATCH_PARENT更改为WARP_CONTENT时,孩子的位置发生了变化,并且mabye不在屏幕上,所以我想考虑x和y如果父级为MATCH_PARENTWARO_CONTENT

,则父级在动画和减去子级位置时会使子级处于相同位置

1 个答案:

答案 0 :(得分:0)

怎么样?
RelativeLayout mlayout = findViewById(R.id.yourID);

float x = mlayout.getX();
float y = mlayout.getY();

修改:来自Resizing layouts programmatically (as animation)

所以你需要最终的高度和宽度,也许你可以使它成为invisbile,改为WRAP_CONTENT,测量新的高度和宽度,改变回来,使用上面的代码创建可见和动画大小到新的高度和宽度。 不幸的是,不知道更好的方式。

然后设置动画宽度:

实例化ResizeAnimation

ResizeAnimation resizeAnimation = new ResizeAnimation(
     view, 
     targetHeight, 
     startHeight,
     targetWidth,
     startWidth
); 
resizeAnimation.setDuration(duration); 
view.startAnimation(resizeAnimation);

ResizeAnimation类应该看起来像这样

public class ResizeAnimation extends Animation {
    final int targetHeight;
    final int targetWidth;
    View view;
    int startHeight;
    int startWidth;

    public ResizeAnimation(View view, int targetHeight, int startHeight, targetWidth, int startWidth) {
        this.view = view;
        this.targetHeight = targetHeight;
        this.startHeight = startHeight;
        this.targetWidth = targetWidth;
        this.startWidth = startWidth;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newHeight = (int) (startHeight + targetHeight * interpolatedTime);
        int newWidth= (int) (startWidth + targetWidth * interpolatedTime);

        view.getLayoutParams().height = newHeight;
        view.getLayoutParams().width = newWidth;
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}
相关问题