How can I change the color of my text based on the color of the ImageView it overlays?

偶尔善良 提交于 2021-01-21 05:04:51

问题


So I've got transparant buttons with white text labels set up over a user uploaded ImageView. If the user uploads an image that is mostly white, then the buttons are hard to see if not completely invisible.

Does anyone know of a way to get the average color of a ImageView's source picture/drawable? If I can do this, I can compare it to a certain threshold I can trial and error for... If I can get this, then I can change the color of the text on my buttons to the inverted version of this color...or something??

Just idea spitting here..

And of course, if someone knows a better way, more info would be much appreciated, thanks!!


回答1:


You could use the Palette class.

From the developers guide:

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor().

Palette.from() expects a Bitmap param, so you'll have to get it from your ImageView.

Something like this should work:

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

And then you can call the appropriate methods on this Palette instance to get the prominent colors, for example:

int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);

Check out this example screenshot on how the Palette class recognizes colors:




回答2:


You can use Palette to get color of bitmap in an imageview

    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>

You can set transparent color to button view . So button text is visible even if image is in white color. If its okay dont forget to thumb up answer!!



来源:https://stackoverflow.com/questions/38216167/how-can-i-change-the-color-of-my-text-based-on-the-color-of-the-imageview-it-ove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!