我对这个问题感到很困惑,希望有人能帮助我。
我使用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吗?
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 会覆盖默认画布(这正是我想要的),对吗?