采用Bitmap的extractAlpha产生图片边缘光晕效果

      前几天使用一款android手机测试的时候,
发现了应用的 shortcut 九宫格页面有一个点击效果,
就是当点击一个应用的icon图标的时候,会在icon的周围有荧光效果,
无论icon的形状是什么样子的都会有这样的效果,然后又想到Apidemo里面有个alphaDrawable例子
大家可以去在回顾一下,之后我就想到了会不会是使用这个extractAlpha实现的,自己就动手写了个例子
发现效果确实不错,分享给大家
主要关键点
1、设置imageview的src drawable
2、从src drawable中抽取 bitmap的alpha(只有透明度没有颜色)
3、使用bitmap的alpha填充一种颜色后生产新的bitmap
4、使用新生成的bitmap做一个statelistdrawable作为imageview的backgroud
5、这需要注意的是要跟imageview设置几个像素的padding这样才不会让src和backgroud重合






主要的代码:
Java代码   收藏代码
  1. /* 
  2.  * Copyright (C) 2009 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package com.study;  
  18.   
  19. import android.content.Context;  
  20. import android.content.res.TypedArray;  
  21. import android.graphics.Bitmap;  
  22. import android.graphics.Canvas;  
  23. import android.graphics.Color;  
  24. import android.graphics.Paint;  
  25. import android.graphics.Bitmap.Config;  
  26. import android.graphics.drawable.BitmapDrawable;  
  27. import android.graphics.drawable.StateListDrawable;  
  28. import android.util.AttributeSet;  
  29. import android.view.View;  
  30. import android.view.ViewGroup;  
  31. import android.widget.ImageView;  
  32.   
  33. /** 
  34.  * A layout that arranges its children in a grid.  The size of the 
  35.  * cells is set by the {@link #setCellSize} method and the 
  36.  * android:cell_width and android:cell_height attributes in XML. 
  37.  * The number of rows and columns is determined at runtime.  Each 
  38.  * cell contains exactly one view, and they flow in the natural 
  39.  * child order (the order in which they were added, or the index 
  40.  * in {@link #addViewAt}.  Views can not span multiple cells. 
  41.  */  
  42. public class FixedGridLayout extends ViewGroup {  
  43.     int mCellWidth;  
  44.     int mCellHeight;  
  45.   
  46.     public FixedGridLayout(Context context) {  
  47.         super(context);  
  48.     }  
  49.   
  50.     public FixedGridLayout(Context context, AttributeSet attrs) {  
  51.         super(context, attrs);  
  52.   
  53.         // Read the resource attributes.  
  54.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedGridLayout);  
  55.         mCellWidth = a.getDimensionPixelSize(R.styleable.FixedGridLayout_cellWidth, -1);  
  56.         mCellHeight = a.getDimensionPixelSize(R.styleable.FixedGridLayout_cellHeight, -1);  
  57.         a.recycle();  
  58.           
  59.     }  
  60.   
  61.     public void setCellWidth(int px) {  
  62.         mCellWidth = px;  
  63.         requestLayout();  
  64.     }  
  65.   
  66.     public void setCellHeight(int px) {  
  67.         mCellHeight = px;  
  68.         requestLayout();  
  69.     }  
  70.   
  71.     @Override  
  72.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  73.         int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.AT_MOST);  
  74.         int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.AT_MOST);  
  75.   
  76.         int count = getChildCount();  
  77.         for (int index=0; index<count; index++) {  
  78.             final View child = getChildAt(index);  
  79.             child.measure(cellWidthSpec, cellHeightSpec);  
  80.         }  
  81.         // Use the size our parents gave us  
  82.         setMeasuredDimension(resolveSize(mCellWidth*count, widthMeasureSpec),  
  83.                 resolveSize(mCellHeight*count, heightMeasureSpec));  
  84.     }  
  85.   
  86.     @Override  
  87.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  88.         int cellWidth = mCellWidth;  
  89.         int cellHeight = mCellHeight;  
  90.         int columns = (r - l) / cellWidth;  
  91.         if (columns < 0) {  
  92.             columns = 1;  
  93.         }  
  94.         int x = 0;  
  95.         int y = 0;  
  96.         int i = 0;  
  97.         int count = getChildCount();  
  98.         for (int index=0; index<count; index++) {  
  99.             final View child = getChildAt(index);  
  100.   
  101.             int w = child.getMeasuredWidth();  
  102.             int h = child.getMeasuredHeight();  
  103.   
  104.             int left = x + ((cellWidth-w)/2);  
  105.             int top = y + ((cellHeight-h)/2);  
  106.   
  107.             child.layout(left, top, left+w, top+h);  
  108.             if (i >= (columns-1)) {  
  109.                 // advance to next row  
  110.                 i = 0;  
  111.                 x = 0;  
  112.                 y += cellHeight;  
  113.             } else {  
  114.                 i++;  
  115.                 x += cellWidth;  
  116.             }  
  117.         }  
  118.     }  
  119.       
  120.     @Override  
  121.     protected void onFinishInflate() {  
  122.         super.onFinishInflate();  
  123.         int cnt = getChildCount();  
  124.         Paint p = new Paint();  
  125.         p.setColor(Color.CYAN);  
  126.           
  127.         for (int i=0; i<cnt; i++) {  
  128.             View v = getChildAt(i);  
  129.             setStateDrawable((ImageView)v, p);  
  130.         }  
  131.     }  
  132.       
  133.     private void setStateDrawable(ImageView v, Paint p) {  
  134.         BitmapDrawable bd = (BitmapDrawable) v.getDrawable();  
  135.         Bitmap b = bd.getBitmap();  
  136.         Bitmap bitmap = Bitmap.createBitmap(bd.getIntrinsicWidth(), bd.getIntrinsicHeight(), Config.ARGB_8888);  
  137.         Canvas canvas = new Canvas(bitmap);  
  138.         canvas.drawBitmap(b.extractAlpha(), 00, p);  
  139.           
  140.         StateListDrawable sld = new StateListDrawable();  
  141.         sld.addState(new int[]{android.R.attr.state_pressed}, new BitmapDrawable(bitmap));  
  142.           
  143.         v.setBackgroundDrawable(sld);  
  144.     }  
  145. }  


具体见附件。

为ImageView或者LinearLayout画图片阴影
http://407827531.iteye.com/blog/1194984

图片阴影效果和影子效果

http://www.eoeandroid.com/thread-90575-1-2.html


http://gundumw100.iteye.com/blog/1205547


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值