Android 使用动画将ImageView从同一活动中的一个布局移动到另一个布局
我想通过动画将此图像视图从相对_底部转换为相对_顶部布局。我想把那个图像放在相对顶部布局的中心。这是我的splashscreen,所以我希望它能自动传输Android 使用动画将ImageView从同一活动中的一个布局移动到另一个布局,android,android-layout,animation,android-animation,Android,Android Layout,Animation,Android Animation,我想通过动画将此图像视图从相对_底部转换为相对_顶部布局。我想把那个图像放在相对顶部布局的中心。这是我的splashscreen,所以我希望它能自动传输 : 我不知道如何设置准确的位置 我应该添加什么来获得这样的输出?我试过很多方法,但都找不到解决办法 示例xml文件: <LinearLayout android:id="@+id/activity_main" android:layout_width="match_parent" androi
- :
- 我应该添加什么来获得这样的输出?我试过很多方法,但都找不到解决办法
<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>
....
尝试下面的动画
//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
获取屏幕高度,并使用它设置“玩具”参数,使其从下向上移动。从
加
添加到您的应用程序依赖项。
然后,在ChangeGravity之前添加这一行(来自android.support.transition包的TransitionManager)
例如:
mContainer.setOnClickListener(v -> {
TransitionManager.beginDelayedTransition((ViewGroup) mContainer.getParent());
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mContainer.getLayoutParams();
layoutParams.gravity = Gravity.TOP;
mContainer.setLayoutParams(layoutParams);
});
首先为动画创建一个xml:
private ImageView img;
Animation fab_show = AnimationUtils.loadAnimation(getApplication(), R.anim.hsfab1_show);
img = (ImageView) findViewById(R.id.your_imageview);
img.startAnimation(fab_show);
fab_show.xml
<?xml version="1.0" encoding="utf-8"?>
要触发动画,请执行以下操作:
private ImageView img;
Animation fab_show = AnimationUtils.loadAnimation(getApplication(), R.anim.hsfab1_show);
img = (ImageView) findViewById(R.id.your_imageview);
img.startAnimation(fab_show);
试试这个:
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);
}
为这两个位置添加图像,这样有助于轻松理解问题。您是否尝试过这种方式:-@NitinPatel我已编辑了我的问题。请检查一下,我已经试过了。这种效果仅限于我的相对底部布局高度。它没有过渡到相对的顶部布局。谢谢。它成功了。但是你能告诉我如何从中间过渡到顶部吗。在我的输出中,我的一半图像徽标从屏幕上消失。我想把它放在我的顶部布局的中心。好吧,不管怎样,为什么你想在xml中有230000个不同的布局,这首先不是一个好的做法。我不确定你能不能用TransitionManager实现这种类型的转换,因为你想在不同的布局之间制作动画,如果这不可能,你可以用ObjectAnimator尝试一个非常酷的淡入淡出动画。PS:如果我的回答有帮助,我会感谢+rep(评论的左侧),因为我想提高我的声誉,同时按下复选标记,谢谢:DI没有得到完美的解决方案。但效果比以前好。
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);
}