【问题标题】:Button ripple effect spread to cover only alpha image OR avoid covering transparent spots按钮波纹效果扩散到仅覆盖 alpha 图像或避免覆盖透明点
【发布时间】:2017-12-16 10:57:27
【问题描述】:

我以前读过的东西

  1. Add ripple effect to my button with button background color?
  2. Android ripple for button not working
  3. Ripples on a shape with a transparent background
  4. Transparent background in ImageButton with ripple effect?
  5. RippleDrawable - dev

我需要使自定义波纹效果仅覆盖图像的 Alpha 通道或避免覆盖透明点。

我原来的按钮

            <Button
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:layout_gravity="center"
                android:background="@drawable/btn_round"
                android:foreground="@drawable/btn_ripple"/>

我从按钮建议中尝试过的内容 android:background="?attr/selectableItemBackgroundBorderless‌​" android:foreground="?attr/selectableItemBackgroundBorderless‌​"。它不太奏效。事实上,它忽略了我的图像边框和波纹中设置的形状,而我需要将波纹扩散形状限制为 Alpha 通道。

如您所见,我有 3 个选项可以组合

  1. 自定义按钮形状
  2. 自定义波纹
  3. 从建议的选项中截断 Alpha 通道

所以我尝试将button 转换为imageButton

           <ImageButton
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:layout_gravity="center"
                android:background="@drawable/btn_round"
                android:foreground="@drawable/btn_ripple"
                android:scaleType="fitXY"/>

但是,我尝试了所有 3 个 srcbackgroundforeground 的所有组合,但没有成功。也尝试添加 android:background="?attr/selectableItemBackgroundBorderless‌​" android:foreground="?attr/selectableItemBackgroundBorderless‌​" 涟漪。这也不行。

参考这张图片(那张没有 alpha 但事实并非如此)

【问题讨论】:

    标签: android android-layout button


    【解决方案1】:

    解决方案一目了然:

    在波纹.xml 中

    <item
        android:id="@android:id/mask"
        android:drawable="@drawable/btn_enable" />
    

    里面没有&lt; shape &gt;

    【讨论】:

      【解决方案2】:

      为波纹效果添加蒙版。仅使用蒙版的 Alpha 通道,因此它可以与您的背景图像相同。

      var rippleColorLst = android.content.res.ColorStateList.valueOf(
          android.graphics.Color.argb(255,50,150,255)
      );
      var ripple = new android.graphics.drawable.RippleDrawable(
          rippleColorLst,
          yourImageDrawableWithAlpha,
          yourImageDrawableWithAlpha   // the 3rd parameter is mask
      ); 
      yourButtor.setBackground(ripple);
      

      (JavaScript/NativeScript代码见谅,希望大家能看懂。)

      【讨论】:

        猜你喜欢
        • 2015-03-04
        • 2011-06-09
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 2015-07-23
        • 2014-02-06
        • 2020-05-30
        • 1970-01-01
        相关资源
        最近更新 更多