将涟漪效应添加到以编程方式添加的视图

Add Ripple Effect to Prgramatically added View

我有一个图像视图,由程序添加为:

imagen = new ImageView(getApplicationContext());

现在我想对此应用连锁反应。 我有涟漪效应库 :

<com.skyfishjy.library.RippleBackground
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content"
    app:rb_color="#0099CC"
    app:rb_radius="32dp"
    app:rb_rippleAmount="4"
    app:rb_duration="3000"
    app:rb_scale="6">
    <ImageView
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:id="@+id/centerImage"
        android:src="@drawable/demoImage"/>
</com.skyfishjy.library.RippleBackground>

在上面的代码中,我在 XML 中有图像视图,但我的图像视图是在 运行 时间创建的,所以我如何将波纹效果应用到 运行时间Imageview

Add Ripple Effect to Prgramatically added View

您可以从 xml 扩充视图并以编程方式将其添加到容器布局。

View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false);
        container.addView(inflatedLayout);

my_view.xml

<com.skyfishjy.library.RippleBackground
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content"
    app:rb_color="#0099CC"
    app:rb_radius="32dp"
    app:rb_rippleAmount="4"
    app:rb_duration="3000"
    app:rb_scale="6">
    <ImageView
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:id="@+id/centerImage"
        android:src="@drawable/demoImage"/>
</com.skyfishjy.library.RippleBackground>

drawable 文件夹中创建 ripple.xml

ripple.xml :

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorFocusedHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

然后

imagen.setBackground(context.getDrawable(R.drawable.ripple));

试试这个,而不是使用第三方来产生涟漪。