最近在做李宁的一个项目,要求特别高,所以今天记录一下自己遇到的难点,希望对初学者有用!本来想着用android系统图库的原来实现的,但是图片加密解密麻烦,图片也不让放本地。

流转换Bitmap的方法

public Bitmap doBitmapFile(InputStream in, boolean isFlag) {// isFlag是一个标志 true表示是大图  false小图Bitmap bitmap = null;//BitmapFactory.Options options = null;ByteArrayOutputStream outStream = null;final int BUFFER_SIZE = 2048;if (in == null) {return null;}try {outStream = new ByteArrayOutputStream();byte[] bytes = new byte[BUFFER_SIZE];int len = -1;while ((len = in.read(bytes)) != -1) {        outStream.write(bytes, 0, len);outStream.flush();}byte[] data = new byte[outStream.toByteArray().length];data = outStream.toByteArray();options = new BitmapFactory.Options();// 这里设置true的时候,decode时候Bitmap返回的为空, 将图片宽高读取放在Options里.options.inJustDecodeBounds = true;options.inTempStorage = new byte[16 * 1024];options.inPreferredConfig = Bitmap.Config.ARGB_8888;options.inInputShareable = true;options.inPurgeable = true;options.inDither = false;BitmapFactory.decodeByteArray(data, 0, data.length, options);if (isFlag) {// 计算浏览单张图片的缩放比例值int totalSize = 0;int width = options.outWidth;int height = options.outHeight;totalSize = width * height * 4;//计算图片内存占用大小if (totalSize < 13631488) {options.inSampleSize = 1;} else {int maxNumOfPixels = 1280 * 1280 * 2;options.inSampleSize = computeSampleSize(options, -1, maxNumOfPixels);}} else {// 计算浏览列表显示图片的缩放比例值int maxNumOfPixels = MAX_RESOLUTION_A * MAX_RESOLUTION_B * 2;options.inSampleSize = computeSampleSize(options, -1, maxNumOfPixels);options.outWidth = options.outWidth / options.inSampleSize;options.outHeight = options.outHeight / options.inSampleSize;}options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);return watermarkBitmap(recycleBitmap(bitmap), "水印文字内容", Context, options);} catch (OutOfMemoryError e) {bitmap = null;System.gc();Log.e("Eeron", e.getMessage().toString());e.printStackTrace();return null;} catch (Exception e) {bitmap = null;System.gc();Log.e("Eeron", e.getMessage().toString());return null;} finally {closeStream(in);closeStream(outStream);}}/** * @Description: 关闭流 * @param stream * @return: void */private static void closeStream(Closeable stream) {if (stream != null) {try {stream.close();} catch (IOException e) {Log.e("LiNing", "Could not close stream", e);}}}
水印添加方法:

        /** * @Description: 添加水印 * @param bitmap * @param title * @param context * @return * @return: Bitmap */public static Bitmap watermarkBitmap(Bitmap bitmap, String title, Context context, Options options) {if (bitmap == null) {return null;}Bitmap watermark = BitmapFactory.decodeResource(context.getResources(), R.drawable.add_water_mark, options);int w = bitmap.getWidth();int h = bitmap.getHeight();// 需要处理图片太大造成的内存超过的问题,这里我的图片很小所以不写相应代码了Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图Canvas cv = new Canvas(newb);cv.drawBitmap(bitmap, 0, 0, null);// 在 0,0坐标开始画入src// 加入图片if (watermark != null) {int ww = watermark.getWidth();int wh = watermark.getHeight();Rect src = new Rect();// 图片Rect dst = new Rect();// 屏幕位置及尺寸src.left = 0; // 0,0src.top = 0;src.right = w;// 是桌面图的宽度,src.bottom = h;// 是桌面图的高度// 下面的 dst 是表示 绘画这个图片的位置dst.left = 0; // 绘图的起点X位置dst.top = 0; // 相当于 桌面图片绘画起点的Y坐标dst.right = ww + w - 60; // 表示需绘画的图片的右上角dst.bottom = wh + h - 60; // 表示需绘画的图片的右下角cv.drawBitmap(watermark, src, dst, null);// 在src的右下角画入水印src = null;        dst = null;}// 加入文字if (title != null) {String familyName = "宋体";Typeface font = Typeface.create(familyName, Typeface.BOLD);TextPaint textPaint = new TextPaint();textPaint.setColor(Color.RED);textPaint.setTypeface(font);textPaint.setTextSize(22);textPaint.setAlpha(50);cv.drawText(title, 40, h - 30, textPaint);}cv.save(Canvas.ALL_SAVE_FLAG);// 保存cv.restore();// 存储watermark.recycle();bitmap.recycle();return recycleBitmap(newb);}
Bitmap内存释放方法:

        public static Bitmap recycleBitmap(Bitmap bitmap) {if (bitmap == null || bitmap.getConfig() != null) {return bitmap;}Bitmap newBitmap = bitmap.copy(Config.ARGB_8888, false);bitmap.recycle();return newBitmap;}

动态计算options.inSampleSize的方法,这个是我从android系统图库里面弄过来的

        /** * @Description: * @param options * @param minSideLength * @param maxNumOfPixels * @return 动态计算出图片的inSampleSize * @return: int */public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);int roundedSize;if (initialSize <= 8) {roundedSize = 1;while (roundedSize < initialSize) {roundedSize <<= 1;}} else {roundedSize = (initialSize + 7) / 8 * 8;}return roundedSize;}private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {double w = options.outWidth;double h = options.outHeight;// Math.ceil(Math.sqrt(w * h / maxNumOfPixels)) :w * h /// maxNumOfPixels平方结果的小数部分一律向整数部分进位int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));// Math.floor(w / minSideLength) 将w / minSideLength结果值一律舍去小数,仅保留整数int upperBound = (minSideLength == UNCONSTRAINED) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));if (upperBound < lowerBound) {return lowerBound;}if ((maxNumOfPixels == UNCONSTRAINED) && (minSideLength == UNCONSTRAINED)) {return 1;} else if (minSideLength == UNCONSTRAINED) {return lowerBound;} else {return upperBound;}}




更多相关文章

  1. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载
  2. android的带值跳转
  3. Eclipse: Android(安卓)Device Chooser - Unknown Target
  4. Android(安卓)Developers:存储选项
  5. 【Android(安卓)开发】: AsyncTask 详解
  6. android 引用工程作为类库
  7. JS调用Android、Ios原生控件
  8. Android侧边导航栏+ListView基础实践
  9. android 存储方法一SharedPreferences存储

随机推荐

  1. Android深入探究-- 实现即时拍照并上传
  2. The logbook of Android(安卓)bug in dai
  3. android复制数据库到SD卡(网上搜集,未经验
  4. Android中通过Intent 调用图片、视频、音
  5. [Android]PhoneGap源码分析——CallbackS
  6. Android(安卓)异步获取网络图片并处理图
  7. Android四大基本组件介绍与生命周期
  8. android 横屏重启的解决方案
  9. Android 强制设置横屏或竖屏 设置全屏
  10. android之ListView和SimpleAdapter的组合