将图片设置为 ImageView Android

Set Picture to ImageView Android

我在我的代码中生成图片是这样的:

try {
    SVG svg = SVG.getFromResource(this, R.raw.splatter);

    SVGImageView svgImageView = new SVGImageView(this);
    svgImageView.setSVG(svg);
    svgImageView.setLayoutParams(
            new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));

    RelativeLayout layout =
            (RelativeLayout) findViewById(R.id.main_layout);

    layout.addView(svgImageView);

    //svg.renderViewToPicture(Integer.toString(R.id.map), layout.getWidth(), layout.getHeight());
    svg.renderToPicture();
} catch (SVGParseException e) {
    e.printStackTrace();
}

现在我需要将此图片设置到我的 ImageView。我该怎么做?

你真的需要把 svg 放在 res/raw 中吗?您可以直接从 res/drawable 引用它,就像您引用普通图像一样(即 imageView.setImageResource(R.drawable.splatter))。 This site 有一个非常好的 SVG 到 Android 可读的 SVG 工具。

Bitmap PicturetoDrawable(Picture picture) {
    return Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
}