10

私はこれに頭を悩ませる運があまりないので、誰かが私を助けてくれることを願っています.

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 はデフォルトのキャンバスを上書きします (これが私が望むものです)。

4

2 に答える 2

8

私はそれを考え出した。または、少なくとも機能する方法を見つけました。答えは、私が気に入らなかったそのスケーリングされたビットマップ関数の顔をじっと見つめていたことでした。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)を使用してビューに配置することで、任意のビューにサイズを変更できます。

于 2013-01-06T07:05:33.580 に答える