我想通过动画将 imageview 从 relative_bottom 转换为 relative_top 布局。我想将该图像放在 relative_top 布局的中心。这是我的闪屏,所以我想让它自动转换。
here is hiiii as imageview.I want to animate this automatically:
我不知道如何设置确切的位置。
示例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 :(得分:2)
从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);
});
答案 1 :(得分:1)
尝试以下动画
//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”参数,使其从下往上移动。
答案 2 :(得分:1)
首先为动画创建一个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)的起始位置,你可以根据你的要求和toDDelta调整,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);
答案 3 :(得分:1)
试试这个:
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);
}