【问题标题】:Android ImageView - How to not make imageview tintedAndroid ImageView - 如何不使 imageview 着色
【发布时间】:2016-06-14 01:54:22
【问题描述】:

我有以下layout.xml

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:src="@drawable/logo"/>
        ...
    </LinearLayout>

我这样设置父级LinearLayout的背景色:

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
    layout.setBackgroundColor(Color.parseColor("#727272"));
    layout.setAlpha(0.5f);

但是,ImageView (id:image) 是有色的。有没有办法在设置父布局的背景颜色时不给ImageView 着色?

【问题讨论】:

  • 什么是 eventLogoLayout?
  • 为什么要设置 eventLogoLayout.setAlpha(0.5f)?它的着色可能取决于您的徽标。您必须在此处附上您的屏幕截图。
  • 设置 ImageView 背景 = null。可能对你有帮助
  • @Mark Shen,是布局。已更新。
  • @Konstantin,我不明白你设置 ImageView 背景 = null 的意思,因为我必须为 imageView 设置图像。我尝试在imageView中设置android:background="@color/transparent",但是不起作用。

标签: android android-layout android-imageview


【解决方案1】:

事实证明,图像被着色是因为我为父布局调用了 setAlpha:

layout.setAlpha(0.5f);

但是,我需要为父布局设置 50% 的不透明度。所以我使用下面的方法为父布局的背景颜色设置Alpha和RGB,它的工作原理是:父布局的背景颜色设置为不给子图像着色。

LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
layout.setBackgroundColor(BitmapDrawableUtility. getColorByOpacityPercentage(calculateOpacity(Color.parseColor("#727272"), 50));


public static int getColorByOpacityPercentage(int color, double percentage) {
    int transparency = (int) Math.round((percentage / 100.0) * 255.0);
    return Color.argb(transparency, Color.red(color), Color.green(color), Color.blue(color));
}

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多