我想用这段代码在 Canvas 上绘制一个可绘制对象,但它不起作用,我也不知道为什么
getResources().getDrawable(R.drawable.allergist).draw(canvas);
我将自定义 View 的高度和宽度设置为与父级匹配,但整个屏幕都是白色的,屏幕上没有可绘制对象
最佳答案
You need to load your
image
asbitmap
:
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.allergist);
Then make the
bitmap mutable
and create a canvas over it:
Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));
After that you can draw it on canvas.
编辑 1
Set bounds To
Drawable
.
Drawable d = getResources().getDrawable(R.drawable.allergist);
d.setBounds(left, top, right, bottom);
d.draw(canvas);
关于java - 在 Canvas 中绘制可绘制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429369/