大家都知道用Android的API画图比JAVA麻烦得多,其实也不是太多,稍微抱怨一下下。
下面先把Android画图的逻辑捋一下:首先,先考虑图片要画在哪,一般是imageView这控件。imageView的作用呢是用来装一张图片的,因此需要用到位图Bitmap这个类。然后
考虑用什么工具对Bitmap进行操作(也就是在上面绘画)?一般会用到canvas和paint这两个类。canvas是画布,提供的作图的方法,譬如画一个圆canvas.drawOval()还是画一个
矩形canvas.drawRect()。当然光有方法不行还要有画笔paint来把这个圆画出来,因此用canvas画图的时候还要在最后加入paint这个参数。
当然,上面说的都是废话,今天主要是讨论创建canvas类时,new canvas(Bitmap bmp),bmp是否可以随便给?先看看canvas的实现:
public Canvas(Bitmap bitmap) {
if (!bitmap.isMutable()) {
throw new IllegalStateException("Immutable bitmap passed to Canvas constructor");
}
throwIfRecycled(bitmap);
mNativeCanvas = initRaster(bitmap.ni());
mFinalizer = new CanvasFinalizer(mNativeCanvas);
mBitmap = bitmap;
mDensity = bitmap.mDensity;
} 倒数面5行由于看不懂就不管了,不过我从前面这个if判断就知道Bitmap的Mutable若为false,即不可修改的话就会报错,这说明画布不能在immutable的位图上画图。
而通过直接加载资源得到的位图一般都是immutable的,即Bitmap.decodeResource()。所以不能直接在刚加载得到的位图上作图,必须先将该位图画在另一位图上,然后才开始
作图。就举一个例子:
/**
* Returns a mutable bitmap with the specified width and height. Its
* initial density is as per {@link #getDensity}.
*
* @param width The width of the bitmap
* @param height The height of the bitmap
* @param config The bitmap config to create.
* @throws IllegalArgumentException if the width or height are <= 0
*/
public static Bitmap createBitmap(int width, int height, Config config) {
return createBitmap(width, height, config, true);
}看注释就知道这个调用会返回一个mutable的Bitmap,即canvas可以在上面作图。
本文详细介绍了在Android中使用API进行绘图的过程,并解释了如何创建可编辑的Bitmap以供Canvas类绘制。文中还强调了不可直接在immutable的Bitmap上绘图,并提供了创建mutable Bitmap的方法。
1949

被折叠的 条评论
为什么被折叠?



