是Android 5.0棒棒糖:setColorFilter"泄漏"到其他按钮 [英] Android 5.0 Lollipop: setColorFilter "leaks" onto other buttons

查看:270
本文介绍了是Android 5.0棒棒糖:setColorFilter"泄漏"到其他按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 setColorFilter 来设置我的按钮ONE彩色滤光片。这已经完美的工作,直到的是Android 5.0棒棒糖更新。现在,彩色滤光片似乎漏到我的其他按钮,甚至当我关闭活动并重新打开(其重置,如果我关闭应用程序并重新打开)。

I am using setColorFilter to set the color filter of ONE of my button. This has been working perfectly until the Android 5.0 Lollipop update. Now, the color filter seems to leak onto my other buttons, even when I close the activity and reopen (it resets if I close the app and reopen).

我的styles.xml(V21):(相同,只是在这里年长其父是物质的,之前它是全息)

My styles.xml (v21): (same as older except here its parent is Material, before it was Holo)

<style name="Theme.FullScreen" parent="@android:style/Theme.Material.Light.NoActionBar.Fullscreen">
    <item name="android:buttonStyle">@style/StandardButton</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

我的styles.xml(所有版本):

<style name="StandardButton" parent="android:style/Widget.Button">
    <item name="android:background">@android:drawable/btn_default</item>
</style>

我的按钮:

<Button
    android:id="@+id/mainMenuButton"              
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="mainMenu"
    android:text="@string/button_mainMenu"
    android:visibility="gone" />

我的code:

Button mainMenuButton = (Button) findViewById(R.id.mainMenuButton);
mainMenuButton.getBackground().setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setVisibility(View.VISIBLE);

颜色:

<color name="light_green">#5CD65C</color>

结果:

我打开应用程序,那么游戏活动,所有的按钮都正确显示。我preSS的按钮设置彩色滤光片,回到主菜单,重新打开游戏活动,现在所有的按钮是绿色的。

I open the app, then the game activity and all the buttons are displaying correctly. I press the button to set the color filter, go back to the main Menu and reopen the game activity and now all buttons are green.

任何想法?

推荐答案

OP这里。谢谢您的建议。使用下面code解决了这一问题。

OP here. Thank you for your suggestions. Using the following code fixed the problem.

设置过滤器

Drawable background = getResources().getDrawable(android.R.drawable.btn_default);
background.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setBackground(background); // Use setBackgroundDrawable for API<16
mainMenuButton.setVisibility(View.VISIBLE);

其他按钮没有变成绿色。

The other buttons did not become green.

清除过滤器

接下来发生的事情是,它似乎是Android的(部分),忽视了我的电话,清除彩色滤光片(这恰好后来在code)。 天色,因为它是绿色的,但是当我pressed它,它变成黄色(这是相对于深绿色与过滤器btn_default行为)。为了解决这个问题,我不得不改变我的函数调用为以下内容:

What happened next was, that it seemed that Android (partly) ignored my call to clear the color filter (which happens later on in the code). "Partly", because it was green, but when I pressed it, it became yellow (which is btn_default behavior as opposed to dark green with the filter). To fix this, I had to change my function call order to the following:

mainMenuButton.setVisibility(View.VISIBLE)
mainMenuButton.getBackground().clearColorFilter();
mainMenuButton.invalidate();

注意:然而,这倒按钮的行为。当我在按钮上preSS(住),似乎仍然有它的绿色滤色器的行为,变成深绿色。当我释放它返回到被灰色按btn_default。对此有什么建议?

NOTE: This however inversed the behavior of the button. When I press (and hold) on the button, it seems to still have the green color filter behavior on it and becomes dark green. When I release it returns to being grey as per btn_default. Any suggestions on this?

我希望这至少在某种程度上可以帮助别人这个奇怪的问题,这对我来说只是发生在Android 5.0棒棒糖API级别21。(我已经测试API 8,9,12,13,16,17,18和19在这里并没有出现这个问题。此外,它似乎并没有成为艺术与如我所料,因为让一个Android 4.4.4设备上的技术并没有引起这个问题的AOT编译)。奇怪的是,突变()也不能工作,因为我本来期望。

I hope this at least somehow helps someone else with this strange problem, which for me only occured on Android 5.0 Lollipop API level 21. (I have tested API 8, 9, 12, 13, 16, 17, 18 and 19 where this problem did not occur. Also it doesn't seem to be ART with its AOT compilation as I expected, since enabling ART on an Android 4.4.4 device did not cause this problem.) Strangely, mutate() did not work either, as I would have expected.

这篇关于是Android 5.0棒棒糖:setColorFilter&QUOT;泄漏&QUOT;到其他按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆