【问题标题】:ImageView shows Drawable translucent/transparent on some devicesImageView 在某些设备上显示 Drawable 半透明/透明
【发布时间】:2014-10-22 00:12:12
【问题描述】:

我们有一个 TextView,其中包含来自应用资源的 drawable-left 和 drawable-right。在某些设备上,例如

  • 运行 Android 4.1.2 的索尼 Xperia L,
  • 三星 Galaxy Young 或
  • 三星 Galaxy SII

我们正面临一个奇怪的问题。可绘制对象将显示为半透明/透明。在其他设备上,如

  • 三星 Galaxy S4 (4.4.2) 或
  • HTC ONE V (4.0.3)

drawable 将正确显示。

这是 Layout 定义的截图:

        <TextView
            android:id="@+id/button_current_warnings"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical"
            android:background="@drawable/selector_classic_list_item"
            android:layout_gravity="center_vertical"
            android:drawablePadding="8dp"
            android:drawableLeft="@drawable/ic_classic_current_warning_white"
            android:drawableRight="@drawable/ic_classic_row_arrow"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:maxLines="2"
            android:ellipsize="end"
            android:text="@string/current_warnings"
            android:textSize="?attr/warningGermanyBoxTextSize"
            android:textColor="?attr/itemFavoriteTitleTextColor"/>

在 HTC One V 设备上,这个布局看起来很不错:

在 Sony Xperia L 上,由于 imageview 可绘制对象的透明度,布局被破坏。两个drawable都只显示了一点点:

此外,如果我们将可绘制对象更改为不同的对象,则结果在两种设备上都很好:

宏达电:

索尼:

另外,我附上了用于我当前测试的两个可绘制对象(它们都是白色的,因此你不会在这里看到任何东西):

错误显示的drawable:

正确显示的drawable:

有没有人遇到同样的问题或有什么提示给我们?

更新:在我的代码中,我只在这个 TextView 上设置了一个 ClickListener:

     buttonGermany = returnView.findViewById(R.id.button_current_warnings);
     buttonGermany.setVisibility(View.VISIBLE);
     buttonGermany.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do something
        }
     });

【问题讨论】:

  • 您是否在应用程序中的任何地方从代码中更改了@drawable/ic_classic_current_warning_white drawable(可绘制,而非视图)的 alpha?
  • alpha 不是直接在代码中操作的。但是这个drawable也是selector的一部分,它基于一个状态使用这个drawable。 schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
  • 这可能是由于您的应用中的一些小细节造成的...提示:这个元素的层次结构如此复杂有什么原因吗?您可以将整个 FrameLayout(有 3 个孩子)减少为单个 TextView(因此将减少 3 个视图)。两个图标都应放置为android:drawableLeftandroid:drawableRight + 有这个蓝色android:background
  • 感谢 TextView 的提示。这是一个很好的改进,但它不能解决我的问题。我将用更多代码更新我的问题。
  • 当前调查。我认为问题只出现在 Jelly Bean 4.1.2 设备上。

标签: android android-layout imageview png


【解决方案1】:

我只在 Android 4.1.2 (Jelly Bean) 设备上遇到过这个问题。图标半透明呈现的问题与 4.1.2 中的可绘制对象缓存有关。就我而言,我也在可绘制选择器中使用了错误的 ic_classic_current_warning_white-drawable:

<selector xmlns:android="schemas.android.com/apk/res/android"; android:exitFadeDuration="@android:integer/config_mediumAnimTime"> 
    <item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/ic_classic_current_warning_white"/> 
     <item android:state_pressed="true" android:drawable="@drawable/ic_classic_current_warning_white"/> 
    <item android:drawable="@drawable/ic_classic_current_warning_black"/> 
</selector>

此选择器在上一个活动中的按钮上使用过。如果我触摸了这个按钮,就会出现上面的视图。通过使用android:exitFadeDuration ic_classic_current_warning_white-drawable 在触摸我的按钮后淡出。但似乎当新活动打开时,可绘制缓存包含对 ic_classic_current_warning_white-drawable 的引用,其 alpha 值低于 100%。如果我在下一个视图中再次使用此可绘制对象,它不会以 100% 的 alpha 显示,而是在离开前一个活动时以 alpha 值显示。

我当前的解决方案是简单地复制可绘制的 ic_classic_current_warning_white。一个将用于选择器。另一个副本将用于“静态”目的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多