【问题标题】:How to set shape's opacity?如何设置形状的不透明度?
【发布时间】:2011-04-13 09:17:14
【问题描述】:

我已经知道如何设置背景图像的不透明度,但我需要设置我的形状对象的不透明度。

在我的 Android 应用程序中,我有这样的:

我想让这个黑色区域有点透明,比如这里,例如我可以通过这个“欢迎...”看到圆圈:

这是我的形状代码:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shape_my"">
    <stroke android:width="4dp" android:color="#636161" />
    <padding android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp" />
    <corners android:radius="24dp" />
</shape>

我该怎么做?

【问题讨论】:

  • 形状对象代码在这里会有所帮助。

标签: android graphics


【解决方案1】:

一般而言,您只需在创建形状时定义一种稍微透明的颜色即可。

您可以通过设置颜色 alpha 通道来实现。

#FF000000 会给你一个纯黑色,而#00000000 会给你一个 100% 透明的黑色(显然它不再是黑色了)。

配色方案是这样的#AARRGGBB,其中 A 代表 alpha 通道,R 代表红色,G 代表绿色,B 代表蓝色。

如果您在 Java 中设置颜色,同样的情况也适用。在那里它只会​​看起来像0xFF000000

更新

在您的情况下,您必须添加一个 solid 节点。如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shape_my">
    <stroke android:width="4dp" android:color="#636161" />
    <padding android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp" />
    <corners android:radius="24dp" />
    <solid android:color="#88000000" />
</shape>

这里的颜色是半透明的黑色。

【讨论】:

  • codeviewer.org/view/code:1974 - shape.xml 。 codeviewer.org/view/code:1975 - layout.xml .codeviewer.org/view/code:1976 - .java setOpacity 不适用于形状。我尝试将 android:background="#00000000" 添加到 shape.xml,但没有任何反应。
  • 这很奇怪......我设置的颜色值无关紧要,我的形状完全透明 =(
  • 在我看来,不透明度只能设置为视图或图像...因为当我写 android:background="#88000000" 而不是 android:background="@drawable/形状”在我的 LinearLayout 中,它可以工作。
  • 它也适用于形状。您是否尝试过我上面发布的内容?
  • 是的,但它不起作用。您是否在您的机器上尝试过所有这些代码?
【解决方案2】:

使用下面的代码作为progress.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#00000000" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#00000000" />
            </shape>
        </clip>
    </item>

</layer-list>

地点:

  • “progress”是拇指之前的当前进度,“secondaryProgress”是拇指之后的进度。
  • color="#00000000" 是完美的透明度
  • 注意:上面的文件来自默认的 android res,适用于 2.3.7,它可在以下 android 源中获得:frameworks/base/core/res/res/drawable/progress_horizo​​ntal.xml。对于较新的版本,您必须找到与您的 Android 版本相对应的搜索栏的默认可绘制文件。

之后在包含 xml 的布局中使用它:

<SeekBar
    android:id="@+id/myseekbar"
    ...
    android:progressDrawable="@drawable/progress"
    />

您还可以使用自定义图标 seek_thumb.png 来自定义拇指:

android:thumb="@drawable/seek_thumb"

【讨论】:

    【解决方案3】:

    使用这个,我已经把它写到我的应用程序中了,

    <?xml version="1.0" encoding="utf-8"?>
    <!--  res/drawable/rounded_edittext.xml -->
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" android:padding="10dp">
        <solid android:color="#882C383E"/>
        <corners
            android:bottomRightRadius="5dp"
            android:bottomLeftRadius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"/>
    </shape>
    

    【讨论】:

      【解决方案4】:

      除了将 alpha 转换为十六进制值,我们还可以使用颜色状态列表来定义 alpha。

      res/values/colors.xml

      <color name="colorPrimary">#0000FF</color>
      

      res/values/color_primary_20.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:alpha="20" android:color="?attr/colorPrimary" />
      </selector>
      

      res/drawble/a_shape.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="oval">
          <stroke
              android:width="2dp"
              android:color="@color/color_primary_20" />
      </shape>
      

      【讨论】:

      • 酷我正在寻找的东西!
      【解决方案5】:

      如果有人尝试使用 Cardview,他们可以尝试此代码。

      <androidx.cardview.widget.CardView
                  android:id="@+id/cvView"
                  android:layout_width="48dp"
                  android:layout_height="48dp"
                  android:layout_marginBottom="16dp"
                  android:shape="ring"
                  android:visibility="visible"
                  app:cardCornerRadius="75dp"
                  app:cardBackgroundColor="@android:color/transparent"
                  app:layout_constraintBottom_toTopOf="@id/fab"
                  app:layout_constraintEnd_toEndOf="@+id/fab"
                  app:layout_constraintStart_toStartOf="@+id/fab">
      
                  <ImageView
                      android:id="@+id/ivImage"
                      android:orientation="horizontal"
                      android:layout_width="48dp"
                      android:layout_height="48dp"
                      android:layout_gravity="center"
                      android:padding="@dimen/_10sdp"
                      android:background="@drawable/custom_circle_shape"
                      android:src="@drawable/ic_fab_rpt"/>
      
      </androidx.cardview.widget.CardView>
      

      custom_circle_shape.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="oval">
          <solid android:color="#4D4953D2" />
          <corners android:radius="40dp" />
          <stroke
              android:width="@dimen/_2sdp"
              android:color="#7882FA" />
      </shape>
      

      【讨论】:

        【解决方案6】:

        ?attr/colorSurface这样的主题属性颜色设置alpha:

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:tint="?attr/colorSurface"> <!-- specify your tint color -->
            <solid android:color="#AA000000" /> <!-- instead of `AA` specify your alpha level -->
        </shape>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-14
          • 1970-01-01
          • 2014-09-05
          • 2011-05-26
          • 2016-03-01
          • 2011-07-02
          • 2015-11-10
          • 1970-01-01
          相关资源
          最近更新 更多