Java 在图像视图上设置涟漪效果
已获取以下图像视图:Java 在图像视图上设置涟漪效果,java,android,android-5.0-lollipop,ripple,Java,Android,Android 5.0 Lollipop,Ripple,已获取以下图像视图: <ImageView android:id="@+id/header" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="centerCrop" app:layout_collapseMode="parallax"
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"/>
这是我的ripple.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
我猜位图隐藏了涟漪效应,如何使其可见?已尝试:
有什么想法吗?我见过棒棒糖隐形眼镜也能做到这一点。你可以用RippleDrawable包装可拉丝
ImageView iv=((ImageView)rootView.findViewById(R.id.header));
Drawable image = .... // your image.
RippleDrawable rippledImage = new
RippleDrawable(ColorStateList.valueOf(rippleColor), image, null);
iv.setImageDrawable(rippledImage)
) 我有一个图像库(用RecyclerView
和GridLayoutManager
构建)。这幅图像是用毕加索的作品设置的。为了给图像添加涟漪我用FrameLayout
包装了ImageView
。项目布局为:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:foreground="@drawable/ripple"
android:clickable="true"
android:focusable="true"
>
<ImageView
android:id="@+id/grid_item_imageView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:scaleType="centerInside"
/>
</FrameLayout>
请注意,选中该项目时,图像顶部会显示半透明颜色(适用于版本早于Android 5.0的设备)。如果不需要,可以将其删除
然后将ripple.xml
drawable with ripple添加到res/drawable-v21
:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/coral">
</ripple>
对于像我这样懒惰的人:
android:foreground="?android:attr/selectableItemBackground"
并根据您的风格定制涟漪颜色
在values/styles.xml中
<style name="colorControlHighlight_blue">
<item name="colorControlHighlight">@color/main_blue_alpha26</item>
</style>
使用圆形效果,尝试以下操作:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/colorTema3"
tools:targetApi="lollipop">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/colorPurpleBarra" />
</shape>
</item>
<item
android:id="@android:id/background"
android:drawable="@android:color/transparent" />
我参加聚会有点晚了,但我发现在每个版本的android for TextView中都能工作 只需将此添加到您的文本视图
android:theme="@style/ClickableView"
在样式文件中,添加以下内容:
<style name="ClickableView">
<item name="colorControlHighlight">@android:color/white</item>
<item name="android:background">?selectableItemBackground</item>
</style>
@android:彩色/白色
?selectableItemBackground
但它需要api 21+。是否有一种方法,可能是使用支持,使其能够降低api?Ripple需要api 21。你不能将它与低级api一起使用。好的,这是有效的。如何通过代码将以下xml应用于ripple drawable?我不想在布局中创建RippleDrawable,因为我必须复制粘贴两次布局。什么是“@color/coral”?我得到了这个错误。@Paritosh这是一个颜色资源。在res/values/colors.xml中定义如下颜色:#3F51B5
。显然,你可以把它改成你喜欢的任何东西。@AlbertVilaCalvo,谢谢。我认为它是android资源。android:foreground=“?android:attr/selectableItemBackground”
工作得很好,但图像位于矩形图像视图中,连锁反应确实显示了矩形。如果效果是圆形的,那就太好了。安卓:前台对于像我这样的懒人来说真是太棒了
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getContext().getTheme().applyStyle(R.style.colorControlHighlight_blue, true); //blue ripple color
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/colorTema3"
tools:targetApi="lollipop">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/colorPurpleBarra" />
</shape>
</item>
<item
android:id="@android:id/background"
android:drawable="@android:color/transparent" />
android:theme="@style/ClickableView"
<style name="ClickableView">
<item name="colorControlHighlight">@android:color/white</item>
<item name="android:background">?selectableItemBackground</item>
</style>