采用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重合

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

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

采用Bitmap的extractAlpha产生图片边缘光晕效果
主要的代码:
Java代码 收藏代码
  1. /*
  2. *Copyright(C)2009TheAndroidOpenSourceProject
  3. *
  4. *LicensedundertheApacheLicense,Version2.0(the"License");
  5. *youmaynotusethisfileexceptincompliancewiththeLicense.
  6. *YoumayobtainacopyoftheLicenseat
  7. *
  8. *http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  11. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  12. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  13. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  14. *limitationsundertheLicense.
  15. */
  16. packagecom.study;
  17. importandroid.content.Context;
  18. importandroid.content.res.TypedArray;
  19. importandroid.graphics.Bitmap;
  20. importandroid.graphics.Canvas;
  21. importandroid.graphics.Color;
  22. importandroid.graphics.Paint;
  23. importandroid.graphics.Bitmap.Config;
  24. importandroid.graphics.drawable.BitmapDrawable;
  25. importandroid.graphics.drawable.StateListDrawable;
  26. importandroid.util.AttributeSet;
  27. importandroid.view.View;
  28. importandroid.view.ViewGroup;
  29. importandroid.widget.ImageView;
  30. /**
  31. *Alayoutthatarrangesitschildreninagrid.Thesizeofthe
  32. *cellsissetbythe{@link#setCellSize}methodandthe
  33. *android:cell_widthandandroid:cell_heightattributesinXML.
  34. *Thenumberofrowsandcolumnsisdeterminedatruntime.Each
  35. *cellcontainsexactlyoneview,andtheyflowinthenatural
  36. *childorder(theorderinwhichtheywereadded,ortheindex
  37. *in{@link#addViewAt}.Viewscannotspanmultiplecells.
  38. */
  39. publicclassFixedGridLayoutextendsViewGroup{
  40. intmCellWidth;
  41. intmCellHeight;
  42. publicFixedGridLayout(Contextcontext){
  43. super(context);
  44. }
  45. publicFixedGridLayout(Contextcontext,AttributeSetattrs){
  46. super(context,attrs);
  47. //Readtheresourceattributes.
  48. TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.FixedGridLayout);
  49. mCellWidth=a.getDimensionPixelSize(R.styleable.FixedGridLayout_cellWidth,-1);
  50. mCellHeight=a.getDimensionPixelSize(R.styleable.FixedGridLayout_cellHeight,-1);
  51. a.recycle();
  52. }
  53. publicvoidsetCellWidth(intpx){
  54. mCellWidth=px;
  55. requestLayout();
  56. }
  57. publicvoidsetCellHeight(intpx){
  58. mCellHeight=px;
  59. requestLayout();
  60. }
  61. @Override
  62. protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
  63. intcellWidthSpec=MeasureSpec.makeMeasureSpec(mCellWidth,MeasureSpec.AT_MOST);
  64. intcellHeightSpec=MeasureSpec.makeMeasureSpec(mCellHeight,MeasureSpec.AT_MOST);
  65. intcount=getChildCount();
  66. for(intindex=0;index<count;index++){
  67. finalViewchild=getChildAt(index);
  68. child.measure(cellWidthSpec,cellHeightSpec);
  69. }
  70. //Usethesizeourparentsgaveus
  71. setMeasuredDimension(resolveSize(mCellWidth*count,widthMeasureSpec),
  72. resolveSize(mCellHeight*count,heightMeasureSpec));
  73. }
  74. @Override
  75. protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
  76. intcellWidth=mCellWidth;
  77. intcellHeight=mCellHeight;
  78. intcolumns=(r-l)/cellWidth;
  79. if(columns<0){
  80. columns=1;
  81. }
  82. intx=0;
  83. inty=0;
  84. inti=0;
  85. intcount=getChildCount();
  86. for(intindex=0;index<count;index++){
  87. finalViewchild=getChildAt(index);
  88. intw=child.getMeasuredWidth();
  89. inth=child.getMeasuredHeight();
  90. intleft=x+((cellWidth-w)/2);
  91. inttop=y+((cellHeight-h)/2);
  92. child.layout(left,top,left+w,top+h);
  93. if(i>=(columns-1)){
  94. //advancetonextrow
  95. i=0;
  96. x=0;
  97. y+=cellHeight;
  98. }else{
  99. i++;
  100. x+=cellWidth;
  101. }
  102. }
  103. }
  104. @Override
  105. protectedvoidonFinishInflate(){
  106. super.onFinishInflate();
  107. intcnt=getChildCount();
  108. Paintp=newPaint();
  109. p.setColor(Color.CYAN);
  110. for(inti=0;i<cnt;i++){
  111. Viewv=getChildAt(i);
  112. setStateDrawable((ImageView)v,p);
  113. }
  114. }
  115. privatevoidsetStateDrawable(ImageViewv,Paintp){
  116. BitmapDrawablebd=(BitmapDrawable)v.getDrawable();
  117. Bitmapb=bd.getBitmap();
  118. Bitmapbitmap=Bitmap.createBitmap(bd.getIntrinsicWidth(),bd.getIntrinsicHeight(),Config.ARGB_8888);
  119. Canvascanvas=newCanvas(bitmap);
  120. canvas.drawBitmap(b.extractAlpha(),0,0,p); 先extractAlpha 一下,然后才可用 画笔 产生新的位图。 否则画笔不生效。
  121. StateListDrawablesld=newStateListDrawable();
  122. sld.addState(newint[]{android.R.attr.state_pressed},newBitmapDrawable(bitmap));
  123. v.setBackgroundDrawable(sld);
  124. }
  125. }





bitmap1 bitmap2

InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code);
mBitmap = BitmapFactory.decodeStream(is);
mBitmap2 = mBitmap.extractAlpha();

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


Paint p = new Paint();
float y = 10;

p.setColor(Color.RED);
canvas.drawBitmap(mBitmap, 10, y, p);
y += mBitmap.getHeight() + 10;
canvas.drawBitmap(mBitmap2, 10, y, p);
y += mBitmap2.getHeight() + 10;

你可能感兴趣的:(bitmap)