使用动画将 ImageView 从一个布局移动到同一 activity 内的另一个布局
Move ImageView from one layout to another layout within same activity using animation
我想将此 imageview 从 relative_bottom 翻译成 relative_top 通过动画布局。我想将该图像放在 relative_top 布局的 中心。这是我的启动画面,所以我希望它自动传输。
here is hiiii as imageview.I want to animate this automatically:
I want to place this image here with animation
我不知道如何设置准确的位置。
- 我应该添加什么来获得这样的输出?试了很多方法都没解决。
示例 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>
试试下面的动画
//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"参数使其从底部移动到顶部。
复制自
添加
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);
});
首先为动画创建一个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);
试试这个:
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);
}
我想将此 imageview 从 relative_bottom 翻译成 relative_top 通过动画布局。我想将该图像放在 relative_top 布局的 中心。这是我的启动画面,所以我希望它自动传输。
here is hiiii as imageview.I want to animate this automatically:
I want to place this image here with animation
我不知道如何设置准确的位置。
- 我应该添加什么来获得这样的输出?试了很多方法都没解决。
示例 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>
试试下面的动画
//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"参数使其从底部移动到顶部。
复制自
添加
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);
});
首先为动画创建一个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);
试试这个:
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);
}