Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将SVG源中的可缩放图标放到按钮上?_Android_Svg - Fatal编程技术网

Android 如何将SVG源中的可缩放图标放到按钮上?

Android 如何将SVG源中的可缩放图标放到按钮上?,android,svg,Android,Svg,我想编写如下代码: Context c = getContext(); Button b = new Button( c ); b.setBackground( new SvgDrawable( c.getAssets(), "iconImage.svg" )); int pxWidth = Math.round( 48/*sp*/ // scaling to user preference * c.getResources().getDisplayMetrics().scaledDens

我想编写如下代码:

Context c = getContext();
Button b = new Button( c );
b.setBackground( new SvgDrawable( c.getAssets(), "iconImage.svg" ));
int pxWidth = Math.round( 48/*sp*/  // scaling to user preference
  * c.getResources().getDisplayMetrics().scaledDensity/*px per sp*/ );
addView( b, new ViewGroup.LayoutParams( pxWidth, WRAP_CONTENT ));
  // determines unspecified height from pxWidth and aspect ratio
但是没有
SvgDrawable
。我可以用什么替代来将SVG放到按钮上

请给出一个适用于任何IDE的答案。我不使用Studio或Eclipse。请执行以下操作:

try {
   SVG svg = SVGParser.getSVGFromAsset(getAssets(), "iconImage.svg");
   yourButton.setBackground(svg.createPictureDrawable());
} catch (IOException e) {
   e.printStackTrace();
} 

如您所说,如果您的minSdkVersion为21或更高,那么您可以选择只使用
VectorDrawable
。只需使用其中一个在线转换器就可以将SVG转换为矢量绘图

否则,您可以使用
按钮
并根据Surabhi的建议设置背景

或者您可以使用
ImageButton
执行以下操作

// AndroidSVG
try {
   SVG svg = SVG.getSVGFromAsset(getAssets(), "iconImage.svg");
   yourButton.setImageDrawable(new PictureDrawable(svg.renderToPicture()));
}
catch (IOException e) {
   e.printStackTrace();
}
或者,如果您使用的是svg android,则可能是:

// svg-android
try {
   SVG svg = SVGParser.getSVGFromAsset(getAssets(), "iconImage.svg");
   yourButton.setImageDrawable(svg.createPictureDrawable());
}
catch (IOException e) {
   e.printStackTrace();
}

这就是我最终确定的解决方案

1.将SVG转换为可矢量绘制 矢量图像的首选形式通常是SVG,但Android上的本地首选是
VectorDrawable
。这就产生了形式问题,我选择的解决方案是转换

下面的示例源代码是从用Nashorn JavaScript编写的构建脚本中提取的

var bufferFile=tmpDir.resolve('vectorDrawableBuffer.xml');//VD输出
var inX=Build.xmlInputFactory().createXMLEventReader(
新建(Java.type('Java.io.BufferedInputStream'))(
新建(Java.type('Java.io.FileInputStream'))(sourceFile));//SVG在
var out=Files.newBufferedWriter(缓冲文件);
尝试
{
var factory=Build.xmlEventFactory();
while(inX.hasNext())
{
var xml=inX.next();
如果(!xml.isStartElement()
||xml.getName().getLocalPart()!=“svg”)继续;
var父代;
var parents=[];//SVG
var _parents=[];//可矢量绘制
函数行()
{
out.newLine();
for(var p=parents.length;p>0;--p)out.append('\t');
}
//svg→ 矢量
// - - - - - - -
out.append(
'' );
while(inX.hasNext())
{
xml=inX.next();
if(xml.isStartElement())
{
如果(xml.getName().getLocalPart()!='path')继续;
//路径
// - - -
缩进线();
out.append(“”);
}
else if(xml.isEndElement())
{
name=xml.getName().getLocalPart();
如果(名称==父项)
{
缩进线();
out.append(“”);
}
}
}
}
}
最后
{
out.close();
inX.close();
}
有关缺少的上下文,请参见

2.设置VectorDrawable on按钮
final Resources res=context.getResources();
最终矢量绘制图标;
//试一试
// {
//最终c级=
//Class.forName(“android.graphics.drawable.VectorDrawable”);
//最终方法m=c.getMethod(“创建”,Resources.class,int.class);
//icon=(VectorDrawable)m.invoke(null,res,
//R.drawable.vector_drawable_res_id);
// }
//catch(final Exception ex){throw new RuntimeException(ex);}
////调用API隐藏方法,但最好将其内联用于生产代码:
最终XmlResourceParser p=res.getXml(R.drawable.vector\u drawable\u res\u id);
尝试
{
//转到第一个元素,否则VectorDrawable将抛出XmlPullParserException
//“二进制XML文件行#-1标记要求viewportWidth>0”。
for(int t=p.getEventType();t!=START_TAG;t=p.next())
{
if(t==结束文档)
{
抛出新的XmlPullParserException(“缺少开始标记”);
}
}
icon=新的VectorDrawable();
充气(res,p,Xml.asAttributeSet(p));
}
捕获(最终IOException | XmlPullParseRexException ex)
{
抛出新的运行时异常(ex);
}
按钮。设置背景(图标);
最后,正如承诺的那样:

3.缩放
final float aspectRatio=
(float)icon.getIntrinsicWidth()/icon.getIntrinsicHeight();
final int pxWidth=Math.round(48/*sp*///根据用户偏好进行缩放
*context.getResources().getDisplayMetrics().scaledDensity/*px per sp*/);
最终整数pxHeight=数学圆(pxWidth/aspectRatio);
//图标.setBounds(/*左*/0,/*顶*/0,pxWidth,pxHeight);
///无需缩放图标本身,只需按钮:
addView(按钮,新视图组。布局参数(pxWidth,pxHeight));

这不是一个简单的代码解决方案。但我认为它将被证明是健壮的(更少的意外陷阱和其他隐藏的复杂性),因为它在两个方面都使用了首选的形式:源端的SVG,可执行端的
VectorDrawable

您在android中使用的svg库是什么?您的
minSdkVersion
?@Surabhi我还没有承诺使用库@Pravin My
minSdkVersion
是21,但我可以增加它。那么您是如何解析svg文件的?使用一些库,android有许多库用于在中使用svgandroid@Surabhi还没做,只是问怎么做。我编写了代码示例(伪代码)来说明我想要完成的任务。对于AndroidSVG,您需要使用
yourButton.setBackground(新PicturedRavable(svg.renderToPicture())