首页 > 解决方案 > 如何在android中更改svg图像的颜色。图片是从 web api 下载的

问题描述

我有一个问题,我正在从 web api 下载图像并成功设置到 imageview 中。我无法更改我下载的 svg 图像的颜色。我尝试了 xml 中的 tintColor 和 .java 文件中的 setFilterColor() 但对我来说没有任何效果。有什么解决办法请回复本帖。

public static void loadSvgWithColor(final Context context, String url, final int color, final ImageView target) {
    if (httpClient == null) {
        httpClient = new OkHttpClient.Builder()
                .cache(new Cache(context.getCacheDir(), 5 * 1024 * 1014))
                .build();
    }
    if (!url.equals("")) {
        Request request = new Request.Builder().url(url).build();
        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                target.setImageResource(R.drawable.app_icon);
                target.setColorFilter(color);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream stream = response.body().byteStream();
                Sharp.loadInputStream(stream).into(target);
                stream.close();
                target.setColorFilter(color);
       }
        });
    } else {
        target.setImageResource(R.drawable.app_icon);
        target.setColorFilter(color);
    }
}

标签: javaandroidsvg

解决方案


收到 api 时编辑 svg 文件流,并设置到 imageview 通过此示例: 没有编辑颜色

编辑颜色


推荐阅读