简体   繁体   English

如何更改Android中的应用程序中使用的图像的颜色和阴影

[英]How to change colors and shades of images used in the app in android

In my Android App, I am using one image in background and also using some images of arrow. 在我的Android应用程序中,我在背景中使用一个图像,并使用一些箭头图像。 I have added the image in xml like below: 我在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>

Now I want to change the image colors from the Java Code with some provided color codes. 现在我想用一些提供的颜色代码更改Java代码中的图像颜色。 The image background will recieve one color and the texture/shades/actual image portion will receive another color. 图像背景将接收一种颜色,纹理/阴影/实际图像部分将接收另一种颜色。 I heard something about getting the alpha of the image and apply colors on the image checking where is white and where is gray. 我听到了一些关于获取图像的alpha并在图像上应用颜色的检查,检查哪里是白色,哪里是灰色。 Not sure how to do that. 不知道该怎么做。

For example, I am including an image of the arrow. 例如,我包括箭头的图像。 I want to change the background from white to different colors and also the arrow from to another color. 我想将背景从白色更改为不同颜色,还要将箭头从更改为另一种颜色。 Please suggest. 请建议。

在此输入图像描述

Also, is there any way to change the colors if the image is in the alpha mode where the background is transparent? 此外,如果图像处于背景透明的alpha模式,有没有办法更改颜色?

If you make the image background transparent (the white part), so you have only what you call the "image portion" visible, than you can change the background by either setBackground(Drawable) , setBackgroundColor(color) or setBackgroundResource(resource) (since the background will be visible through the transparent parts of the image). 如果你使图像背景透明(白色部分),那么只有你所谓的“图像部分”可见,你可以通过setBackground(Drawable)setBackgroundColor(color)setBackgroundResource(resource)来改变背景(因为背景将通过图像的透明部分可见)。

To change the actual image color, use setColorFilter() , there are three different versions of this method, so you should be able to find one that suits your needs. 要更改实际图像颜色,请使用setColorFilter() ,此方法有三种不同版本,因此您应该能够找到适合您需要的版本。


To change a single image used as a background , make the base image red and green (or some other different colors that don't use several color channels. Black and white would not work) and get it as a Drawable . 要更改用作背景单个图像 ,请将基本图像设置为红色和绿色(或其他一些不使用多个颜色通道的颜色。黑色和白色不起作用)并将其作为Drawable This is since the Drawable can have a ColorFilter , just like the ImageView . 这是因为Drawable可以有一个ColorFilter ,就像ImageView Give it a ColorMatrixColorFilter . 给它一个ColorMatrixColorFilter

If the background part is red , and the foreground green , you can then use a matrix such as 如果背景部分为红色 ,前景为绿色 ,则可以使用如下的matrix

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

where 哪里

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM