【问题标题】:How can I change the color of my text based on the color of the ImageView it overlays?如何根据它覆盖的 ImageView 的颜色更改文本的颜色?
【发布时间】:2016-07-06 04:05:21
【问题描述】:

所以我在上传ImageView 的用户上设置了带有白色文本标签的透明按钮。如果用户上传的图片大部分是白色的,那么按钮如果不是完全不可见就很难看到。

有谁知道获得ImageView 的源图片/drawable 的平均颜色的方法吗?如果我能做到这一点,我可以将它与我可以反复试验的某个阈值进行比较......如果我能得到这个,那么我可以将按钮上的文本颜色更改为这种颜色的反转版本.. .还是什么??

只是想在这里吐口水..

当然,如果有人知道更好的方法,将不胜感激更多信息,谢谢!!

【问题讨论】:

  • ...更好的方法... - 您可以使用透明渐变背景来保护按钮免受图像影响,就像材料设计指南 @987654322 中所解释的那样@(文本保护)。正如其中一个答案提到的那样,您可以使用 Palette 库提取颜色,但您仍然无法获得 100% 的保护,因为您最终可能会在按钮下方有一块白色背景的图像中结束
  • @Luksprog 是的,我想过,但我认为这可能是临时修复。添加渐变叠加是个好主意,我会考虑这样做。谢谢!

标签: android android-layout user-interface imageview color-detection


【解决方案1】:

您可以使用Palette 类。

来自developers guide

您可以使用 getter 从图像中检索突出的颜色 Palette 类中的方法,如Palette.getVibrantColor()

Palette.from() 需要 Bitmap 参数,因此您必须从您的 ImageView 获取它。

这样的事情应该可以工作:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();

然后你可以在这个Palette实例上调用适当的方法来获得突出的颜色,例如:

int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);

查看此示例屏幕截图,了解 Palette 类如何识别颜色:

【讨论】:

  • 非常好,谢谢!我看看明天能不能拿到这个。我会回来找你的
【解决方案2】:

您可以使用 Palette 在图像视图中获取位图的颜色

    bitmap = BitmapFactory
                    .decodeResource(getResources(), R.drawable.header);

            Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                @Override
                public void onGenerated(Palette palette) {

                     //Set normal shade to textview
                    int vibrantColor = palette.getVibrantColor(R.color.primary_500);

                     //Set darkershade to textview
                    int vibrantDarkColor = palette
                            .getDarkVibrantColor(R.color.primary_700);


                }
            });

【讨论】:

  • 是的,它工作正常,您认为问题出在哪里
【解决方案3】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    <FrameLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="110dp"
            android:layout_height="110dp"
            android:layout_gravity="top|center"
            android:background="@drawable/noimage"
            android:scaleType="fitXY" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:text="Upload image"
            android:textColor="#fff"
            android:background="#34000000" />

    </FrameLayout>
</LinearLayout>

您可以将透明颜色设置为按钮视图。因此,即使图像为白色,按钮文本也是可见的。如果没问题别忘了点赞回答!!

【讨论】:

  • 我想你误会了。按钮背景是透明的,但按钮文本是可见的。问题是按钮文本是白色的,所以当我的按钮顶部的图像视图有白色背景时,你看不到按钮。我的目标是在背景颜色为白色(或发白)时将按钮的文本从白色更改为黑色或其他内容
  • 好的,那么您应该从图像中提取颜色并基于此更改文本颜色,您可以参考此示例“chris.banes.me/2014/07/04/palette-preview”@Ripyde4
猜你喜欢
  • 2023-03-18
  • 2016-06-02
  • 2012-08-05
  • 1970-01-01
  • 2022-10-18
  • 2019-11-25
  • 2018-11-12
  • 2015-12-31
相关资源
最近更新 更多