作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嵌套在我的主要 ConstraintLayout 中,我有另一个 ConstraintLayout,Id = stripLayout
,里面有一个 ImageView 和几个 Button。我希望能够左右滑动内部 ConstraintLayout(图像、按钮和所有),为此我正在尝试如下代码所示的动画:
ConstraintLayout 按预期滑动,但在动画完成后它会重置到初始位置。这可能与 onAnimationEnd()
方法的代码似乎对最终位置没有影响有关,因为我尝试设置 params.leftMargin = 0
和其他值而不会注意到行为上的任何差异。显然这适用于其他帖子(Android translate animation - permanently move View to new position using AnimationListener)中已建立的 View ,但如何使用 ConstraintLayout 来实现?
ConstraintLayout stripImageLayout = findViewById(R.id.stripLayout);
TranslateAnimation anim_to_right = new TranslateAnimation(0, pixToSlide, 0, 0);
anim_to_right.setDuration(1000);
anim_to_right.setAnimationListener(new TranslateAnimation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams)stripImageLayout.getLayoutParams();
params.leftMargin += pixToSlide;
stripImageLayout.setLayoutParams(params);
}
});
stripImageLayout.startAnimation(anim_to_right);
和activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:onClick="onBackgroundClick"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/outletInfoLayout"
android:layout_width="151dp"
android:layout_height="387dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="100dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/ThresholdValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="NONE"
app:layout_constraintBottom_toBottomOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toTopOf="@+id/textView4"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Threshold:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/deviceName"
android:layout_width="136dp"
android:layout_height="80dp"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="31dp"
android:text="Outlet: "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/deviceName" />
<TextView
android:id="@+id/outletNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/ThresholdValue"
app:layout_constraintTop_toTopOf="@+id/textView2"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/stripLayout"
android:layout_width="103dp"
android:layout_height="475dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/outlet3button"
android:layout_width="60dp"
android:layout_height="37dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="8dp"
android:background="@android:color/transparent"
android:onClick="onOutlet3ButtonClick"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outlet2button" />
<Button
android:id="@+id/outlet1button"
android:layout_width="60dp"
android:layout_height="37dp"
android:layout_marginStart="8dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="8dp"
android:background="@android:color/transparent"
android:onClick="onOutlet1ButtonClick"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/smartstripImage" />
<Button
android:id="@+id/outlet2button"
android:layout_width="60dp"
android:layout_height="37dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="8dp"
android:background="@android:color/transparent"
android:onClick="onOutlet2ButtonClick"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outlet1button" />
<Button
android:id="@+id/outlet5button"
android:layout_width="60dp"
android:layout_height="37dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="8dp"
android:background="@android:color/transparent"
android:onClick="onOutlet5ButtonClick"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outlet4button" />
<ImageView
android:id="@+id/smartstripImage"
android:layout_width="81dp"
android:layout_height="441dp"
android:contentDescription="@string/smartstrip_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/strip" />
<Button
android:id="@+id/outlet4button"
android:layout_width="60dp"
android:layout_height="37dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="8dp"
android:background="@android:color/transparent"
android:onClick="onOutlet4ButtonClick"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outlet3button" />
<Button
android:id="@+id/outlet6button"
android:layout_width="60dp"
android:layout_height="37dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="8dp"
android:background="@android:color/transparent"
android:onClick="onOutlet6ButtonClick"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outlet5button" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
最佳答案
您必须使用 ConstraintSet
来实现此目的。这尚未经过全面测试,因此可能需要编辑此答案,但类似这样的内容,请确保将 id 添加到父 ConstraintLayout
:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(R.id.parentLayout);
constraintSet.connect(R.id.outletInfoLayout, ConstraintSet.START, R.id.parentLayout, ConstraintSet.START, startMarginInPixels);
constraintSet.applyTo(R.id.parentLayout);
不过,为了让整个事情变得更简单,我会使用 MotionLayout
,尽管它仍处于测试阶段。
关于android - 如何在运行时永久移动整个 ConstraintLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58561531/
我是一名优秀的程序员,十分优秀!