在图像视图上设置涟漪效果

Jja*_*ang 17 java android ripple android-5.0-lollipop

得到以下图片视图:

<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"/>
Run Code Online (Sandbox Code Playgroud)

如果我没有为图像视图设置位图,则Ripple工作得很好.但是一旦我设置了这样的位图,涟漪效应就消失了:

ImageView iv=((ImageView)rootView.findViewById(R.id.header));
iv.setImageBitmap(myBitmap);
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

我猜这个位图隐藏了涟漪效应,我怎样才能让它可见?已经尝试过:

  1. 将android:background更改为android:foreground,这不起作用.
  2. 在此顶部配置另一个透明ImageView,但由于它是透明的纹波未显示.

有任何想法吗?我见过Lollipop Contacts也是这样做的.

Alb*_*lvo 27

我有一个图像库(用a RecyclerView和a 构建GridLayoutManager).使用Picasso设置图像.为了给图像添加一个波纹,ImageView用a 包裹了FrameLayout.项目布局是:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意android:foreground.它不一样android:background.我尝试过没有 android:clickable="true",android:focusable="true"它也有效,但它并没有伤害.

然后添加一个ripple.xmldrawable到res/drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#7FF5AC8E" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

请注意,当选择项目时,这会在图像顶部显示半透明颜色(适用于版本低于Android 5.0的设备).如果您不想要它,可以将其删除.

然后将ripple.xml带有纹波的drawable 添加到res/drawable-v21:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/coral">
</ripple>
Run Code Online (Sandbox Code Playgroud)

您可以使用默认的涟漪效果而不是自定义ripple.xml,但在图像上很难看到,因为它是灰色的:

android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 5

你可以实现它,将drawable包装在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)
Run Code Online (Sandbox Code Playgroud)

;

  • Ripple需要api 21.你不能在较低的api下使用它. (3认同)

ama*_*Bit 5

对于像我这样的懒人:

android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)

并根据您的风格自定义波纹颜色。

在 values/styles.xml

<style name="colorControlHighlight_blue">
   <item name="colorControlHighlight">@color/main_blue_alpha26</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,在 onCreateView() 膨胀之前的片段中:

@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;
}
Run Code Online (Sandbox Code Playgroud)