【问题标题】:How to change color of svg image in android. Images are downloaded from web api如何在android中更改svg图像的颜色。图片是从 web api 下载的
【发布时间】:2019-10-31 06:31:29
【问题描述】:

我有一个问题,我正在从 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);
    }
}

【问题讨论】:

    标签: java android svg


    【解决方案1】:

    收到api后编辑svg文件流,设置成imageview 通过这个样本:

    【讨论】:

      【解决方案2】:

      您使用的 Sharp 库会生成一个 PictureDrawable 并将其放入您的 ImageView。最后我检查了一下,android:tintsetColorFilter() 方法在 PictureDrawables 上不起作用。

      我能想到几个解决方案:

      【讨论】:

        猜你喜欢
        • 2021-08-11
        • 2020-09-20
        • 2020-05-07
        • 2016-11-18
        • 2019-01-15
        • 2013-09-15
        • 2018-11-25
        • 2011-08-07
        • 1970-01-01
        相关资源
        最近更新 更多