如何修改Android 9patch图像的颜色?

Bog*_*iel 6 android android-studio

我真的不明白如何创建一个9patch图像,但我找到了一个正在处理我的片段的图像.问题是边框外的颜色不是背景的颜色.我尝试将像素的颜色从图像更改为背景的颜色,但生成的图像不再起作用.这是正在工作但颜色错误的图像:

http://i.stack.imgur.com/cJBfV.png

如何更改边框外像素的颜色,或者如何创建看起来像这样的新9patch图像?

小智 8

你可以使用DrawableCompatsupprot v4.

现在,我会告诉你用这种方法改变Toast颜色.如你所知,吐司背景是一个9patch Drawable命名toast_frame.9.png(你可以在你的sdk目录中找到它)

这段代码:

    Toast toast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
    toastView.findViewById(android.R.id.message);
    View toastView = toast.getView();
    Drawable toastBg = toastView.getBackground();
    Drawable drawable = tintDrawable(toastBg, ColorStateList.valueOf(Color.RED));
    toastView.setBackground(drawable);
    toast.setView(toastView);
    toast.show();

public  Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多信息,请点击:http://www.race604.com/tint-drawable/


Bre*_*ntM 2

您可以使用 Android SDK 提供的 Draw 9-patch 工具来编辑 9 补丁。然而,由于您的图像已经包含 9 个补丁可拉伸区域,您只需在图像编辑器(例如 GIMP 或 Photoshop)中编辑颜色即可。确保重命名图像以使用.9.png扩展名,以便将其识别为 9 补丁图像。

http://developer.android.com/tools/help/draw9patch.html