bitmap描边(光晕)效果

最近在玩英雄杀,有个效果看起来挺酷的,有图有真相。图片的周围有一圈光晕
是不是挺赞的, 三星英雄项羽。。
言归正传,作为一个技术人员呢,看到一个很赞的效果就想着能不能实现。
思路一:对原始图片处理,对图片边缘做描边效果。
思路二:对图片所在的ImageView设置background。
对于第一种方案现在没有好的解决方法,并且处理起来也并不简单,所以就用了偷懒的方案(思路二)。这里用到了bitmap的一个不常用的api :extractAlpha();其官方解释为

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 通道,而我们就是在此基础上做手脚。。

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); 是为图片设置描边颜色。其核心代码为:

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 重合了。布局如下:

<?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>

由于代码部分都贴出来了,就不提供源码了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值