Android 中的动画图像转换
Animate Image transitions in Android
我有 4 个 ImageView(请看下图)
4张图片的代码如下:
<LinearLayout
android:id="@id/LinearLayout_fromAddItemActivity_Priority"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/FAB_fromAddItemActivity_AddItem"
app:layout_constraintTop_toBottomOf="@+id/TextView_fromAddItemActivity_SubtitlePriority"
app:layout_constraintVertical_chainStyle="packed">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P1}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority1Icon"
app:srcCompat="@drawable/ic_priority_1"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P2}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority2Icon"
app:srcCompat="@drawable/ic_priority_2"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P3}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority3Icon"
app:srcCompat="@drawable/ic_priority_3"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P4}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority4Icon"
app:srcCompat="@drawable/ic_priority_4"/>
单击这些图像中的任何一个,我希望它们如下图所示进行过渡,单击该图像位于顶部!
有人可以帮我解决这些问题吗?
(我对Android中的过渡和动画不太了解,我只知道如何在活动之间进行过渡)。
您可以使用此功能将视图移动到屏幕的水平中心
private fun moveViewToScreenCenter(view:View) {
val dm = DisplayMetrics()
getWindowManager().getDefaultDisplay().getMetrics(dm)
val originalPos = IntArray(2)
view.getLocationOnScreen(originalPos)
val xDelta = (dm.widthPixels - view.getMeasuredWidth() - originalPos[0]) / 2
val yDelta = originalPos[1]//because you want to centre it horizontally
val animSet = AnimationSet(true)
animSet.setFillAfter(true)
animSet.setDuration(1000)
animSet.setInterpolator(BounceInterpolator())
val translate = TranslateAnimation(0, xDelta, 0, yDelta)
animSet.addAnimation(translate)
val scale = ScaleAnimation(1f, 2f, 1f, 2f, ScaleAnimation.RELATIVE_TO_PARENT, .5f, ScaleAnimation.RELATIVE_TO_PARENT, .5f)
animSet.addAnimation(scale)
view.startAnimation(animSet)
}
我认为您可以在 LinearLayout 中设置 android:animateLayoutChanges="true"
,然后只设置需要隐藏的 ImageView 的可见性,如 View.GONE
。
我有 4 个 ImageView(请看下图)
4张图片的代码如下:
<LinearLayout
android:id="@id/LinearLayout_fromAddItemActivity_Priority"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/FAB_fromAddItemActivity_AddItem"
app:layout_constraintTop_toBottomOf="@+id/TextView_fromAddItemActivity_SubtitlePriority"
app:layout_constraintVertical_chainStyle="packed">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P1}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority1Icon"
app:srcCompat="@drawable/ic_priority_1"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P2}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority2Icon"
app:srcCompat="@drawable/ic_priority_2"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P3}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority3Icon"
app:srcCompat="@drawable/ic_priority_3"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="setPriority"
android:tag="@{Constant.ENUM_PRIORITY_P4}"
android:contentDescription="@string/ContentDescription_fromAddItemActivity_Priority4Icon"
app:srcCompat="@drawable/ic_priority_4"/>
单击这些图像中的任何一个,我希望它们如下图所示进行过渡,单击该图像位于顶部!
有人可以帮我解决这些问题吗?
(我对Android中的过渡和动画不太了解,我只知道如何在活动之间进行过渡)。
您可以使用此功能将视图移动到屏幕的水平中心
private fun moveViewToScreenCenter(view:View) {
val dm = DisplayMetrics()
getWindowManager().getDefaultDisplay().getMetrics(dm)
val originalPos = IntArray(2)
view.getLocationOnScreen(originalPos)
val xDelta = (dm.widthPixels - view.getMeasuredWidth() - originalPos[0]) / 2
val yDelta = originalPos[1]//because you want to centre it horizontally
val animSet = AnimationSet(true)
animSet.setFillAfter(true)
animSet.setDuration(1000)
animSet.setInterpolator(BounceInterpolator())
val translate = TranslateAnimation(0, xDelta, 0, yDelta)
animSet.addAnimation(translate)
val scale = ScaleAnimation(1f, 2f, 1f, 2f, ScaleAnimation.RELATIVE_TO_PARENT, .5f, ScaleAnimation.RELATIVE_TO_PARENT, .5f)
animSet.addAnimation(scale)
view.startAnimation(animSet)
}
我认为您可以在 LinearLayout 中设置 android:animateLayoutChanges="true"
,然后只设置需要隐藏的 ImageView 的可见性,如 View.GONE
。