【发布时间】: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。我建议使用这些参数保存图像并重试。