Android 安卓系统:<;包括>;RippleEffect&;状态列表动画师
我有一个布局,其中包括另一个布局:Android 安卓系统:<;包括>;RippleEffect&;状态列表动画师,android,android-layout,rippledrawable,android-include,Android,Android Layout,Rippledrawable,Android Include,我有一个布局,其中包括另一个布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout1">
<include layout="@layout/my_layout"/>
</LinearLayout>
我需要将RippleEffect和StateListAnimator添加到包含的布局中
例如:
<include layout="@layout/my_layout"
android:stateListAnimator="@anim/lift_up"
android:background="@drawable/ripple_effect"/>
RippleEffect和StateListAnimator都能100%工作。我无法更改包含的布局。因此,我需要对include标记或父布局本身执行效果的原因。
我尝试过这两种方法,但都没有成功
更新
如果可能的话,这应该以编程方式关闭
更新2
其次,一旦它设置了动画?您需要找到视图并调用适当的方法来更改状态列表animator和背景。您可能还需要在包含的布局的根视图上调用setClickable
LinearLayout layout1 = findViewById(R.id.layout1);
View root = layout1.getChildAt(0);
StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context, R.anim.lift_up);
root.setStateListAnimator(sla);
root.setBackground(R.drawable.ripple_effect);
基于线程,
标记似乎只支持android:id
、layout.*
和android:visibility
属性。因此,设置background
和stateListAnimator
的代码无效
要使用的代码修复以下问题:
您的方法正确地启用了涟漪效应,但是SLA不正确
解雇。我看不到有任何上升
如果膨胀的视图是透明的,则立面不可见,则必须设置视图轮廓
或使用一些不透明的颜色使视图可以看到阴影
ViewOutlineProvider的摘录:
视图构建其轮廓的接口,用于阴影投射
和剪辑
要设置outlineProvider
,可以使用方法,也可以通过android:outlineProvider
xml标记进行设置
更新代码:
LinearLayout root =(LinearLayout) findViewById(R.id.container);
StateListAnimator sla = AnimatorInflater.loadStateListAnimator(this, R.animator.lift_up);
root.setStateListAnimator(sla);
root.setClickable(true);
root.setOutlineProvider(ViewOutlineProvider.PADDED_BOUNDS);
root.setBackground(ContextCompat.getDrawable(this, R.drawable.ripple_effect));
涟漪效应.xml
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="?android:colorAccent"
tools:ignore="NewApi">
<item>
<shape
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<!-- Doesn't require outlineProvider
<solid android:color="@android:color/darker_gray"/>
-->
</shape>
</item>
</ripple>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="48dp"/>
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"/>
</item>
</selector>
res/animator/lift\u up.xml
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="?android:colorAccent"
tools:ignore="NewApi">
<item>
<shape
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<!-- Doesn't require outlineProvider
<solid android:color="@android:color/darker_gray"/>
-->
</shape>
</item>
</ripple>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="48dp"/>
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"/>
</item>
</selector>
这似乎足够合法。我已经用基于SLA的另一个方面更新了该问题。您编辑的是另一个问题。你应该打开另一个有特定问题的。酷。对于当前的问题,您的方法正确地启用了涟漪效应,但是没有触发SLA。我看不到有任何上升的地方,你能用另一个动画测试吗?查看问题是以编程方式应用动画还是与立面相关的问题。我在其他位置的卡片视图上使用相同的动画。高度是100%工作感谢解释,但它仍然不工作。我认为问题在于include标记与父级宽度匹配,因此实际标高不可见。我已尽一切可能尝试了你的方法。完全没有avail@TejjD我试过使用match_parent来测量高度和宽度,一切正常。我已经更新了帖子,加入了lift\u up.xml
(提升)代码。为了清晰起见,请查看。只是好奇,你在测试运行中使用的是哪种设备和android版本?这也是我正在使用的代码。我正在测试Nexus 6P,正在运行Marshmallow@TejjD我在Nexus6P中试过这个代码,效果很好。请共享my_layout
及其相关文件,以及您用于扩大版面和设置SLAnimators的java代码。我附带的版面是一个带有2个文本视图的简单图像视图。我使用的代码和你和stephane提供的相同。我觉得宽度有问题。我会继续努力