Launcher2 快捷方式图标的圆角处理及解析
依然是针对Launcher2的代码解析,并且是关于创建的快捷方式图标的一点代码解析和后期处理方式。
直接看代码,Utilities.java,因为快捷方式图标绘制最后返回的bitmap对象是在这个类中完成的,在之前的博客也提到过可以给应用图标添加统一的背景图,同样的在这个类中实际也是可以实现的。
直接上代码,其中增加了添加统一背景的代码,可以结合之前的文章参考来做,效果是一样的。
这次主要来解析下下面这个函数的过程
/*** Returns a bitmap suitable for the all apps view.*/static Bitmap createIconBitmap(Drawable icon, Context context) {synchronized (sCanvas) { // we share the statics :-(if (sIconWidth == -1) {initStatics(context);}//将icon的默认宽和高赋值给width和height,sIconWidth,sIconHeight的定义可以在代码中找到int width = sIconWidth;int height = sIconHeight;if (icon instanceof PaintDrawable) {PaintDrawable painter = (PaintDrawable) icon;painter.setIntrinsicWidth(width);painter.setIntrinsicHeight(height);} else if (icon instanceof BitmapDrawable) {// Ensure the bitmap has a density. //直接从Drawable中获取bitmap对象BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;Bitmap bitmap = bitmapDrawable.getBitmap();BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;Bitmap bitmap = bitmapDrawable.getBitmap();//设置icon的适配DPI参数,可忽略if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());}}//获取适配的应用图标实际大小,正常情况下会有三个尺寸48*48,72*72,96*96int sourceWidth = icon.getIntrinsicWidth();int sourceHeight = icon.getIntrinsicHeight();if (sourceWidth > 0 && sourceHeight > 0) {// There are intrinsic sizes.if (width < sourceWidth || height < sourceHeight) {// It's too big, scale it down.//应用图标尺寸大于默认尺寸,则默认尺寸按比例缩放至应用图标的宽高比例final float ratio = (float) sourceWidth / sourceHeight;if (sourceWidth > sourceHeight) {height = (int) (width / ratio);} else if (sourceHeight > sourceWidth) {width = (int) (height * ratio);}} else if (sourceWidth < width && sourceHeight < height) {// Don't scale up the icon//应用图标大小小于默认给定大小,不进行裁剪缩放width = sourceWidth;height = sourceHeight;}}//默认icon图标大小// no intrinsic size --> use default sizeint textureWidth = sIconTextureWidth;int textureHeight = sIconTextureHeight;//创建画布为默认icon大小final Bitmap bitmap = Bitmap.createBitmap(textureWidth,textureHeight, Bitmap.Config.ARGB_8888);final Canvas canvas = sCanvas;canvas.setBitmap(bitmap);final int left = (textureWidth - width) / 2;final int top = (textureHeight - height) / 2;@SuppressWarnings("all")// suppress dead code warningfinal boolean debug = false;if (debug) {// draw a big box for the icon for debuggingcanvas.drawColor(sColors[sColorIndex]);if (++sColorIndex >= sColors.length)sColorIndex = 0;Paint debugPaint = new Paint();debugPaint.setColor(0xffcccc00);canvas.drawRect(left, top, left + width, top + height,debugPaint);}
/*** 添加统一背景,另一种方案,简单说就是利用一个现有的bitmap创建一个画布,并将图标画在这个画布上,实现增加背景的功能투ᅪ뾰ᄏᄆᄈᄒᄚ*/
/* if (true) {Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_bg);int backWidth = backBitmap.getWidth();int backHeight = backBitmap.getHeight();if (backWidth != sIconWidth || backHeight != sIconHeight) {Matrix matrix = new Matrix();matrix.postScale((float) sIconWidth / backWidth,(float) sIconHeight / backHeight);canvas.drawBitmap(Bitmap.createBitmap(backBitmap, 0, 0,backWidth, backHeight, matrix, true), 0.0f, 0.0f,null);} else {canvas.drawBitmap(backBitmap, 0.0f, 0.0f, null);}}*/将应用图标bitmap绘制在画布上sOldBounds.set(icon.getBounds());icon.setBounds(left, top, left + width, top + height);icon.draw(canvas);icon.setBounds(sOldBounds);canvas.setBitmap(null);//返回已经过处理的圆角bitmapreturn getRoundedBitmap(bitmap);}}
下面是圆角处理函数
//bitmap圆角处理public static Bitmap getRoundedBitmap(Bitmap mBitmap){//Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.frame); //创建与原始位图大小相同的画布位图ᄡᄡᄑ따샤ᅫ콰ᄐ Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888); //初始化画布Canvas mCanvas = new Canvas(bgBitmap); Paint mPaint = new Paint(); Rect mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); RectF mRectF = new RectF(mRect); //设置圆角半径 float roundPx = 70; mPaint.setAntiAlias(true); //绘制圆角矩形mCanvas.drawRoundRect(mRectF, roundPx, roundPx, mPaint); //设置图像的叠加模式, 此处模式选择可参考后面的规则mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//绘制图像 mCanvas.drawBitmap(mBitmap, mRect, mRect, mPaint); return bgBitmap;
}
1.PorterDuff.Mode.CLEAR
所绘制不会提交到画布上。
2.PorterDuff.Mode.SRC
显示上层绘制图片
3.PorterDuff.Mode.DST
显示下层绘制图片
4.PorterDuff.Mode.SRC_OVER
正常绘制显示,上下层绘制叠盖。
5.PorterDuff.Mode.DST_OVER
上下层都显示。下层居上显示。
6.PorterDuff.Mode.SRC_IN
取两层绘制交集。显示上层。
7.PorterDuff.Mode.DST_IN
取两层绘制交集。显示下层。
8.PorterDuff.Mode.SRC_OUT
取上层绘制非交集部分。
9.PorterDuff.Mode.DST_OUT
取下层绘制非交集部分。
10.PorterDuff.Mode.SRC_ATOP
取下层非交集部分与上层交集部分
11.PorterDuff.Mode.DST_ATOP
取上层非交集部分与下层交集部分
12.PorterDuff.Mode.XOR
13.PorterDuff.Mode.DARKEN
14.PorterDuff.Mode.LIGHTEN
15.PorterDuff.Mode.MULTIPLY
16.PorterDuff.Mode.SCREEN
通过以上的代码就可以实现应用图标的圆角处理,效果可在修改后自行看到,在此不贴图了,每次截图甚是麻烦。同时对于位图的处理实际还可以有很多做法,可以参考来做,诸多效果在此借鉴下其他网友的做法
带倒影的图标位图处理代码
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {final int reflectionGap = 4;int w = bitmap.getWidth();int h = bitmap.getHeight();Matrix matrix = new Matrix();matrix.preScale(1, -1);Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,h / 2, matrix, false);Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),Config.ARGB_8888);Canvas canvas = new Canvas(bitmapWithReflection);canvas.drawBitmap(bitmap, 0, 0, null);Paint deafalutPaint = new Paint();canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);Paint paint = new Paint();LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,0x00ffffff, TileMode.CLAMP);paint.setShader(shader);// Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));// Draw a rectangle using the paint with our linear gradientcanvas.drawRect(0, h, w, bitmapWithReflection.getHeight()+ reflectionGap, paint);return bitmapWithReflection;}
还有直接可以缩放drawable对象的代码,这样可以不用刻意的获取drawable的bitmap对象位图再对位图进行缩放,再绘制去实现缩放的功能。
public static Drawable zoomDrawable(Drawable drawable, int w, int h) {int width = drawable.getIntrinsicWidth();int height = drawable.getIntrinsicHeight();// drawable转换成bitmapBitmap oldbmp = drawableToBitmap(drawable);// 创建操作图片用的Matrix对象Matrix matrix = new Matrix();// 计算缩放比例float sx = ((float) w / width);float sy = ((float) h / height);// 设置缩放比例matrix.postScale(sx, sy);// 建立新的bitmap,其内容是对原bitmap的缩放后的图Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,matrix, true);return new BitmapDrawable(newbmp);}
bitmap的缩放处理代码
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {int w = bitmap.getWidth();int h = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidth = ((float) width / w);float scaleHeight = ((float) height / h);matrix.postScale(scaleWidth, scaleHeight);Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);return newbmp;}
以上就是对于launcher2的图标处理方法的解析和修改。
Launcher2 快捷方式图标的圆角处理及解析相关推荐
- Android 如何预置桌面上的应用程序图标、快捷方式图标或者窗口小部件?
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- xp系统如何将计算机放到桌面快捷方式,如何整理xp系统电脑桌面快捷方式图标...
通常win xp用户都有这样的烦恼,在电脑上安装了大量的应用软件时,电脑桌面上的快捷图标也会变得很多,以至于找到一个快捷方式图标都不容易,这也间接的影响了我们的工作效率,并且桌面看起来很乱,影响美观, ...
- Win7快捷方式图标不显示解决办法
问题:WIN7的系统,桌面.开始菜单以及任务栏的快捷方式图标显示不正常,看不到程序默认图标,快捷方式图标不显示. 解决方法:删除程序图标缓存即可. 将下面的内容复制到记事本保存为"图标缓存清 ...
- 桌面计算机怎么覆盖文件,win7系统桌面快捷方式图标被未知文件覆盖如何解决...
win7系统纯净版开机发现桌面桌面快捷方式图标被未知文件覆盖了,但是运行起来一切正常,到底是什么原因呢?主要原因是由于操作系统中图标缓存满了,所桌面图标快捷图标被未知文件覆盖了.大家不必紧张,按照下面 ...
- windows桌面快捷方式图标上面怎么老是会产生一个问号解决方案
桌面快捷方式图标上面怎么老是会产生一个问号(获取帮助符号)? 新建一txt文件,如:a.txt, 复制该代码: for /r . %%a in (.) do @if exist "%%a\. ...
- eclipse里source的快捷方法_教你如何在Linux中设置快捷方式图标
这篇文章主要给大家介绍了关于如何在Linux中设置快捷方式图标的相关资料,文中通过示例代码的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧 前言 Linux系统 ...
- C#项目打包后安装的桌面快捷方式图标怎么设置成自己想要的图标
#项目打包后安装的桌面快捷方式图标怎么设置成自己想要的图标 2012-08-25 09:11匿名 | 浏览 3286 次 C#编程 C#项目用vs2005自带的工具打包后安装的桌面快捷方式图标怎么设置 ...
- 快捷方式图标 html,界面上的应用程序快捷方式图标都变成了IE图标
你这个应该是快捷方式显示箭头了,但是箭头图标的文件丢失了~ 一.修改属性.在windowsXP系统下,用鼠标右键单击桌面空白处,从弹出菜单中选择"属性".在打开的"显示属 ...
- win10桌面计算机快捷没了,Win10桌面快捷方式图标消失了的解决方法介绍
不少使用windows10系统的朋友都反映,自己的电脑在运行一段时间后,桌面上的快捷方式图标突然全都不见了,这该怎么办呢?桌面快捷方式不见会带来很多不便之处.下面,小编就为大家介绍win10系统恢复桌 ...
最新文章
- java校园足球管理系统_基于jsp的校园足球管理平台-JavaEE实现校园足球管理平台 - java项目源码...
- 数据库如何生成sql语句
- 怎么知道文本前后空格的字符格式_JDK 14的新特性:文本块Text Blocks
- CSS布局(二) 盒子模型属性
- wdcp找回mysql密码_wdcp忘记后台admin密码,重置MYSQL ROOT密码
- 2019/2/17 Python今日收获
- linux initrd usb热插拔,8.3 发行注记 Red Hat Enterprise Linux 8 | Red Hat Customer Portal
- java数组变量_关于java 的数组引用变量
- Visual C++ 内存泄露检测工具(VLD)
- 安卓手机使用VNET抓包京东wskey
- ps学习笔记4-PS常用的格式
- No Spring WebApplicationInitializer types detected on classpath
- aws saa是什么缩写?aws saa认证考察什么?
- Oracle 11g 新特性 -- Result Cache(结果高速缓存)
- css中伪类和伪元素有什么不一样
- Mysql进阶四:常见函数-日期函数
- 用xlwt和xlrd在不修改Excel单元格格式的情况下修改单元格内容
- C语言接龙,C语言完成成语接龙小游戏
- 如何使用python提取dwg文件中的坐标信息
- 计算机专业课观后感,计算机公开课观后感