gpt4 book ai didi

android - 缩放 Canvas 以在 Android 中调整 SVG 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:32 24 4
gpt4 key购买 nike

我没有太多运气来解决这个问题,所以我希望有人能帮助我。

我正在使用 svg-android 从 SVG 中获取可绘制对象,但该可绘制对象未缩放到 View 。我能找到的所有内容都说我应该直接在 Canvas 上绘制并重新缩放 Canvas ,但是当我尝试这样做时,它似乎只改变了边界而不是缩放图像。

这是我到目前为止尝试过的:

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。

显然,我遗漏了一些东西,而且我缺乏经验,这让我非常沮丧。

希望能够做的是让 svg-android 在从中拉出 Picture 或 PictureDrawable 之前完全重新缩放 SVG,但我不知道如何单步执行 SVGParser,并且在每个坐标对上运行乘法器无论如何都可能是 super 资源密集型的。

[edit] 是缩放和重新绘制图片并将其分配给 View 以创建自定义 View 并覆盖 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 会覆盖默认 Canvas (这是我想要的),不是吗?

最佳答案

我想通了。或者至少想出了一个可行的方法。答案就在我不喜欢的缩放位图函数中盯着我看。我从根本上误解了 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 上,从而将其调整为我想要的任何 View 。

关于android - 缩放 Canvas 以在 Android 中调整 SVG 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com