我希望ViewGroup中的ImageView,当按下时,绘制波纹,这是有效的!但是当按下ViewGroup时,其中的ImageView应保持透明,否则ImageView背景可见:(实际上,您看到的颜色为alpha-orange,是按下时的纹波).
这仅在设备API 21中发生.使用设备API< 21时,我使用选择器,当按下ViewGroup时,图像背景保持透明. layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_detail_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/margin_left"
android:paddingStart="@dimen/margin_left"
android:paddingTop="@dimen/margin_top_item_field"
android:paddingBottom="@dimen/margin_bottom"
android:baselineAligned="false"
android:background="@drawable/layout"
android:stateListAnimator="@anim/touch_raise"
android:orientation="horizontal">
...
<ImageView
android:id="@+id/item_detail_showfield_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_mini_oval"
android:stateListAnimator="@anim/touch_raise"
android:clickable="true"
android:contentDescription="@null"
android:scaleType="center"
android:visibility="invisible"
android:src="@drawable/show_button"/>
...
</LinearLayout>
绘制/ button_mini_oval.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/primary_highlight" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>
可绘制-V21 / button_mini_oval.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime"
android:color="?android:colorControlHighlight">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@android:color/white" />
</shape>
</item>
<item>
<color android:color="@android:color/transparent" />
</item>
</ripple>
我在ripple xml中尝试了很多配置,添加了一个< selector>它可以工作(见下文),但是如果你只是点击ImageView,纹波就不可见了(涟漪动画似乎被剪掉了),只有你按下它才能看到涟漪……
在正常状态下,压力状态下的波纹和透明状态的组合是什么?
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime"
android:color="?android:colorControlHighlight">
<item>
<selector>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@android:color/white" />
</shape>
</item>
<item>
<color android:color="@android:color/transparent" />
</item>
</selector>
</item>
</ripple>
解决方法:
删除android:state_pressed =“true”,我找到了2个解决方案,但在这两种情况下我都丢失了touch_raise动画(为什么?):
带有蒙面纹波:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<selector>
<item>
<shape android:shape="oval">
<solid android:color="@android:color/white" />
</shape>
</item>
<item>
<color android:color="@android:color/transparent" />
</item>
</selector>
</item>
</ripple>
带有未掩盖的小纹波(好于1):
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime"
android:color="?android:colorControlHighlight">
<selector>
<item>
<color android:color="@android:color/transparent" />
</item>
</selector>
</ripple>
我不知道为什么添加< selector>就足够了在透明背景上有波纹!!!很多人都试图实现这一目标,但这不是谷歌的意图,相反,这种情况非常有用.