形状绘制的chagnge颜色编程 [英] chagnge color of shape drawable programatically

查看:188
本文介绍了形状绘制的chagnge颜色编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要恰克形绘色编程...

i need to chage color of shape drawable programatically...

我用这

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/app_theme_blue" />
    <corners android:radius="8dp" />
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

在这里,我需要改变的颜色机器人:颜色=@色/ app_theme_blue
请帮助我。

Here i required to change color of android:color="@color/app_theme_blue" . please help me.

推荐答案

是的,可以。而且它真的超级容易。

Yes, you can. And it's really super easy.

首先,请记住,它更容易与黑白图像工作,为了有一个中立的源应用颜色(因此,想象有一个透明背景的白色轮廓)。

First of all, keep in mind that it's easier to work with white images, in order to have a neutral source to apply the colour to (so, imagine to have a white contour on a transparent background).

Drawable drw = mContext.getResources().getDrawable(R.id.baseDrawable);

这是什么行代码仅仅让从资源的绘制对象(baseDrawable - 命名为任何你喜欢)

What this line does is simply getting the Drawable from the resources (baseDrawable - name it whatever you like).

接下来,我们将要使用的accetpts两个参数setColorFilter()方法的重载:我们想要的颜色和混合模式。

Next, we're going to use an overload of the setColorFilter() method which accetpts two parameters: the color we want and the blending mode.

请注意,该绘制对象是默认unmutable,所以我们必须让它可变,以适用任何转换到它。

Note that the Drawable is unmutable by default, so we have to make it mutable, in order to apply any transformations to it.

drw.mutate().setColorFilter(finalColor, PorterDuff.Mode.MULTIPLY);

现在您可以专注于您的软件,而不需要做出同样的资源的许多变化。

Now you can concentrate on your software, without the need to make many variations of the same resource.

顺便说一句,有没有API级别的要求,它的工作原理,因为API等级1。

By the way, there's no API Level requirement, it works since API Level 1.

P.S:

这是必需的进口:

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;

这篇关于形状绘制的chagnge颜色编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆