【问题标题】:Move ImageView from one layout to another layout within same activity using animation使用动画将 ImageView 从一个布局移动到同一活动中的另一个布局
【发布时间】:2017-06-05 06:44:36
【问题描述】:

我想通过动画将这个 imageviewrelative_bottom 转换为 relative_top 布局。我想将该图像放在 relative_top 布局的中心。这是我的启动画面,所以我希望它自动传输。

我不知道如何设置确切的位置。

  • 我应该添加什么来获得这样的输出?我尝试了很多方法,但无法找到解决方案。

示例 xml 文件:

<LinearLayout android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       android:background="@drawable/background"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg"
            android:layout_weight="2"
            android:layout_gravity="center"
            android:id="@+id/relative_top"
            android:orientation="vertical">

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:id="@+id/bottom"
            android:layout_gravity="center_horizontal"
            android:gravity="top">
            <ProgressBar>
               ....
               </Progressbar>        
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/relative_bottom"
                android:layout_alignTop="@id/progressBar"
                android:layout_centerInParent="true"
                >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageview"
                    android:src="@drawable/logo"
                    android:scaleType="fitCenter"
                    />

            </RelativeLayout>

        </RelativeLayout>

</LinearLayout>

【问题讨论】:

  • 为两个位置添加图片,这样可以帮助您轻松理解问题。
  • 你试过这种方式吗:- stackoverflow.com/a/22370395/7806873
  • @NitinPatel 我已经编辑了我的问题。请检查

标签: android android-layout animation android-animation


【解决方案1】:

here复制

添加

compile 'com.android.support:transition:25.2.0'

到您的应用程序依赖项。 然后,在改变重力之前添加这一行(来自android.support.transition包的TransitionManager)

TransitionManager.beginDelayedTransition(parentOfAnimatedView);

例如:

mContainer.setOnClickListener(v -> {
        TransitionManager.beginDelayedTransition((ViewGroup) mContainer.getParent());
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mContainer.getLayoutParams();
        layoutParams.gravity = Gravity.TOP;
        mContainer.setLayoutParams(layoutParams);
    });

【讨论】:

    【解决方案2】:

    试试这个:

    public void moveImage(){
    
        //layout is your Relativelayout
        TransitionManager.beginDelayedTransition(layout);
    
        RelativeLayout.LayoutParams imageparam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        //add Rule where you want to align it
        imageparam.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    
        imageView.setLayoutParams(imageparam);
    }
    

    【讨论】:

    • 谢谢。它成功了。但是你能告诉我如何从中间过渡到顶部吗?在我的输出中,我一半的图像标志从屏幕上消失了。 n 我想把它放在顶部布局的中心。
    • 好吧,为什么你想在你的 xml 中有 230000 种不同的布局,这首先不是一个好习惯。而且我不确定你是否可以使用 TransitionManager 实现这种类型的过渡,因为你想在不同的布局之间制作动画,如果这不可能,你可以尝试使用 ObjectAnimator 制作一个看起来很酷的淡入淡出动画。
    • PS:如果我的回答有帮助,我将不胜感激+rep(评论左侧),因为我想提高我的声誉,并按复选标记,谢谢:D跨度>
    • 我没有得到完美的解决方案。但效果比以前更好。
    【解决方案3】:

    试试下面的动画

    //0,0 is the  current coordinates
    Animation an = new TranslateAnimation(fromX,fromY,toX,toY);
    an.setFillAfter(true);// to keep the state after animation is finished
    imageView.startAnimation(an);// to start animation obviously
    

    获取屏幕高度并使用它设置“toY”参数,使其从下向上移动。

    【讨论】:

      【解决方案4】:

      首先为动画创建一个xml:

      fab_show.xml

      <?xml version="1.0" encoding="utf-8"?>
      

      <!--Move-->
      <translate
          android:duration="650"
          android:fromXDelta="650%"
          android:fromYDelta="350%"
          android:interpolator="@android:anim/linear_interpolator"
          android:toXDelta="0%"
          android:toYDelta="0%">
      
      </translate>
      

      fromXDelta 和 fromYDelta 是 imageView(在我的例子中是 FAB)的起始位置,您可以根据自己的要求进行调整,toXDelta、toYDelta 是动画完成后您希望项目所在的位置。

      这是你如何使用这个动画:

       private ImageView img;
      
      Animation fab_show = AnimationUtils.loadAnimation(getApplication(), R.anim.hsfab1_show);
      

      触发动画:

      img = (ImageView) findViewById(R.id.your_imageview);
      img.startAnimation(fab_show);
      

      【讨论】:

      • 我已经尝试过了。这种效果仅限于我的 relative_bottom 布局高度。它不会过渡到 relative_top 布局。
      猜你喜欢
      • 2016-09-30
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多