gpt4 book ai didi

android - MotionLayout中的TextView在大小动画上闪烁

转载 作者:行者123 更新时间:2023-12-03 13:26:15 25 4
gpt4 key购买 nike

enter image description here

我目前正在尝试使用 MotionLayout 创建工具栏动画。一切正常,除了以约束为中心并用高度和宽度“wrap_content”定义的 TextView 在 TextView 的大小增加时部分闪烁。如果我将 TextView 向左对齐,一切都会按预期工作。这是一个已知的错误还是您知道我该如何解决这个问题?

布局

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:fitsSystemWindows="false">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="@style/AppTheme.AppBarOverlay">

<com.example.motionlayout.CollapsibleToolbar
android:id="@+id/constraintToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="56dp"
app:layoutDescription="@xml/scene_11_header"
app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:src="@drawable/maxmustermann" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Marco Schlosser"
android:textColor="#FFFFFF"/>

<ImageView
android:id="@+id/ivMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_mail" />

<ImageView
android:id="@+id/ivPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_phone" />

<ImageView
android:id="@+id/ivInfo"
android:layout_width="24dp"
android:layout_height="24dp"
app:srcCompat="@drawable/ic_info_button" />


</com.example.motionlayout.CollapsibleToolbar>

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_scrolling" />

场景
<Transition
app:constraintSetEnd="@id/end"
app:constraintSetStart="@id/start"
app:duration="1000"
app:motionInterpolator="linear">

<KeyFrameSet>
<KeyPosition
app:framePosition="30"
app:keyPositionType="deltaRelative"
app:motionTarget="@id/icon"
app:percentX="0.9"
app:percentY="0.1" />

<KeyPosition
app:framePosition="30"
app:keyPositionType="deltaRelative"
app:motionTarget="@id/name"
app:percentX="0.9"
app:percentY="0.1" />
</KeyFrameSet>

<ConstraintSet android:id="@+id/start">

<Constraint
android:id="@id/icon"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Constraint
android:id="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon">

<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="22"/>

</Constraint>

<Constraint
android:id="@id/ivMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="50dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/ivPhone"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />

<Constraint
android:id="@id/ivPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="50dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@+id/ivMail"
app:layout_constraintTop_toBottomOf="@+id/name" />

<Constraint
android:id="@id/ivInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</ConstraintSet>

<ConstraintSet android:id="@+id/end">

<Constraint
android:id="@id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Constraint
android:id="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/icon">

<CustomAttribute
app:attributeName="textSize"
app:customFloatValue="14"/>

</Constraint>

<Constraint
android:id="@id/ivMail"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/ivPhone" />

<Constraint
android:id="@id/ivPhone"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/ivInfo" />

<Constraint
android:id="@id/ivInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

</ConstraintSet>
</Transition>

最佳答案

我找到了解决此闪烁问题的解决方案。

1.扩展运动布局,增加onChange接口(interface):

class CollapsibleToolbar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : MotionLayout(context, attrs, defStyleAttr), AppBarLayout.OnOffsetChangedListener {
val TAG : String = "CollapsibleToolbar"
var onHeaderOffsetChangeed: OnHeaderOffsetChangeed? = null

override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
progress = -verticalOffset / appBarLayout?.totalScrollRange?.toFloat()!!
onHeaderOffsetChangeed?.let {
it.onChanged(progress)
}
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
(parent as? AppBarLayout)?.addOnOffsetChangedListener(this)
}


interface OnHeaderOffsetChangeed{
fun onChanged(progress: Float)
}

}

2.在Activity上设置界面,设置缩放计算
(info_header as CollapsibleToolbar).onHeaderOffsetChangeed = object : CollapsibleToolbar.OnHeaderOffsetChangeed {
override fun onChanged(progress: Float) {
// e.g. 23sp zoom to 14sp
tv_title.textSize = 23 - 9 * progress
}

}

3. 从 MotionScene 中删除 textSize CustomAttribute。
            <CustomAttribute
app:attributeName="textSize"
app:customFloatValue="14"/>

关于android - MotionLayout中的TextView在大小动画上闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com