【问题标题】:Performing a view animation after adding the view添加视图后执行视图动画
【发布时间】:2018-05-23 09:27:11
【问题描述】:

所以我的问题是我想在双击 RecyclerView 适配器中的项目后触发 ImageView 上的动画。我检测到适配器中的双击,以int[] location的格式获取点击区域的坐标,调用Fragment中的方法传递位置。

public void performAnimation(int[] location) {

    ImageView imageView = new ImageView(getContext());
    imageView.setImageDrawable(getActivity().getDrawable(R.drawable.ic_imageview));
    ConstraintLayout.LayoutParams params =
            new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    params.setMargins(0, location[0], 0, location[1]);

    imageView.setLayoutParams(params);
    parentViewGroup2.addView(imageView);


    ObjectAnimator translateAnim = ObjectAnimator.ofFloat(imageView, View.TRANSLATION_Y, 0.5f);
    translateAnim.setDuration(2000);
    translateAnim.start();

所以问题是我只能在布局中绘制图像,但动画不会开始。我也尝试过使用带有 beginDelayedTransition 的 TransitionManager,但同样没有。有谁知道我做错了什么?提前致谢。

【问题讨论】:

    标签: android android-layout android-fragments android-animation


    【解决方案1】:

    ObjectAnimator.ofFloat 接收动画将运行的浮点值数组,您只传递 1 个0.5f 参数是不够的。还有,这里的float应该是屏幕上会被平移的绝对像素,而不是偏移值。

    试试这个ViewPropertyAnimator

    imageView.animate().translationY(400).setDuration(200).start();
    

    【讨论】:

    • 非常感谢,它适用于 ViewPropertyAnimator :) 我有点困惑,因为在视图上执行某种动画的各种方式和 API。再次感谢!
    • 很高兴我能帮上忙,您可以将此标记为已回答,以便对其他人有所帮助
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 2012-01-29
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多