以前写的一个Android程序,一直有个bug悬而未决:背景色原来为白色,可保存图片时却变成黑色。昨天又拿出来看了看,忽然想到新建Bitmap对象时,默认变量应该和Java中其余新建变量或对象的状况相似,默认值为0。所以猜测新建一个Bitmap时,每一个像素的值都是0,即黑色。因而建立一个每一个像素点都是255(白色)的Bitmap就好了。canvas
部分代码以下。数组
获得位图的方法:ide
1 /** 2 * 获得相应背景色的位图 3 * @param width 位图的宽度 4 * @param height 位图的高度 5 * @param color 位图的背景色 6 * @return 该颜色的位图 7 */ 8 public Bitmap getBitmapByColor(int width,int height,int color){ 9 Bitmap newBitmap; 10 int[] colors=new int[width*height];//新建像素点数组,数组元素个数是位图的宽乘以高 11 for (int i=0;i<colors.length;i++){ 12 colors[i]=color;//将颜色赋值给每个像素点 13 } 14 newBitmap= createBitmap(colors,width,height,Bitmap.Config.ARGB_8888); 15 return newBitmap; 16 }
构造方法:测试
1 public DrawView(Context context, AttributeSet attributeSet) { 2 super(context, attributeSet); 3 int width = context.getResources().getDisplayMetrics().widthPixels;//获得屏幕的宽度 4 int height = context.getResources().getDisplayMetrics().heightPixels;//获得屏幕的高度 5 Bitmap bitmap=getBitmapByColor(width,height,Color.WHITE); 6 Canvas canvas = new Canvas(); 7 Canvas.setBitmap(bitmap); 8 }
保存图片的方法:spa
1 /** 2 * 将图片保存在内存卡的pictures文件夹内 3 * @param fileName 文件名 4 * @throws IOException 5 */ 6 public void savePic(String fileName) throws IOException { 7 File file = new File("/sdcard/pictures/" + fileName + ".png"); 8 file.createNewFile(); 9 FileOutputStream fileOS = new FileOutputStream(file); 10 cacheBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS);//注意是PNG格式的。若设置为JPG格式,背景色会变黑 11 fileOS.flush(); 12 fileOS.close(); 13 }
改为这样,运行后发现报错!进行断点调试,发现” canvas.setBitmap(bitmap);”出错了。在网上查了一下,原来Canvas对象在执行setBitmap方法时,首先判断这个位图是否是可变的,若是是不可变的,那么便不能执行setBitmap()方法。bitmap.isMutable()返回一个布尔值。若是bitmap是可变的,则返回true;反之返回false。将这条语句嵌入到代码中,logcat上显示bitmap确实是不可变的。所以得想办法将Bitmap对象变为可变才行。在网上查到,只有一种方法可行,那就是调用bitmap的copy()方法,拷贝一份给另外一个Bitmap对象,copy()有一个参数,能够设置拷贝的一份是否是可变的。不过这样原来的Bitmap对象就没什么用了。调试
所以其余方法不变,将构造方法改成:code
1 public DrawView(Context context, AttributeSet attributeSet) { 2 super(context, attributeSet); 3 int width = context.getResources().getDisplayMetrics().widthPixels;//获得屏幕的宽度 4 int height = context.getResources().getDisplayMetrics().heightPixels;//获得屏幕的高度 5 Bitmap tempBitmap=getBitmapByColor(width,height,Color.WHITE); 6 Canvas canvas = new Canvas(); 7 Bitmap bitmap=tempBitmap.copy(tempBitmap.getConfig(),true);//true表示该bitmap对象是可变的;false则反之 8 canvas.setBitmap(bitmap); 9 }
这样便运行成功了。orm
其实还有一种方法,就是先执行createBitmap()方法,建立一个Bitmap对象。而后将这个Bitmap对象的像素点所有设置为想要的颜色。经测试发现,这样不会致使bitmap变为不可变的。对象
部分代码以下。 blog
设置位图背景色的方法:
1 /** 2 * 设置位图的背景色 3 * @param bitmap 须要设置的位图 4 * @param color 背景色 5 */ 6 public void setBitmapBGColor(Bitmap bitmap,int color){ 7 for(int i=0;i<bitmap.getWidth();i++){ 8 for(int j=0;j<bitmap.getHeight();j++){ 9 bitmap.setPixel(i,j,color);//将bitmap的每一个像素点都设置成相应的颜色 10 } 11 } 12 }
构造方法:
1 public DrawView(Context context, AttributeSet attributeSet) { 2 super(context, attributeSet); 3 width = context.getResources().getDisplayMetrics().widthPixels;//获得屏幕的宽度 4 height = context.getResources().getDisplayMetrics().heightPixels;//获得屏幕的高度 5 Bitmap bitmap=createBitmap(width,height,Bitmap.Config.ARGB_8888); 6 setBitmapBGColor(bitmap,Color.WHITE); 7 Canvas canvas = new Canvas(); 8 canvas.setBitmap(bitmap); 9 }
保存图片的方法不变。以上全部的构造方法只是给出了部分代码。