bitmap描边(光晕)效果
2015-08-26 11:13
357 查看
最近在玩英雄杀,有个效果看起来挺酷的,有图有真相。
![](http://img.blog.csdn.net/20150826104701668)
是不是挺赞的, 三星英雄项羽。。
言归正传,作为一个技术人员呢,看到一个很赞的效果就想着能不能实现。
思路一:对原始图片处理,对图片边缘做描边效果。
思路二:对图片所在的ImageView设置background。
对于第一种方案现在没有好的解决方法,并且处理起来也并不简单,所以就用了偷懒的方案(思路二)。这里用到了bitmap的一个不常用的api :extractAlpha();其官方解释为
我的理解就是返回 bitmap 一个原始 alpha 通道,而我们就是在此基础上做手脚。。
以上为一个自定义LinearLayout,主要就是onFinishInflate(Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added. )就是在xml被加载完成之后,对它里面所包含的ImageView进行处理加上点击效果,
其中 p.setColor(Color.CYAN); 是为图片设置描边颜色。其核心代码为:
创建一个图片大小的画布进行描边。
为了让图片光晕效果突出,在xml里要为其设置 padding 值,这样就不会使 src 于与 setBackgroundDrawable 重合了。布局如下:
由于代码部分都贴出来了,就不提供源码了。
是不是挺赞的, 三星英雄项羽。。
言归正传,作为一个技术人员呢,看到一个很赞的效果就想着能不能实现。
思路一:对原始图片处理,对图片边缘做描边效果。
思路二:对图片所在的ImageView设置background。
对于第一种方案现在没有好的解决方法,并且处理起来也并不简单,所以就用了偷懒的方案(思路二)。这里用到了bitmap的一个不常用的api :extractAlpha();其官方解释为
[code]public Bitmap extractAlpha () Added in API level 1 Returns a new bitmap that captures the alpha values of the original. This may be drawn with Canvas.drawBitmap(), where the color(s) will be taken from the paint that is passed to the draw call. Returns new bitmap containing the alpha channel of the original bitmap.
我的理解就是返回 bitmap 一个原始 alpha 通道,而我们就是在此基础上做手脚。。
[code]public class FixedGridLayout extends LinearLayout { public FixedGridLayout(Context context) { super(context); } public FixedGridLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); int cnt = getChildCount(); Paint p = new Paint(); p.setColor(Color.CYAN); for (int i=0; i<cnt; i++) { View v = getChildAt(i); if(v instanceof ImageView){ setStateDrawable((ImageView)v, p); } } } /** * 主要函数:为bitmap做光晕效果 * @param v * @param p */ private void setStateDrawable(ImageView v, Paint p) { BitmapDrawable bd = (BitmapDrawable) v.getDrawable(); Bitmap b = bd.getBitmap(); Bitmap bitmap = Bitmap.createBitmap(bd.getIntrinsicWidth(), bd.getIntrinsicHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(b.extractAlpha(), 0, 0, p); StateListDrawable sld = new StateListDrawable(); sld.addState(new int[]{android.R.attr.state_pressed}, new BitmapDrawable(bitmap)); v.setBackgroundDrawable(sld); } }
以上为一个自定义LinearLayout,主要就是onFinishInflate(Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added. )就是在xml被加载完成之后,对它里面所包含的ImageView进行处理加上点击效果,
其中 p.setColor(Color.CYAN); 是为图片设置描边颜色。其核心代码为:
[code]Bitmap bitmap = Bitmap.createBitmap(bd.getIntrinsicWidth(), bd.getIntrinsicHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(b.extractAlpha(), 0, 0, p);
创建一个图片大小的画布进行描边。
为了让图片光晕效果突出,在xml里要为其设置 padding 值,这样就不会使 src 于与 setBackgroundDrawable 重合了。布局如下:
[code]<?xml version="1.0" encoding="utf-8"?> <com.study.FixedGridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:padding="5dp" android:clickable="true" android:layout_height="wrap_content" android:src="@drawable/gimp" android:scaleType="fitCenter" /> </com.study.FixedGridLayout>
由于代码部分都贴出来了,就不提供源码了。
相关文章推荐
- 关于Scalability的一些思考与疑问
- iOS return和copy的区别
- apache + subversion for linux 安装
- centos服务器操作要点
- keil注册机激活的方法
- Objective-C 【动态类型检测&响应方法】
- Objective-C 【动态类型检测&响应方法】
- [HEOI2012][BZOJ2744] 朋友圈|匈牙利算法|最大独立集|最大团
- eclipse 导入 jar 源码
- Java性能优化哪些事
- Android大图片裁剪终极解决方案(中:从相册截图)
- linux挂载新硬盘,开机自动挂载
- Make 简要教程及整理 --- 第二篇 Makefile 总述
- Swift 学习记录—1
- BS和CS架构
- poj 3349 hash(相同雪花)
- SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)
- Linux单独安装svn服务器-subversion
- 如何了解Office 365 Plans& Pricing
- ext5学习网站