我用android中的一些动画创建了简单的应用程序。在我的布局中,我有一个带有sourceImage的ImageView和一个按钮。当我单击按钮时,我希望它同时移动和调整大小。 我使用ObjectAnimator和ValueAnimator来创建动画并与animatorSet一起播放它们。我的问题是我的按钮不能顺利移动。 我查了几个动画库,比如Transitions Everywhere和APIDemos。在这些库中,当我设置ImageBackground时,视图无法顺利移动 他们都有我的问题。 任何人都可以帮我解决这个问题吗?
我的布局代码:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageview"
android:fitsSystemWindows="true"
android:src="@drawable/image"
android:scaleType="fitXY" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame">
<Button
android:layout_width="100dp"
android:layout_height="45dp"
android:id="@+id/btn_start"
android:layout_gravity="center_horizontal|bottom"
android:gravity="center_vertical|center_horizontal"
android:textAllCaps="false"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Start" />
// ....................
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
我的动画代码:
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ButtonAnimation();
}
});
private void ButtonAnimation() {
Animator moveButtonAnimY = ObjectAnimator.ofFloat(btn_start, "translationY", 0, 200)
.setDuration(500);
final ValueAnimator valueAnimator = ValueAnimator.ofFloat(width_btn_start, width_btn_start * 2.0f);
valueAnimator.setDuration(500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
btn_start.getLayoutParams().width = value.intValue();
btn_start.requestLayout();
}
});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(moveButtonAnimY, valueAnimator);
animatorSet.start();
}
答案 0 :(得分:0)
根据维基百科:
动画是通过快速显示一系列彼此差异最小的静态图像来制造运动和变化的过程。
因此,动画对于设备来说是一项非常繁重的任务,并且所引用的平滑度取决于设备配置。
因此,首先检查设备是否有足够的电量来顺利执行该动画。
其次,每次单击按钮时,您的代码都会初始化动画。相反,您应该初始化动画一次,并在每次需要启动动画时使用它们的实例。
更好的代码版本是:
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ButtonAnimation();
}
});
Animator moveButtonAnimY = ObjectAnimator.ofFloat(btn_start,"translationY", 0, 200).setDuration(500);
ValueAnimator valueAnimator = ValueAnimator.ofFloat(width_btn_start, width_btn_start * 2.0f);
valueAnimator.setDuration(500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
btn_start.getLayoutParams().width = value.intValue();
btn_start.requestLayout();
}
});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(moveButtonAnimY, valueAnimator);
private void ButtonAnimation() {
animatorSet.start();
}