之前写的一个Android程序,一直有个bug悬而未决:背景色原来为白色,可保存图片时却变成黑色。昨天又拿出来看了看,突然想到新建Bitmap对象时,默认变量应该和Java中其他新建变量或对象的情况类似,默认值为0。因此猜想新建一个Bitmap时,每个像素的值都是0,即黑色。于是创建一个每个像素点都是255(白色)的Bitmap就行了。
部分代码如下。
得到位图的方法:
1 /**
2 * 得到相应背景色的位图
3 * @paramwidth 位图的宽度
4 * @paramheight 位图的高度
5 * @paramcolor 位图的背景色
6 * @return该颜色的位图
7 */
8 public Bitmap getBitmapByColor(int width,int height,intcolor){
9 Bitmap newBitmap;
10 int[] colors=new int[width*height];//新建像素点数组,数组元素个数是位图的宽乘以高