我没有太多的运气来解决这个问题,所以我希望有人可以帮助我。
我正在使用 svg-android 从 SVG 获取可绘制对象,但可绘制对象没有缩放到视图。我能够找到的所有内容都表明我应该直接绘制到画布并重新缩放画布,但是当我尝试时它似乎只改变了边界而不是缩放图像。
这是我迄今为止尝试过的:
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,并在每个无论如何,坐标对可能是超级资源密集型的。
[编辑] 是缩放和重新绘制图片并将其分配给视图以创建自定义视图并覆盖 OnDraw 的唯一方法吗?
IE
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 会覆盖默认画布(这是我想要的),不是吗?