【问题标题】:Import SVG images导入 SVG 图像
【发布时间】:2015-06-21 23:17:59
【问题描述】:

当我点击应用程序时,我只想查看一些 SVG 图像。我尝试过这个。导入 android studio 的步骤是正确的。但是当我运行 apk 时,我只能看到一个空白屏幕。 SVG 图像没有显示出来。那我能做什么?我该如何纠正? 我使用的代码是,

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create a new ImageView

        ImageView imageView = new ImageView(this);
        // Set the background color to white
        imageView.setBackgroundColor(Color.BLACK);
        // Parse the SVG file from the resource
        SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
        // Get a drawable from the parsed SVG and set it as the drawable for the ImageView
        imageView.setImageDrawable(svg.createPictureDrawable());
        // Set the ImageView as the content view for the Activity
        setContentView(imageView);
    }

【问题讨论】:

  • 您使用哪个库进行 SVG 解析?
  • android-svg。我将此 jar 文件包含在“libs”文件夹中。
  • ok,只是它有一段时间没有维护了,最近我成功使用了它的fork,它也没有维护,但没有那么久。您可以尝试使用这个库而不是第一个库,它们的用法非常相似。您是否遵循他们对输入 SVG 文件的要求?它们有一系列支持的格式,其余的则无法保证。
  • 谢谢你。我不知道输入 SVG 文件的要求以及它们支持的格式范围。实际上我只是按照下面链接中给出的步骤进行操作。 android-coffee.com/svg-integration-in-android-studio
  • 这是他们支持的formats。我建议使用这些参数保存图像并重试。

标签: android image svg


【解决方案1】:

您的问题可能是由以下两件事之一引起的:

  1. 硬件加速在最新的 Android 设备上默认开启,这会导致一些问题,因为 SVG 渲染可能需要 Canvas 的某些功能,而某些版本的 Android 上的硬件加速渲染器不支持这些功能。每个版本的 Android 都会变得更好,但您可能必须明确告诉它使用软件渲染器。

见:Having issue on Real Device using vector image in android. SVG-android

  1. 当您说“android-svg”时,我假设您的意思是“svg-android”? svg-android 没有维护,不支持很多 SVG 功能。这可能是您的 SVG 未呈现的原因。尝试更新的 fork,或 AndroidSVG 等其他库。

【讨论】:

  • 非常感谢。有用!这是因为打开了硬件渲染。
猜你喜欢
  • 2022-01-03
  • 2013-05-18
  • 2017-10-05
  • 2020-06-06
  • 2023-04-05
  • 2020-07-08
  • 2021-11-29
  • 1970-01-01
  • 2013-03-19
相关资源
最近更新 更多