当前位置: 首页 > 面试题库 >

如何设置版式的宽度和高度的动画?

元英朗
2023-03-14
问题内容

LinearLayout通过隐藏上的所有其他布局和视图将其扩展到全屏onClick。有一个Relativelayout上面LinearLayout

我想对此应用自定义动画。大小应缓慢增加(例如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);
}

viewIdLinearLayout我点击的ID 。该函数从onClick()


问题答案:

当然可以。

只需编写自己的自定义动画并修改LayoutParams动画视图的。在此示例中,动画为动画视图的 高度 设置了动画。当然,
对宽度进行动画处理也是可能的

它看起来像这样:

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


 类似资料:
  • 问题内容: 我正在尝试实施一个。当我使用“自动布局”时,单元格不会更改大小,而是对齐。 现在我想将其大小更改为例如 我尝试设置 虽然没有用。 有没有办法做到这一点? 编辑 : 看来,答案只会改变我的CollectionView宽度和高度本身。约束中可能存在冲突吗?有什么想法吗? 问题答案: 使用此方法设置自定义像元高度宽度。 确保添加此协议 如果您使用的是 swift 5 或 xcode 11 及

  • 问题内容: 如何通过ImageView编程设置的宽度和高度? 问题答案: 设置ImageView的高度可能为时已晚,但是为了其他有相同问题的人: 希望这可以帮助。 重要。如果要在布局已经“布局”之后设置高度,请确保还调用:

  • 问题内容: 我的元素: 风格: 脚本,使其保持方形: 问题是要在纯CSS中做同样的事情。没有脚本。 问题答案: 我知道有两种技术可以根据元素的高度保持元素的长宽比: 当 高度相对于视口时 : 您可以使用vh单位: 对于 基于父元素的高度的高度 : 您可以使用具有所需纵横比的虚拟图像。宽高比为1:1的示例可以使用1 * 1透明.png图像,也可以使用@vlgalik注释为1 * 1 base64编码

  • 问题内容: 我只是无法设置导航元素的高度和宽度。 有什么想法我做错了吗? 问题答案: 添加显示:块; a-tag是一个内联元素,因此您的高度和宽度将被忽略。

  • 如何设置自定义的高度和宽度的PDF格式像这样: 我要设定高度:13.6厘米,宽度:21.2厘米 如果我导出为PDF,纸张大小始终为A4格式,如何使纸张大小为13.6cm(高度)和**21.2cm(宽度),如上图所示 我在yii2上使用mpdf kartik。。 这是我的控制器代码: 希望能有帮助。。。

  • 问题内容: Tkinter窗口的大小可以通过以下方法控制: 是否有等效的方法来控制Tkinter或ttk帧的大小? @Bryan:我将您的frame1.pack代码更改为以下代码: 我添加了此事件处理程序: 上面的事件处理程序检测到帧的宽度太大,但是无法防止大小增加。这是Tkinter的限制,还是我误解了您的解释? 问题答案: 没有单个魔术功能可以将帧强制为最小或固定大小。但是,您可以通过为框架指