Android开发中经常需要创建图片,而且从表现上来说,图片信息往往比文字信息更直观,更容易被人理解。所以创建图片的方法还是需要掌握的比较透彻,不应该像刚学Android开发一样——从别的地方复制过来再修改。
虽然我就是刚入门
目前接触到的都是通过Bitmap生成图片文件,所以创建Bitmap就成为了工作的第一布。Bitmap的创建往往是通过createBitmap方法来实现。createBitmap方法有多种,根据传入参数来选择所需。其中有:
//Returns an immutable bitmap from the specified subset of the source bitmap.
createBitmap(Bitmap source, int x, int y, int width, int height)
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
createBitmap(int[] colors, int width, int height, Bitmap.Config config)
//Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.
createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
//Returns a mutable bitmap with the specified width and height.
createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha,ColorSpace colorSpace)
//Returns an immutable bitmap from the source bitmap.
createBitmap(Bitmap src)
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
createBitmap(DisplayMetrics display, int[] colors, int offset, int stride, int width, int height,Bitmap.Config config)
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
createBitmap(DisplayMetrics display, int[] colors, int width, int height, Bitmap.Config config)
//Returns a mutable bitmap with the specified width and height.
createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
//Returns a mutable bitmap with the specified width and height.
createBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha,ColorSpace colorSpace)
//Returns a mutable bitmap with the specified width and height.
createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha)
//Returns a mutable bitmap with the specified width and height.
createBitmap(int width, int height, Bitmap.Config config)
//Returns a mutable bitmap with the specified width and height.
createBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha)
//Creates a new bitmap, scaled from an existing bitmap, when possible.
createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
在获得到Bitmap后创建Canvas,然后对Canvas进行操作,例如设置背景色,填充其它Bitmap等。用创建或传入的File创建OutputStream,再调用bitmap.compress();将Bitmap写入(压缩)文件,格式JPEG,压缩率20%,最后关闭OutputStream。代码如下:
File photo = new File("文件夹", "文件名");
Canvas canvas = new Canvas(bitmap);
OutputStream stream = new FileOutputStream(photo);
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);
stream.close();
对canvas进行操作完后,不是使用canvas进行什么写入文件等,而是直接通过创建canvas的bitmap。个人猜测是因为canvas与创建它的bitmap具有一一对应的连接关系,对canvas的操作使bitmap具有了相应的特性,所以直接使用bitmap来输出。
图片生成之后,若需要要在图库中显示,则最好使用广播通知媒体库的扫描,其代码如下,常传入File,封装为一个方法使用:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(photo);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
以上就是创建图片的简单流程,复杂图片的创建大多也是在此基础上进行扩展。新技术很多,很诱人,但不打好基础,好高骛远好像不太好吧。小程序猿一枚,希望能和大家交流探讨,一起进步。