简体   繁体   English

以编程方式将矢量drawable / SVG绘制到屏幕上

[英]Programmatically draw vector drawable / SVG on to screen

I'm trying to programmatically draw an SVG on to the screen. 我正在尝试以编程方式将SVG绘制到屏幕上。 The code below is what I've tried, when I run the program nothing shows up. 下面的代码是我尝试过的,当我运行程序时没有显示任何内容。

    public MainView(Context context) {
        hexagon = AppCompatResources.getDrawable(context,R.drawable.hex_svg);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        hexagon.setBounds(30,30,30,30);
        hexagon.draw(canvas);
    }

The setBounds(int left, int top, int right, int bottom) method takes the positions of the bounding box of the drawable - you've set them all to 30 so you're setting the drawable's width and height to zero. setBounds(int left, int top, int right, int bottom)方法获取drawable的边界框的位置 - 您将它们全部设置为30,因此您将drawable的宽度和高度设置为零。

If you want the image to be at 30x,30y with a width and height of 30 then call setBounds(30, 30, 60, 60) . 如果您希望图像为30x,30y,宽度和高度为30,则调用setBounds(30, 30, 60, 60)

Also I'd call this in the onLayout method rather than in onDraw . 我也会在onLayout方法而不是onDraw调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM