java - 如何为布局的宽度和高度设置动画?

标签 java android android-layout android-animation

我有一个 LinearLayout,它通过在 onClick 上隐藏所有其他布局和 View 来扩展到全屏。 LinearLayout

上面有一个Relativelayout

我想在此应用自定义动画。大小应该缓慢增加(比如 500 毫秒)。

但我怀疑这可能吗?谢谢。

这是我正在做的 onClick:

private void expandView (int viewId) {
    RelativeLayout relativeLayout = (RelativeLayout) ((LinearLayout) view.findViewById(viewId)).getParent();
    ViewGroup.MarginLayoutParams rlMargin = (ViewGroup.MarginLayoutParams) relativeLayout.getLayoutParams();
    rlMargin.setMargins(0, 0, 0, 0);
    relativeLayout.setLayoutParams(rlMargin);
    LinearLayout linearLayout = (LinearLayout) relativeLayout.getParent();
    hideAllLinearLayoutExcept(linearLayout.getId());
    hideAllTilesExcept(viewId);
}

viewId 是我点击的 LinearLayout 的 ID。此函数从 onClick()

调用

最佳答案

当然可以。

只需编写您自己的自定义动画并修改动画 View 的 LayoutParams。 在此示例中,动画为动画 View 的高度设置动画。当然,动画宽度也是可能的

它可能是这样的:

public class ResizeAnimation extends Animation {

    private int startHeight;
    private int deltaHeight; // distance between start and end height
    private View view;

    /**
     * constructor, do not forget to use the setParams(int, int) method before
     * starting the animation
     * @param v
     */
    public ResizeAnimation (View v) {
        this.view = v;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
        view.requestLayout();
    }

    /**
     * set the starting and ending height for the resize animation
     * starting height is usually the views current height, the end height is the height
     * we want to reach after the animation is completed
     * @param start height in pixels
     * @param end height in pixels
     */
    public void setParams(int start, int end) {

        this.startHeight = start;
        deltaHeight = end - startHeight;
    }

    /**
     * set the duration for the hideshowanimation
     */
    @Override
    public void setDuration(long durationMillis) {
        super.setDuration(durationMillis);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}   

在代码中,创建一个新的 Animation 并将其应用到您想要设置动画的 RelativeLayout:

RelativeLayout relativeLayout = (RelativeLayout) ((LinearLayout) view.findViewById(viewId)).getParent();

// getting the layoutparams might differ in your application, it depends on the parent layout
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();

ResizeAnimation a = new ResizeAnimation(relativeLayout);
a.setDuration(500);

// set the starting height (the current height) and the new height that the view should have after the animation
a.setParams(lp.height, newHeight);

relativeLayout.startAnimation(a);

关于java - 如何为布局的宽度和高度设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18742274/

相关文章:

java - 存储类的 JSON 以及对同一类的对象的引用

java - 在 Weka 中更改堆大小

android - 如何自定义Action Bar字幕字体?

java - 菜单选项卡不响应

android - 动态生成多个Gridview

java - 具有 SSL 和自签名证书的 ActiveMQ;不包含正确证书的客户端能够连接到代理

java - 无效的正则表达式

android - 自动展开并关注 SearchView

java - 如何在TimePickerDialog中显示额外1小时

android - ActionBarSherlock 中溢出图标和操作项之间的间距