Android Studio 中的 SVG 图像质量较差
SVG image with poor quality in Android Studio
我对 SVG
图片还很陌生。我正在尝试在 Android Studio 项目中使用 SVG 图像。
我在项目中的 raw
目录中有一个在 Illustrator 中创建的 SVG 图像。
我还集成了一个 jar fie:svg-android.jar...
在我的主 activity 中,我有这个简陋的代码:
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,图像质量也很差。这是最终输出的样子:
提前致谢。
更新:
我将 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>
这是最终输出:
它看起来不错,正是我想要的,但我认为我应该能够在 Java 代码中使用带有 SVG 文件的图像获得相同的结果。
再次感谢。
我不确定这是否与您的问题有任何联系,但您的代码有一点有点奇怪。这是Rotwang第一条评论的第3点。
首先,您要将布局设置为 Activity 的内容。
setContentView(R.layout.activity_main);
然后您将其替换为 ImageView
setContentView(imageView);
通常您的布局中会有一个 ImageView。然后,您将使用其 id
获取 ImageView 并将可绘制对象设置到其中。
@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());
}
我对 SVG
图片还很陌生。我正在尝试在 Android Studio 项目中使用 SVG 图像。
我在项目中的 raw
目录中有一个在 Illustrator 中创建的 SVG 图像。
我还集成了一个 jar fie:svg-android.jar...
在我的主 activity 中,我有这个简陋的代码:
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,图像质量也很差。这是最终输出的样子:
提前致谢。
更新: 我将 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>
这是最终输出:
它看起来不错,正是我想要的,但我认为我应该能够在 Java 代码中使用带有 SVG 文件的图像获得相同的结果。
再次感谢。
我不确定这是否与您的问题有任何联系,但您的代码有一点有点奇怪。这是Rotwang第一条评论的第3点。
首先,您要将布局设置为 Activity 的内容。
setContentView(R.layout.activity_main);
然后您将其替换为 ImageView
setContentView(imageView);
通常您的布局中会有一个 ImageView。然后,您将使用其 id
获取 ImageView 并将可绘制对象设置到其中。
@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());
}