【问题标题】:Add Ripple Effect to Prgramatically added View将波纹效果添加到以编程方式添加的视图
【发布时间】:2017-03-12 07:27:41
【问题描述】:

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

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

【问题讨论】:

    标签: android android-layout android-view android-xml ripple


    【解决方案1】:

    为实用添加的视图添加波纹效果

    您可以从 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>
    

    【讨论】:

      【解决方案2】:

      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));
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-05
        • 1970-01-01
        • 2017-10-09
        • 2017-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多