Vir*_*ral 0 java animation android android-animation android-view
我正在尝试使用翻译动画将图像视图从屏幕顶部移动到屏幕中央或屏幕中间,即当活动开始时,图像视图开始从屏幕顶部移动到屏幕中间或屏幕中央,其中图像视图空间的顶部和底部图像视图空间在屏幕上显示的完全相同,就像我们在相对布局中所做的一样。在xml文件的布局中,使用父标签中心为true。通常我们会在facebook和whatsapp应用程序中找到这类动画,它们已将它们用于图像的翻译或移动图像视图动画。到目前为止我做了什么。请帮助我解决这些问题。谢谢。
public class MainActivity extends AppCompatActivity {
ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imagview);
RelativeLayout root = (RelativeLayout) findViewById(R.id.rel);
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 50);
anim.setInterpolator((new AccelerateDecelerateInterpolator()));
anim.setAnimationListener(new MyAnimationListener());
anim.setDuration(500);
anim.setFillAfter(true);
imageview.startAnimation(anim);
}
});
}
Run Code Online (Sandbox Code Playgroud)
假设您View位于屏幕左上角的位置,我们希望将其设置为屏幕中央的动画。
布局文件:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root">
<View
android:id="@+id/animated_view"
android:layout_width="50dp"
android:background="#6eed57"
android:layout_height="50dp"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在onCreate():
final View root = findViewById(R.id.root);
final View animatedView = findViewById(R.id.animated_view);
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
animatedView.animate()
.translationX((root.getWidth() - animatedView.getWidth()) / 2)
.translationY((root.getHeight() - animatedView.getHeight()) / 2)
.setInterpolator(new AccelerateInterpolator())
.setDuration(500);
}
});
Run Code Online (Sandbox Code Playgroud)
结果: