Faf*_*hrd 10 android android-canvas svg-android
我没有太多的运气包围我的头,所以我希望有人可以帮助我.
我使用svg-android从SVG获得了一个drawable,但是drawable没有缩放到视图.我能够找到的所有内容都说我应该直接绘制到画布并重新缩放画布,但是当我尝试它时,它似乎只是改变边界而不是缩放图像.
这是我到目前为止所尝试的:
ImageView testview = (ImageView)findViewById(R.id.testview);
//Get SVG and convert to drawable
SVG vector = SVGParser.getSVGFromResource(getResources(),R.drawable.testvector);
Drawable test = vector.createPictureDrawable();
testview.setBackground(test);  //displays fine, but won't scale to the dimensions of
                               //the View
//function that clips the image but doesn't scale:
Drawable testTwo = new CustomPictureDrawable(vector.getPicture(), 
(float)0.5, (float)0.5);
testView.setBackground(testTwo);
class CustomPictureDrawable extends PictureDrawable {
   private float scalex, scaley;
public CustomPictureDrawable(Picture picture, float scalex, float scaley) {
    super(picture);
    this.scalex = scalex;
    this.scaley = scaley;
}
@Override
public void draw(Canvas canvas) {
    Matrix original = canvas.getMatrix();
    canvas.scale(scalex, scaley);
    super.draw(canvas);
    canvas.setMatrix(original);
}
}
//doesn't display anything
Picture testThree = vector.getPicture();
Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
c.drawPicture(testThree, new Rect(0,0,10,10));
testview.draw(c);
我还发现了一个可以创建缩放位图的函数,但图像质量显着降低,所以我也可以使用缩放的PNG.
显然我错过了一些东西,而我缺乏经验让它变得非常令人沮丧.
什么我想能够做的就是拉一个图片或PictureDrawable出来之前,有SVG-机器人完全重新缩放SVG,但我无法弄清楚如何通过SVGParser步骤,并在每个正在运行的乘数无论如何,坐标对可能是超级资源密集型的.
[edit]是缩放和重新绘制图片并将其分配给视图以创建自定义视图和覆盖OnDraw的唯一方法吗?
即
Picture testThree = vector.getPicture();
Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
c.drawPicture(testThree, new Rect(0,0,10,10));
//CustomView extends ImageView or Button or whatever with OnDraw overridden and no
//other changes 
CustomView testview = (CustomView)findViewById(R.id.testview); 
testview.OnDraw(c);
我是在正确的轨道上吗?Canvas c会覆盖默认画布(这就是我想要的),不是吗?
我想到了.或者至少想出一个有效的方法.答案是在我不喜欢的缩放位图功能中盯着我.我从根本上误解了Picture类和Draw调用是如何工作的.
代码似乎已经取消了:
//Get a Picture from the SVG
SVG vector = SVGParser.getSVGfromResource(getResources(), R.raw.testvector);
Picture test = vector.getPicture();
//Redraw the picture to a new size
Bitmap bitmap = Bitmap.createBitmap(desired width, desired height, config);
Canvas canvas = new Canvas(bitmap);
Picture resizePicture = new Picture();
canvas = resizePicture.beginRecording(desiredWidth, desiredGeight);
canvas.drawPicture(test, new Rect(0,0,desiredWidth, desiredHeight);
resizePicture.endRecording();
//get a drawable from resizePicture
Drawable vectorDrawing = new PictureDrawable(resizePicture);
我可以通过getWidth()和getHeight()调用获取desiredWidth和desiredHeight以及最后将setBackground(vectorDrawing)放在View上,将其调整为我想要的任何视图.
我遇到了类似的问题,这就是我学到的以及如何解决它.首先,我尝试将svg直接渲染到画布中
svg.renderToCanvas(canv);
它没有我想要的尺寸.然后我尝试使用调整图像大小
svg.renderToCanvas(canv,new RectF(0,0,200,200));
和
svg.setDocumentWidth(200);
svg.renderToCanvas(canv);
但两者都没有用,所以我开始看看我的SVG.问题是我的SVG是这样开始的
<svg width="66.3679625" height="100.4148375" xmlns="http://www.w3.org/2000/svg">
一旦在svg中设置了宽度和高度,似乎不可能在之后的代码中更改它.在AndroidSvgFAQ中还有一个答案,他们建议删除这些属性.所以我改成了
 <svg version="1.1" viewBox="0 0 1280 696" xmlns="http://www.w3.org/2000/svg">
viewBox属性是我的SVG图片的框架,所以如果现在我使用上面的代码
svg.setDocumentWidth(200);
svg.renderToCanvas(canv);
我得到了左上角为(0,0)并且右下角为(1200,696)的盒子的内容,其大小调整为宽度为200.您可以最初保持比率来改变比率的行为.请注意,可以省略SVG中的width,heigth和ViewBox,并以编程方式设置ViewBox
svg.setDocumentViewBox(0,0,width,height);
要了解有关ViewBox属性的更多信息,请参阅W3ViewBox.
| 归档时间: | 
 | 
| 查看次数: | 8150 次 | 
| 最近记录: |