我对SVG
图像很新。我试图在Android Studio项目中使用SVG图像。
我有一个SVG图像,我在我的项目中的raw
目录中的Illustrator中创建。
我还集成了一个jar fie:svg-android.jar ...
在我的主要活动中,我有这个简陋的代码:
package meaningless.safr.com.meaningless;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;
public class MainActivity extends AppCompatActivity {
@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.WHITE);
// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.personaje);
// 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文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 17.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!--<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">-->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="100px" height="100px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve">
<polygon points="50,25.62 57.82,1.23 57.73,26.84 72.7,6.07 64.69,30.39 85.36,15.26 70.23,35.92 94.55,27.92 73.78,42.89
99.38,42.79 75,50.62 99.38,58.44 73.78,58.34 94.55,73.32 70.23,65.31 85.36,85.97 64.69,70.84 72.7,95.17 57.73,74.39 57.82,100
50,75.62 42.18,100 42.27,74.39 27.3,95.17 35.31,70.84 14.64,85.97 29.77,65.31 5.45,73.32 26.22,58.34 0.62,58.44 25,50.62
0.62,42.79 26.22,42.89 5.45,27.92 29.77,35.92 14.64,15.26 35.31,30.39 27.3,6.07 42.27,26.84 42.18,1.23 "/>
<polygon fill="#FFFFFF" points="50,34.64 55,19.05 54.94,35.42 64.51,22.14 59.39,37.69 72.6,28.02 62.93,41.22 78.47,36.11
65.2,45.68 81.56,45.62 65.98,50.62 81.56,55.61 65.2,55.55 78.47,65.12 62.93,60.01 72.6,73.21 59.39,63.54 64.51,79.09
54.94,65.81 55,82.18 50,66.59 45,82.18 45.06,65.81 35.49,79.09 40.61,63.54 27.4,73.21 37.07,60.01 21.53,65.12 34.8,55.55
18.44,55.61 34.02,50.62 18.44,45.62 34.8,45.68 21.53,36.11 37.07,41.22 27.4,28.02 40.61,37.69 35.49,22.14 45.06,35.42 45,19.05
"/>
<circle fill="#110505" cx="50" cy="50" r="10.89"/>
<circle fill="#FFFFFF" cx="50" cy="50" r="8.71"/>
<circle fill="#E6007E" cx="50" cy="50" r="3.07"/>
</svg>
代码用于其功能,以在设备上显示图像。现在,问题是,即使格式为SVG,图像质量也很差。这是最终输出的样子: image 提前谢谢。
更新 我将SVG转换为XML。现在,我在布局代码中使用它如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="meaningless.safr.com.meaningless.MainActivity">
<ImageView
android:id="@+id/imageButton"
android:src="@drawable/personaje"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
这是最终输出: enter image description here
它看起来不错,就像我想要的那样,但我认为我应该能够在使用带有SVG文件的图像的Java代码中获得相同的结果。
再次感谢。
答案 0 :(得分:0)
我不确定这是否与您的问题原因有任何关联,但您的代码有一点奇怪。这是Rotwang的第一个评论的第3点。
首先,您要将布局设置为您的活动的内容。
setContentView(R.layout.activity_main);
然后你用ImageView替换它
setContentView(imageView);
通常你的布局会有一个ImageView。然后,您将使用其id
获取ImageView,并将drawable设置为。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new ImageView
ImageView imageView = (ImageView) fiindViewById(R.id.imageButton);
// Set the background color to white
imageView.setBackgroundColor(Color.WHITE);
// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.personaje);
// Get a drawable from the parsed SVG and set it as the drawable for the ImageView
imageView.setImageDrawable(svg.createPictureDrawable());
}