Setting transparency for widget background

六眼飞鱼酱① 提交于 2019-12-22 11:16:01

问题


I have a homescreen widget, with linear layout. I'm trying to set Alpha for the background during runtime using remote views, but the widget doesn't load.

I'm using this:

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

Setting background color or text color the same way, works. How can I set the background transparency using remote views?


回答1:


There is a simple solution using background color in hex. You have a color, for example RED - #ff0000

to set background red to your app widget, you can use:

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

for color transparency just add in front of your color how much color - transparency rapport you want, for example:

for 20% color will be #20ff0000:

for 85% color #85ff0000 wou'll have:




回答2:


1.You can use Style.xml to make a backgound transparent like below:

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

Set this file as a theme of your widget. OR 2.Put 80 after # sign in your color code like this:#80000000 Hope it helps..You can use #80FFFFFF for background without any color.I have not tried it in code but may be its helpful.




回答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>

Just set background of layout to transparent. Add below code. this worked for me.

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



回答4:


I used the following solution:

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 is a main layout of my widget

If the main layout of the widget uses shape background (e.g. android:background="@drawable/shape_transparent") which has rounded corner then here is a more complex solution:

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

You can play with variables alfa and colorFilter to meet your needs.




回答5:


Set transparent background of an imageview on Android

Above link is the best solution for this type of Background Color.

Below code for black:-

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

Now if u want to use opacity than you can use below code :-

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

below for opacity code:-

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



来源:https://stackoverflow.com/questions/17187471/setting-transparency-for-widget-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!