如何在屏幕外定位ConstraintLayout的子项(以便稍后在屏幕上翻译它们)



我正在尝试用一个屏幕外元素构建一个动态ConstraintLayout,稍后可以在其中进行转换。但是,我无法使元素在屏幕外启动。

<android.support.constraint.ConstraintLayout 
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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/onscreen_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<ImageView
android:id="@+id/offscreen_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

通过将图像的左侧设置为全屏布局的右侧,图像应该在屏幕外——不是吗?相反,结果是offscreen_iv被直接定位在onscreen_iv之上。编辑:如果我将offscreen_iv设置在onscreen_iv的右边,而不是父屏幕的右边,结果是一样的。

这似乎与将宽度设置为match_parent有关,就好像我将其设置为一个原始数字一样,它似乎有效,但当然是错误的大小。我需要我的图像是屏幕宽度。

感谢您的帮助!

有趣的问题。

我找到了一种让你的offscreen_iv消失的方法,那就是把你的图像放在parent上面。

你仍然可以保留你的android:layout_width="match_parent"

试试这个:

<ImageView
android:id="@+id/offscreen_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="parent" />

您可以在任何视图上使用android:translationX="400dp"属性,并使用"属性动画器"为同一属性设置动画。将值设置为0以获得默认位置。

其他选项包括

android:translationY="400dp"
android:translationZ="400dp"

您必须设置offscreen_iv rightOf onscren_iv

<ImageView
android:id="@+id/onscreen_iv"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<ImageView
android:id="@+id/offscreen_iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/onscreen_iv" />

设置offscreen_iv为不可见

如果你想从右向左滑动,在动画文件夹中创建翻译

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="5000" />
</set>

然后启动动画

offscreen_iv.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate));

最新更新