您的位置:首页 > 其它

bitmap描边(光晕)效果

2015-08-26 11:13 357 查看
最近在玩英雄杀,有个效果看起来挺酷的,有图有真相。



是不是挺赞的, 三星英雄项羽。。

言归正传,作为一个技术人员呢,看到一个很赞的效果就想着能不能实现。

思路一:对原始图片处理,对图片边缘做描边效果。

思路二:对图片所在的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>


由于代码部分都贴出来了,就不提供源码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: