Canvas is not Showing Image/Drawable

て烟熏妆下的殇ゞ 提交于 2019-12-13 12:46:37

问题


I am doing a work on SVG Paths and Image. I have loaded SVG file and get an image and try to set thi image on canvas . But canvas is not showing image. I check the height and width and null check of this image/picture and it is not null so i am unable to understand that why canvas is not showing image. any help

My code:

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

        }

    }
}

回答1:


This is what I use to return a drawable object that I will use through all my app
It works quite well.

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



回答2:


There is a known issue with svg-android that with certain images, the hardware acceleration can actually cause the image to not show up. I'm not sure of the exact circumstances of what causes this, but I do know it happened to me, as I documented in this question. The key is to disable hardware acceleration for the view. Depending on what your target it, you might not need to do something quite as fancy as I did (Android 3.0+ doesn't need to check the build version), but here's the key part. Add the code I included in the constructor to your constructor, then add the disableHardwareAcceleration bit

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


来源:https://stackoverflow.com/questions/20400697/canvas-is-not-showing-image-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!