1

私は SVG パスと画像に関する作業を行っています。SVG ファイルを読み込んで画像を取得し、この画像を canvas に設定しようとしました。しかし、キャンバスには画像が表示されていません。この画像/画像の高さと幅とnullチェックを確認しましたが、nullではないため、キャンバスに画像が表示されない理由を理解できません。どんな助けでも

私のコード:

public class MainActivity extends Activity{

    Context c;


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);




        c=getApplicationContext();
        setContentView(new GameView(this));
    }


    public class GameView extends View{
        private int width, height;

        private long svgId;

        Picture picture;

        long startTime;
        float scaleFactor;

        public GameView(Context context) {
            super(context);



            SVG svg = SVGParser.getSVGFromResource(getResources(),R.raw.android);
            picture = svg.getPicture();




        }



        @Override

        protected void onLayout (boolean changed, int left, int top, int right, int bottom) {

            // get visible area

            width = right - left;

            height = bottom - top;

        }



        @Override

        public void onDraw(Canvas canvas) {

            // paint a white background...

            canvas.drawColor(Color.BLACK);

            if (canvas!=null)
            {
                Toast.makeText(c, "yahooooooooooooooooo"+picture.getHeight(), Toast.LENGTH_LONG).show();

                scaleFactor=Math.min((float)getHeight()/picture.getHeight(),(float)getWidth()/picture.getWidth());
                canvas.scale((float)scaleFactor,(float)scaleFactor);
                canvas.drawPicture(picture);
            }

        }

    }
}
4

2 に答える 2

3

svg-android には既知の問題があり、特定の画像では、ハードウェア アクセラレーションによって実際に画像が表示されないことがあります。これを引き起こした正確な状況はわかりませんが、この質問で文書化したように、それが私に起こったことは知っています。重要なのは、ビューのハードウェア アクセラレーションを無効にすることです。ターゲットによっては、私が行ったほど凝ったことをする必要はないかもしれません (Android 3.0 以降ではビルド バージョンを確認する必要はありません) が、重要な部分は次のとおりです。コンストラクターに含めたコードをコンストラクターに追加し、disableHardwareAccelerationビットを追加します

public GameView(Context context,AttributeSet attrs) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        disableHardwareAcceleration();
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void disableHardwareAcceleration()
{
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
于 2013-12-05T13:40:45.000 に答える
1

これは、すべてのアプリで使用するドローアブル オブジェクトを返すために使用するもので、
非常にうまく機能します。

final BitmapDrawable getSVG_Drawable
    (int svgResourceID, int width, int height)
{
    // Get a Picture from the SVG in res/raw.
    final SVG vector =
        SVGParser.getSVGFromResource(getResources(), svgResourceID);

    final DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    // Redraw the picture to a new size.
    final Bitmap bmp =
        Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas cnv = new Canvas(bmp);
    cnv.setDensity((int) (metrics.xdpi));

    cnv.drawPicture(vector.getPicture(), new Rect(0, 0, width, height));
    final BitmapDrawable drw = new BitmapDrawable(getResources(), bmp);

    // Return the drawable.
    return drw;
}
于 2013-12-05T13:28:18.287 に答える