私はこれに頭を悩ませる運があまりないので、誰かが私を助けてくれることを願っています.
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 を使用することもできます。
明らかに私は何かを見逃しており、私の経験不足が本当にイライラさせています。
私ができるようにしたいのは、PictureまたはPictureDrawableを引き出す前にsvg-androidにSVGを完全に再スケーリングさせることですが、SVGParserをステップスルーする方法がわかりません。とにかく、座標ペアはおそらく非常にリソース集約的です。
[編集] Picture をスケーリングして再描画し、それをビューに割り当ててカスタム ビューを作成し、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 はデフォルトのキャンバスを上書きします (これが私が望むものです)。