我在搞PropertyAnimations
。为此,我有一个简单(但冗长)的设置来玩。通过按钮点击,Views
上有两个y-Axis
切换位置。在进行此转换时,通过缩放视图,一个View将变得更小而另一个更大。
好的,这有效!但我发现了一个令我惊讶的奇怪行为。如果我在其他两个View
已经存在的布局中添加了额外的View
,动画将改变它的行为!
不再有正常的y-Axis
翻译,动画现在会将Views
翻译成一个圆圈,也就是x-Axis
。 我想知道为什么添加一个简单的视图来改变整个动画?
我想我无法更好地描述它,所以这是我完整的设置来重现问题。只需在第一次按下按钮后删除高度为1dp的视图,您将看到。
这是我的视图布局:
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.inputtest.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/origin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="FOO"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#000000"/>
<TextView
android:id="@+id/destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="BAR"/>
</LinearLayout>
<Button
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Switch"/>
</LinearLayout>
这是基本的开始活动:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
TextView foo;
TextView bar;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
foo = (TextView)rootView.findViewById(R.id.origin);
bar = (TextView)rootView.findViewById(R.id.destination);
Button btn = (Button)rootView.findViewById(R.id.switcher);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AnimatorSet scale1 = null;
AnimatorSet scale2 = null;
ObjectAnimator translate1 = null;
ObjectAnimator translate2 = null;
if (foo.getTranslationY() == 0) {
translate1 = ObjectAnimator.ofFloat(foo, "translationY", bar.getTop());
translate2 = ObjectAnimator.ofFloat(bar, "translationY", bar.getTop()*(-1));
scale1 = (AnimatorSet)AnimatorInflater.loadAnimator(getActivity(), R.animator.scaler);
scale2 = (AnimatorSet)AnimatorInflater.loadAnimator(getActivity(), R.animator.scaler2);
} else {
translate1 = ObjectAnimator.ofFloat(foo, "translationY", 0);
translate2 = ObjectAnimator.ofFloat(bar, "translationY", 0);
scale1 = (AnimatorSet)AnimatorInflater.loadAnimator(getActivity(), R.animator.scaler2);
scale2 = (AnimatorSet)AnimatorInflater.loadAnimator(getActivity(), R.animator.scaler);
}
translate1.setDuration(500L);
translate2.setDuration(500L);
scale1.setTarget(foo);
scale2.setTarget(bar);
AnimatorSet set = new AnimatorSet();
set.playTogether(scale1, scale2, translate1, translate2);
set.start();
}
});
return rootView;
}
}
}
动画的scaler.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<set
android:ordering="together">
<objectAnimator
android:propertyName="scaleX"
android:duration="250"
android:valueTo="2.0"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="250"
android:valueTo="2.0"/>
</set>
<set
android:ordering="together">
<objectAnimator
android:propertyName="scaleX"
android:duration="250"
android:valueTo="1.0"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="250"
android:valueTo="1.0"/>
</set>
</set>
动画的scaler2.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<set
android:ordering="together">
<objectAnimator
android:propertyName="scaleX"
android:duration="250"
android:valueTo="0.5"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="250"
android:valueTo="0.5"/>
</set>
<set
android:ordering="together">
<objectAnimator
android:propertyName="scaleX"
android:duration="250"
android:valueTo="1.0"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="250"
android:valueTo="1.0"/>
</set>
</set>
答案 0 :(得分:1)
我玩了一下,找到了解决方案:)对我来说,圆形动画甚至出现时只有2个视图,所以我认为它与它无关,首先我认为它是'布局限制,我将它改为RelativeLayout,没有区别......原因是你的缩放系数为0.5和2.0!视图宽度加倍并减半,因此看起来内容正在移动,但它只是缩放。如果始终将scaleX属性更改为1.0f,则只会看到y转换。
我建议在代码或xml中创建所有动画,这看起来有点凌乱:)而我在这里写的内容可能很有趣...... https://stackoverflow.com/a/22635237/2001247。