繁体   English   中英

画布未显示图像/可绘制

[英]Canvas is not Showing Image/Drawable

我正在研究SVG Paths和Image。 我已加载SVG文件并获取图像并尝试在画布上设置该图像。 但画布没有显示图像。 我检查这个图像/图片的高度和宽度以及空检查,它不是空的,所以我无法理解为什么画布没有显示图像。 任何帮助

我的代码:

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);
            }

        }

    }
}

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);
}

这是我用来返回一个drawable对象,我将通过我的所有应用程序使用它
它运作得很好。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM