2013-06-19 34 views
3

我有一個主屏幕小部件,具有線性佈局。我試圖在運行時使用遠程視圖爲背景設置Alpha,但該小部件不加載。爲小部件背景設置透明度

我使用這個:

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

設置背景顏色或文本顏色以同樣的方式,作品。 如何使用遠程視圖設置背景透明度?

回答

1

1.You可以使用Style.xml做出backgound透明象下面這樣:

<?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> 

設置該文件作爲widget的一個主題。 或 2.在80後註冊#80後註冊你的顏色代碼是這樣的:#80000000 希望它有幫助..你可以使用#80FFFFFF作爲背景沒有任何顏色。我沒有在代碼中試過它,但可能是它的幫助。

+1

但我想動態地改變透明度。當用戶選擇透明度(從0到1)時,小部件將更新。 – domen

+1

爲此,您可以使用顏色爲HashMap 的顏色將值和1到10將作爲您的關鍵作爲第二個選項,我已在答案中給出並根據用戶選擇設置哈希表的關鍵背景顏色。爲此您需要根據透明度使用不同的顏色代碼。從黑色到白色或您想添加的任何內容。 – AndiM

0
<?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" 
1

我使用了下列溶液:

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是我的插件

的主要佈局如果小部件的主要佈局使用形狀背景(例如機器人:背景= 「@ 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來滿足您的需求。

1

在十六進制中使用背景色有一個簡單的解決方案。你有一個顏色,例如紅 - #ff0000

設置背景紅到您的應用程序插件,您可以使用:

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

爲透明色只是在你的顏色多少顏色前加 - 透明融洽你想要的,例如:

爲20%,顏色會#20ff0000enter image description here

85%色#85ff0000 wou'll有: enter image description here

0

Set transparent background of an imageview on Android

上面的鏈接是這種類型的背景色的最佳解決方案。

下面的代碼爲黑色: -

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

現在如果u想使用不透明度比你可以用下面的代碼: -

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

不透明代碼: -

100% - FF

95% - F2

90% - E6

85% - D9

80% - CC

75% - BF

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