添加视图后执行视图动画
Performing a view animation after adding the view
所以我的问题是我想在双击 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,但还是没有。有谁知道我做错了什么?提前致谢。
ObjectAnimator.ofFloat
接收一个浮点值数组,动画将 运行 通过该值,您只传递 1 个 0.5f
参数是不够的。另外,这里的 float
应该是屏幕上将被平移的绝对像素,而不是偏移值。
试试这个 ViewPropertyAnimator
代替:
imageView.animate().translationY(400).setDuration(200).start();
所以我的问题是我想在双击 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,但还是没有。有谁知道我做错了什么?提前致谢。
ObjectAnimator.ofFloat
接收一个浮点值数组,动画将 运行 通过该值,您只传递 1 个 0.5f
参数是不够的。另外,这里的 float
应该是屏幕上将被平移的绝对像素,而不是偏移值。
试试这个 ViewPropertyAnimator
代替:
imageView.animate().translationY(400).setDuration(200).start();