我正在尝试在不同视图上实现一组动画,这些视图是启动画面的组件。
我已经阅读了开发者网站上的属性动画,但我无法弄清楚如何将视图(翻译+缩放)设置为父布局的中心。如果我将值50%或50%p传递给valueFrom属性,则崩溃说它是无效值。
我如何实现这一目标?我现在的动画xml在下面。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<set>
<objectAnimator
android:duration="1000"
android:propertyName="x"
android:valueFrom="50%"
android:valueTo="50%" />
<objectAnimator
android:duration="1000"
android:propertyName="y"
android:valueFrom="0"
android:valueTo="50%" />
</set>
答案 0 :(得分:1)
您可以通过编程方式在代码中执行此操作:
以下代码会动画从屏幕左上角到中心的视图。
// Run on UI thread so that the height and widths are set
Runnable r = new Runnable() {
@Override
public void run() {
// Assign the animation which makes it drop down into view
View view = yourParentView.findViewById(R.id.view);
View yourParentView = findViewById(R.id.parent);
int parentHeight = yourParentView.getHeight();
int parentWidth = yourParentView.getWidth();
int viewHeight = view.getHeight();
int viewWidth = view.getWidth();
Animation animation = getAnimation((parentHeight / 2) - (viewHeight / 2), (parentWidth / 2) - (viewWidth / 2));
view.startAnimation(animation);
}
};
new Handler(Looper.getMainLooper()).post(r );
private static AnimationSet getAnimation(int x, int y) {
AnimationSet animSet = new AnimationSet(false);
animSet.addAnimation(new TranslateAnimation(0, 0, x, y));
animSet.setDuration(DURATION);
return animSet;
}