【问题标题】:android animation move view to another viewandroid动画将视图移动到另一个视图
【发布时间】:2022-01-22 19:18:59
【问题描述】:

我有两个不同布局的视图,我想将一个移到另一个。我的代码有什么问题? Y 动画播放错误。第一个视图位于片段的布局中,第二个位于状态栏中

    ...
    int p1[] = new int[2];
    int p2[] = new int[2];
    viewOne.getLocationInWindow(p1);
    viewTwo.getLocationInWindow(p2);


    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet
            .play(ObjectAnimator.ofFloat(expandedImageView, "X", p1[0], p2[0] - p1[0]))
            .with(ObjectAnimator.ofFloat(expandedImageView, "Y", p1[1], p2[1] - p1[1]))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale));

【问题讨论】:

标签: android android-layout coordinates android-animation


【解决方案1】:

我有另一个解决方案:(将 viewOne 移到 viewTwo)

 TranslateAnimation animation = new TranslateAnimation(0, viewTwo.getX()-viewOne.getX(),0 , viewTwo.getY()-viewOne.getY());
    animation.setRepeatMode(0);
    animation.setDuration(3000);
    animation.setFillAfter(true);
    viewOne.startAnimation(animation); 

【讨论】:

  • 对不起,我尝试了这段代码,但无法正常工作。我有一个图像视图,我想将它移动到另一个固定的图像视图,但实际上我的图像在对角线上以某种方式向上移动
  • 注意: 视图初始化时会返回位置0,请尝试在new Handler().postDelayed(new Runnable{}, 200);执行此操作
  • @KirillKarmazin 或者使用Handler 你也可以在viewTreeObserver 上使用onGlobalLayoutListener,这样上面的代码就在viewTwo.viewTreeObserver.addOnGlobalLayoutListener { // Animation code here } 里面。
【解决方案2】:

试试这个

private fun moveView(viewToBeMoved: View, targetView: View) {
    val targetX: Float =
        targetView.x + targetView.width / 2 - viewToBeMoved.width / 2
    val targetY: Float =
        targetView.y + targetView.height / 2 - viewToBeMoved.height / 2

    viewToBeMoved.animate()
        .x(targetX)
        .y(targetY)
        .setDuration(2000)
        .withEndAction {
            targetView.visibility = View.GONE
        }
        .start()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多