首页 > 解决方案 > 如何根据背景颜色更改浮动绘图的色调?

问题描述

我有一个 Android 应用程序,用户可以在其中(取消)选择图像。图像显示在 ViewPager 中,嵌套在 FrameLayout 中。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    .. ViewPager stuff ..
    <CheckBox
        android:id="@+id/selectImageCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="32dp"
        android:alpha="0.9"
        android:button="@drawable/ic_checkbox_36dp"
        android:textColor="@color/colorLightGray" />
</FrameLayout>

复选框位于右下角。我的问题是我想根据复选框后面的背景更改 Checkbox drawable 的色调。如果是深色图像,则白色色调效果很好。如果图像非常明亮,或者可能根本不够宽,无法到达复选框,那么白色就不起作用,灰色会更好。

我正在努力寻找一种方法来确定放置复选框的矩形中的可见颜色。(我知道如何以编程方式更改色调)。或者有没有更优雅的解决方案来根据周围的颜色改变可绘制的色调?

标签: androidrgbandroid-vectordrawable

解决方案


首先检查图像的尺寸并确定它是否到达复选框。

如果不是,那么灰色。

如果是这样,则可以拍摄图像下半部分的大部分并确定其整体像素亮度。

使用该值,可以确定哪种颜色最能在图像中脱颖而出。


推荐阅读