请教一下自定义Canvas是怎样用。平时都是通过继承view,直接通过findViewById自动调用onDraw(),然后里面用Canvas搞东西,但是我现在想另外做一个类,不继承view,直接自己new canvas来画,但是发现什么都显示不了。。。
难道安卓画图只能用View吗?谢谢
------解决思路----------------------
canvas对象需要借助view、surfaceview这样的载体来绘制
------解决思路----------------------
* The Canvas class holds the "draw" calls. To draw something, you need
* 4 basic components: A Bitmap to hold the pixels, a Canvas to host
* the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
* Path, text, Bitmap), and a paint (to describe the colors and styles for the
* drawing).
直接查看Canvas的说明,可以看到Canvas绘图的四要素(一个位图,一个画布,要画的东西和画刷)
所以,其实你也可以再位图上绘制。但是想输出到屏幕,最终还是要借用View等对象
Bitmap bmp = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
Canvas canvas = new Canvas(bmp);
canvas.drawCircle(50,50,50,paint);
canvas = null;
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageBitmap(bmp);
------解决思路----------------------
Canvas是系统创建的,并传递到系统Graphics的渲染调用流程中去。
你自定义Canvas的话,系统怎么去使用你的类? 除非你修改Android的framewoks。