Bitmap的优化
2016-04-26 21:29
260 查看
这里列出两种常用的Bitmap的优化 :
1. 给Bitmap设置圆角效果下面的代码当我们需要圆角的时候,调用这个方法,第一个参数是传入需要转化成圆角的图片,第二个参数是圆角的度数,数值越大,圆角越大
public final static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
2. 当图片过大,给Bitmap进行二次采样,压缩图片
第一个参数是图片byte资源,第二第三个参数分别是你要压缩出来图片的高和宽
// 通过Options对咱们图片加载进行控制 public static Bitmap sample(byte[] source,int requreHeight,int requreWidth){ // 实例化一个Options BitmapFactory.Options options = new BitmapFactory.Options(); // 设置为true,decode之后不会生成bitmap,它是对bitmap的基本信息进行获取,信息存在options中 options.inJustDecodeBounds = true; // 为options进行赋值 BitmapFactory.decodeByteArray(source,0,source.length,options); // bitmap的高 int outHeight = options.outHeight; // bitmap的宽 int outWidth = options.outWidth; // 假如你需要的宽度是400,采样比例应该怎么算 int sampleWidth = outWidth / requreWidth; // 计算高度采样比率 int sampleHeight = outHeight / requreHeight; // 获取最大的压缩比率 int sample = Math.max(sampleHeight,sampleWidth); // 设置压缩比率 options.inSampleSize = sample; // 不要只读基本信息,边缘了。 options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(source,0,source.length,options()); }
相关文章推荐
- Win32实现迷宫
- SPFA 最短路 带负权边的---- 粗了解
- POJ 1042 Gone Fishing(模拟+贪心)
- SPFA 最短路 带负权边的---- 粗了解
- 深拷贝与浅拷贝
- 【排序算法】选择排序原理及Java实现
- swift - NSUserDefaults 的使用
- postDelayed方法和removeCallbacks方法的使用
- 作业6-学生成绩录入
- Win32实现迷宫
- 夺命雷公狗ThinkPHP项目之----企业网站25之网站前台面包屑导航URL的完善
- 作业6 :团队作业
- OpenCV:对XML和YAML文件实现I/O操作
- Spark:Yarn-cluster和Yarn-client区别与联系
- c++常量
- C++ primer 读书笔记 第九章 顺序容器
- C++上级实验4-求1000以内所有偶数的和
- 页面布局之双飞翼布局
- c++写入txt文件
- 自己玩玩