Android实例] 圆角图片加阴影_第1页
Android实例] 圆角图片加阴影_第2页
Android实例] 圆角图片加阴影_第3页
Android实例] 圆角图片加阴影_第4页
Android实例] 圆角图片加阴影_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Android 实现圆角图片并添加阴影 此方法可实现圆角图片加阴影背景 先上图看下效果 主要的代码在 MyCoverImageView 类里面 具体实现是对 bitmap 的转换合成过程 废话少说 下面直接贴代码 1 工具类 public class MyCoverImageView extends ImageView private Bitmap src null public Paint paint new Paint Paint ANTI ALIAS FLAG private void init Context context src BitmapFactory decodeResource getResources R drawable src src zoomImg src 300 300 src toRoundCorner src 30 public MyCoverImageView Context context super context init context public MyCoverImageView Context context AttributeSet attrs super context attrs init context public MyCoverImageView Context context AttributeSet attrs int defStyle super context attrs defStyle init context Override protected void onSizeChanged int w int h int oldw int oldh super onSizeChanged w h oldw oldh Override protected void onDraw Canvas canvas super onDraw canvas public void setImageBitmapWithCoverIn Bitmap bm Bitmap out toRoundCorner bm 20 int w bm getWidth int h bm getHeight cover Bitmap dd BitmapFactory decodeResource getResources R drawable goods img bg Bitmap cover Bitmap createBitmap w h Bitmap Config ARGB 8888 Canvas canvas cover new Canvas cover NinePatch np new NinePatch dd dd getNinePatchChunk null Rect r new Rect 0 0 w h np draw canvas cover r Bitmap out Bitmap createBitmap w h Bitmap Config ARGB 8888 Canvas canvas out new Canvas out paint setAntiAlias false canvas out drawBitmap cover 0 0 paint dst paint setXfermode new PorterDuffXfermode Mode SRC IN canvas out drawBitmap bm 0 0 paint src try File myCaptureFile new File sdcard test png myCaptureFile deleteOnExit myCaptureFile createNewFile BufferedOutputStream bos new BufferedOutputStream new FileOutputStream myCaptureFile press Bitmap CompressFormat PNG 100 bos bos flush bos close catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace super setImageBitmap out static void setCornerRadii GradientDrawable drawable float r0 float r1 float r2 float r3 drawable setCornerRadii new float r0 r0 r1 r1 r2 r2 r3 r3 param bitmap param pixels return public static Bitmap toRoundCorner Bitmap bitmap int pixels Paint paint new Paint Paint ANTI ALIAS FLAG Bitmap output shadow Bitmap createBitmap bitmap getWidth bitmap getHeight Config ARGB 8888 Bitmap output Bitmap createBitmap bitmap getWidth bitmap getHeight Config ARGB 8888 Rect rect new Rect 0 0 bitmap getWidth bitmap getHeight RectF rectF new RectF rect Canvas canvas new Canvas output Canvas canvas shadow new Canvas output shadow 这里画一个渐变的图形 canvas drawColor 0 x00000000 GradientDrawable mDrawable new GradientDrawable GradientDrawable Orientation TL BR new int 0 xFFFF0000 0 xFF00FF00 0 xFF0000FF mDrawable setBounds rect mDrawable setShape GradientDrawable RECTANGLE mDrawable setGradientRadius float Math sqrt 2 60 mDrawable setGradientType GradientDrawable LINEAR GRADIENT 这里设置圆角 这里不是四边圆角的 要的话可以自己设置喔 可参加 setCornerRadii 方法 setCornerRadii mDrawable 0 pixels 0 pixels paint setShadowLayer 5f 5 0f 5 0f 0 xFF909090 canvas saveLayer rectF paint Canvas ALL SAVE FLAG mDrawable draw canvas paint setXfermode new PorterDuffXfermode Mode SRC IN paint setShadowLayer 0 5 0f 5 0f 0 xFF909090 canvas saveLayer rectF paint Canvas ALL SAVE FLAG BitmapDrawable imageDrawable new BitmapDrawable bitmap imageDrawable setBounds rect imageDrawable draw canvas canvas restore output shadow drawImageDropShadow output output recycle Paint shadowPaint new Paint canvas shadow drawColor 0 xFF000000 shadowPaint setShadowLayer 10 0f 0 0f 2 0f 0 xFF000000 canvas shadow drawBitmap output 0 0 shadowPaint return output shadow zoom bitmap to certain size param bitmap param width param height return public static Bitmap zoomBitmap Bitmap bitmap int width int height if bitmap null return null int w bitmap getWidth int h bitmap getHeight Matrix matrix new Matrix float scaleWidth float width w float scaleHeight float height h matrix postScale scaleWidth scaleHeight Bitmap newbmp Bitmap createBitmap bitmap 0 0 w h matrix true return newbmp param originalBitmap return private static Bitmap drawImageDropShadow Bitmap originalBitmap float radius 8f float readiusHalf radius 2 EmbossMaskFilter filter new EmbossMaskFilter new float 0 0 1 0 1f 20 20 0f BlurMaskFilter filter new BlurMaskFilter radius BlurMaskFilter Blur NORMAL Paint shadowPaint new Paint shadowPaint setAlpha 10 shadowPaint setStyle Paint Style FILL shadowPaint setColor Color WHITE shadowPaint setAntiAlias true shadowPaint setMaskFilter filter int offsetXY new int 2 Bitmap shadowBitmap originalBitmap extractAlpha shadowPaint offsetXY Bitmap shadowBitmap32 shadowBitmap copy Bitmap Config ARGB 8888 true Canvas c new Canvas shadowBitmap32 Log d flydy px offsetXY 0 py offsetXY 1 c drawBitmap originalBitmap offsetXY 0 offsetXY 1 null shadowBitmap recycle return shadowBitmap32 public static Bitmap zoomImg Bitmap bm int newWidth int newHeight int width bm getWidth int height bm getHeight float scaleWidth float newWidth width float scaleHeight float newHeight height Matrix matrix new Matrix matrix postScale scaleWidth scaleHeight Bitmap newbm Bitmap

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论