我是ConstraintLayout的新手,我正在尝试制作一个简单的动画:
一个按钮,一个文本,当我点击按钮时,文本从屏幕顶部移动到屏幕的机器人。如果你再次点击相同的东西,但反过来。请不要关心重构的可能性,我只是想做一些有效的事情。没有动画似乎有效。
我的XML:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:clickable="true"
android:focusable="true"
android:id="@+id/merde">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:text="Hello"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/plop_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我的alt XML:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:clickable="true"
android:focusable="true"
android:id="@+id/merde">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/textview"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/plop_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我的控制员:
public class Plop extends Controller {
@BindView(R.id.plop_button)
AppCompatButton button;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.plop, container, false);
ButterKnife.bind(this , view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChangeBounds transition = new ChangeBounds();
ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.plop,null);
ConstraintSet constraintSetAlt = new ConstraintSet();
transition.setDuration(1000);
TransitionManager.beginDelayedTransition(controllersLoadingRootConstaintLayout, transition);
ConstraintLayout c1 = (ConstraintLayout) getLayoutInflater().inflate(R.layout.plop_alt,null);
constraintSetAlt.clone(c1);
constraintSetAlt.applyTo(controllersLoadingRootConstaintLayout);
}
});
return view;
}
@Override
public void save() {
}
@Override
public void loadData() {
}
@Override
public void updateView() {
}
}
我做错了什么?
编辑:由于plaskoff,上述问题已得到解决。
我现在正尝试在活动中这样做:
在我的onCreated中:
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setContentView(R.layout.controllers_loading_root);
ChangeBounds transition = new ChangeBounds();
ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout)findViewById(R.id.controllers_loading_root_constraint_layout);
ConstraintSet constraintSetAlt = new ConstraintSet();
transition.setDuration(1000);TransitionManager.beginDelayedTransition(controllersLoadingRootConstaintLayout, transition);
ConstraintLayout c1 = (ConstraintLayout) getLayoutInflater().inflate(R.layout.controllers_loading_root_alt,null);
constraintSetAlt.clone(c1);
constraintSetAlt.applyTo(controllersLoadingRootConstaintLayout);
...
}
我的活动出现在最终状态,没有完成动画。
答案 0 :(得分:0)
评论仍然不够,所以答案(hmpf):
首先,alt版面中的TextView没有ID,不确定问题的部分原因是什么,只是想指出它。
其次,你把克隆的东西放在onClick上。您应该按照official doc中的说明将其放在onCreateView(例如)中,并在onClick中使用applyTo。
答案 1 :(得分:0)
问题是您使用了错误的ConstraintLayout
实例来制作动画:
ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.plop,null)
您正在为新的ConstraintLayout
对象充气,该对象与当前显示的对象不同。所以你应该做的是用以下代码替换上面的行:
ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout) view.findViewById(R.id.merde);
作为旁注,您不必实例化新的ConstraintLayout
对象来克隆约束。您可以使用void clone (Context context, int constraintLayoutId)
从布局文件中克隆它们。
编辑回答后续问题:
如果您希望在显示活动后立即启动动画,则可以将动画的代码移动到活动中的onWindowFocusChanged()
方法:
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.controllers_loading_root);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
ChangeBounds transition = new ChangeBounds();
ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout)findViewById(R.id.controllers_loading_root_constraint_layout);
ConstraintSet constraintSetAlt = new ConstraintSet();
transition.setDuration(1000);
TransitionManager.beginDelayedTransition(controllersLoadingRootConstaintLayout, transition);
ConstraintLayout c1 = (ConstraintLayout) getLayoutInflater().inflate(R.layout.controllers_loading_root_alt,null);
constraintSetAlt.clone(c1);
constraintSetAlt.applyTo(controllersLoadingRootConstaintLayout);
}
}