【问题标题】:How to change colors and shades of images used in the app in android如何在android中更改应用程序中使用的图像的颜色和阴影
【发布时间】:2013-12-16 19:51:22
【问题描述】:

在我的 Android 应用程序中,我在背景中使用了一张图片,还使用了一些箭头图片。我在 xml 中添加了图像,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundimage" >

    <LinearLayout
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.20"
        android:background="@drawable/border"
        android:gravity="center_horizontal"
        android:weightSum="1" >

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/none"
            android:gravity="right"
            android:src="@drawable/navigation" />
    </LinearLayout>

现在我想用一些提供的颜色代码更改 Java 代码中的图像颜色。图像背景将接收一种颜色,纹理/阴影/实际图像部分将接收另一种颜色。我听说了一些关于获取图像的 alpha 并在图像上应用颜色检查哪里是白色和哪里是灰色的。不知道该怎么做。

例如,我添加了箭头的图像。我想将背景从白色更改为不同的颜色,并将箭头更改为另一种颜色。请提出建议。

另外,如果图像处于背景透明的 alpha 模式,是否有任何方法可以更改颜色?

【问题讨论】:

    标签: android imageview alpha


    【解决方案1】:

    如果您使图像背景透明(白色部分),那么您只有所谓的“图像部分”可见,那么您可以通过setBackground(Drawable)setBackgroundColor(color)setBackgroundResource(resource) 更改背景(因为背景将通过图像的透明部分可见)。

    要更改实际的图像颜色,请使用setColorFilter(),此方法有三种不同的版本,因此您应该可以找到适合您需要的一种。


    要更改用作背景的单个图像,请将基本图像设置为红色和绿色(或其他一些不使用多个颜色通道的不同颜色。黑色和白色不起作用)和将其作为Drawable 获取。这是因为 Drawable 可以有一个ColorFilter,就像ImageView 一样。给它一个ColorMatrixColorFilter

    如果背景部分是red,而前景是green,那么你可以使用matrix比如

    [bR, aR, 0, 0,
     0, bG, aG, 0, 
     0, 0, bB, aB,
     0, 0, 0, bA,
     aA, 0, 0, 0]
    

    在哪里

    background R,G,B,A = bR, bG, bB, bA
    foreground R,G,B,A = aR, aG, aB, aA
    

    【讨论】:

    • 谢谢!它适用于 imageview 中的图像。这是一个很大的帮助!当我使用图像作为 RelativeLayout 或 TextView 的背景时,您能提出什么建议吗?
    • @MSI 我也为此添加了一个示例。
    • 谢谢!我不确定如何在这里应用矩阵。我将从后端收到的颜色代码并将我收到的任何颜色代码应用到背景图像中。背景图像不是黑白的,并且已经设置为背景资源。我是这样的,但还不是很成功: LinearLayout MyView = (LinearLayout) findViewById(R.id.main); MyView.setBackgroundResource(R.drawable.bgpetals);可绘制博士 = MyView.getBackground();博士.setAlpha(0);
    • 例如;如果您获得颜色代码背景颜色bg = #FF450067 和前景色fg = #FF0065AF,将其插入矩阵:float[] matrix = [Color.Red(bg), Color.Red(fg), 0, 0, 0, Color.Green(bg), Color.Green(fg), 0, 0, 0, Color.Blue(bg), Color.Blue(fg), 0, 0, 0, Color.Alpha(bg), Color.Alpha(fg), 0, 0, 0]。然后用这个矩阵创建一个ColorMatrix实例,并应用到ColorMatrixColorFilter,并使用drawablessetColorFilter()方法应用ColorMatrixColorFilter。
    • 我可能误会了你,我把你的问题解释为好像你有一个有两种颜色的单一图像(例如箭头图中的黑色和白色),并且想要能够独立地将这些颜色更改为其他颜色。
    猜你喜欢
    • 2012-05-03
    • 2017-07-09
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2019-11-02
    • 2019-06-30
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多