【问题标题】:Setting transparency for widget background设置小部件背景的透明度
【发布时间】:2013-06-15 18:21:50
【问题描述】:

我有一个线性布局的主屏幕小部件。我正在尝试使用远程视图在运行时为背景设置 Alpha,但小部件未加载。

我正在使用这个:

remoteView.setFloat(R.id.widget_background, "setAlpha", (float)0.7);

以相同的方式设置背景颜色或文本颜色,有效。 如何使用远程视图设置背景透明度?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    有一个使用十六进制背景颜色的简单解决方案。你有一个颜色,例如红色 - #ff0000

    要为您的应用小部件设置背景红色,您可以使用:

    widget.setInt(R.id.widget_list, "setBackgroundColor", Color.parseColor("#ff0000"));
    

    对于颜色透明度,只需在您的颜色前面添加您想要多少颜色 - 透明度关系,例如:

    对于 20% 的颜色将是 #20ff0000

    对于 85% 颜色#85ff0000,您将拥有:

    【讨论】:

      【解决方案2】:

      1.您可以使用 Style.xml 使背景透明,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
        <style name="Theme.Transparent" parent="android:Theme">
          <item name="android:windowIsTranslucent">true</item>
          <item name="android:windowBackground">@android:color/transparent</item>
          <item name="android:windowContentOverlay">@null</item>
          <item name="android:windowNoTitle">true</item>
          <item name="android:windowIsFloating">true</item>
          <item name="android:backgroundDimEnabled">false</item>
        </style>
      </resources>
      

      将此文件设置为小部件的主题。 要么 2.在你的颜色代码后面加上80,如下所示:#80000000 希望它有所帮助..您可以使用 #80FFFFFF 作为背景而不使用任何颜色。我没有在代码中尝试过,但可能会有所帮助。

      【讨论】:

      • 但我想动态改变透明度。当用户选择透明度(从 0 到 1)时,小部件会更新。
      • 为此,您可以使用颜色的 HashMap,其中颜色为值,而 1 到 10 将作为我在回答中给出的第二个选项的关键。并设置背景颜色根据用户选择的 hashmap 键。为此,您需要根据透明度使用不同的颜色代码。从黑色到白色或您喜欢添加的任何颜色。
      【解决方案3】:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:background="@android:color/transparent">
      
      
          <ImageView
              android:id="@+id/widget_image"
              android:layout_width="110sp"
              android:layout_height="110sp"
              android:layout_gravity="center"
              android:src="@drawable/flashlight1" />
      </LinearLayout>
      

      只需将布局的背景设置为透明。添加以下代码。这对我有用。

      android:background="@android:color/transparent"
      

      【讨论】:

        【解决方案4】:

        我使用了以下解决方案:

        float opacity = 0.3f;           //opacity = 0: fully transparent, opacity = 1: no transparancy
        int backgroundColor = 0x000000; //background color (here black)
        remoteView.setInt( R.id.loMain, "setBackgroundColor", (int)(opacity * 0xFF) << 24 | backgroundColor);
        

        loMain 是我的小部件的主要布局

        如果小部件的主要布局使用具有圆角的形状背景(例如 android:background="@drawable/shape_transparent"),那么这里是一个更复杂的解决方案:

                float transparency = 0.5f;  //0...1
                long colorFilter = 0x01000000L * (long)(255f * transparency);   
                try {
                    final Method[] declaredMethods = Class.forName("android.widget.RemoteViews").getDeclaredMethods();
                    final int len = declaredMethods.length;
                    if (len > 0) {
                        for (int m=0; m<len; m++ ) {
                            final Method method = declaredMethods[m];
                            if (method.getName().equals("setDrawableParameters")) {
                                method.setAccessible(true);
                                method.invoke(remoteView, R.id.loMain, true, -1, (int)colorFilter, PorterDuff.Mode.DST_OUT, -1);
                                break;
                            }
                        }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
        

        您可以使用变量 alfacolorFilter 来满足您的需求。

        【讨论】:

          【解决方案5】:

          Set transparent background of an imageview on Android

          以上链接是此类背景颜色的最佳解决方案。

          下面的黑色代码:-

          <color name="black">#000000</color>
          

          现在,如果您想使用不透明度,则可以使用以下代码:-

          <color name="black">#99000000</color> 
          

          下面是不透明代码:-

          100% — FF

          95% — F2

          90% — E6

          85% — D9

          80% — 抄送

          75% — 高炉

          70% — B3

          65% — A6

          60% — 99

          55% — 8C

          50% — 80

          45% — 73

          40% — 66

          35% — 59

          30% — 4D

          25% — 40

          20% — 33

          15% — 26

          10% — 1A

          5% — 0D

          0% — 00

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-04-30
            • 2018-09-14
            • 2013-11-08
            • 1970-01-01
            • 1970-01-01
            • 2018-12-25
            • 2016-10-09
            相关资源
            最近更新 更多