canvas.drawBitmap is not drawing anything

别等时光非礼了梦想. 提交于 2019-12-11 13:46:00

问题


I have overwritten onDragShadow for a DragShadowBuilder like so

@Override
    public void onDrawShadow(Canvas canvas) {
        super.onDrawShadow(canvas);
        Bitmap bitmap = InGameActivity.getRandomBitmap();
        Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, source, source, null);

    }

I have verified that the bitmap is not null but when I drag, nothing is showing. Any ideas why?


回答1:


If you want to draw the bitmap as is (i.e. you want the whole thing), use:

canvas.drawBitmap(bitmap, null, source, null);

The 2nd parameter is for where you need to specify a subset of the bitmap to be drawn (it can be null if no subset is required). I expect there is a conflict within the method if you have src and dst (2nd and 3rd parameters) set to the same object.




回答2:


Checklist:

  1. Is the bitmap valid, i.e. not all transparent or the same as the background colour? You can view it easily in the Android studio debugger.

  2. Is your source rectangle (i.e. parameter 2, not 3 as is confusingly shown above) within the bounds of the bitmap? You're allowed to set this to null and that can be a good debugging step.

  3. Is your destination rectangle within the bounds of the canvas?

  4. Do you have a 4th parameter, 'paint', which has a low alpha value? Try 'paint.setAlpha(255)' or use 'null' for the 4th parameter.

  5. Do you see this message in your console? "OpenGLRenderer: Bitmap too large to be uploaded into a texture"? If so then see: "Bitmap too large to be uploaded into a texture"



来源:https://stackoverflow.com/questions/21082184/canvas-drawbitmap-is-not-drawing-anything

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