当前位置: 代码迷 >> Android >> Android平台下实现图片的缩放,圆角,倒影效果
  详细解决方案

Android平台下实现图片的缩放,圆角,倒影效果

热度:402   发布时间:2016-05-01 20:05:11.0
Android平台上实现图片的缩放,圆角,倒影效果

主要包含了 放大缩小 圆角图片 和 倒影?

Java代码??收藏代码
  1. package?com.lp.imageUI;??
  2. ??
  3. import?android.graphics.Bitmap;??
  4. import?android.graphics.Canvas;??
  5. import?android.graphics.LinearGradient;??
  6. import?android.graphics.Matrix;??
  7. import?android.graphics.Paint;??
  8. import?android.graphics.PixelFormat;??
  9. import?android.graphics.PorterDuffXfermode;??
  10. import?android.graphics.Rect;??
  11. import?android.graphics.RectF;??
  12. import?android.graphics.Bitmap.Config;??
  13. import?android.graphics.PorterDuff.Mode;??
  14. import?android.graphics.Shader.TileMode;??
  15. import?android.graphics.drawable.Drawable;??
  16. public?class?ImageUtil?{??
  17. ??????
  18. ????//放大缩小图片??
  19. ????public?static?Bitmap?zoomBitmap(Bitmap?bitmap,int?w,int?h){??
  20. ????????int?width?=?bitmap.getWidth();??
  21. ????????int?height?=?bitmap.getHeight();??
  22. ????????Matrix?matrix?=?new?Matrix();??
  23. ????????float?scaleWidht?=?((float)w?/?width);??
  24. ????????float?scaleHeight?=?((float)h?/?height);??
  25. ????????matrix.postScale(scaleWidht,?scaleHeight);??
  26. ????????Bitmap?newbmp?=?Bitmap.createBitmap(bitmap,?0,?0,?width,?height,?matrix,?true);??
  27. ????????return?newbmp;??
  28. ????}??
  29. ????//将Drawable转化为Bitmap??
  30. ?????public?static?Bitmap?drawableToBitmap(Drawable?drawable){??
  31. ????????????int?width?=?drawable.getIntrinsicWidth();??
  32. ????????????int?height?=?drawable.getIntrinsicHeight();??
  33. ????????????Bitmap?bitmap?=?Bitmap.createBitmap(width,?height,??
  34. ????????????????????drawable.getOpacity()?!=?PixelFormat.OPAQUE???Bitmap.Config.ARGB_8888??
  35. ????????????????????????????:?Bitmap.Config.RGB_565);??
  36. ????????????Canvas?canvas?=?new?Canvas(bitmap);??
  37. ????????????drawable.setBounds(0,0,width,height);??
  38. ????????????drawable.draw(canvas);??
  39. ????????????return?bitmap;??
  40. ??????????????
  41. ????????}??
  42. ???????
  43. ?????//获得圆角图片的方法??
  44. ????public?static?Bitmap?getRoundedCornerBitmap(Bitmap?bitmap,float?roundPx){??
  45. ??????????
  46. ????????Bitmap?output?=?Bitmap.createBitmap(bitmap.getWidth(),?bitmap??
  47. ????????????????.getHeight(),?Config.ARGB_8888);??
  48. ????????Canvas?canvas?=?new?Canvas(output);??
  49. ???
  50. ????????final?int?color?=?0xff424242;??
  51. ????????final?Paint?paint?=?new?Paint();??
  52. ????????final?Rect?rect?=?new?Rect(0,?0,?bitmap.getWidth(),?bitmap.getHeight());??
  53. ????????final?RectF?rectF?=?new?RectF(rect);??
  54. ???
  55. ????????paint.setAntiAlias(true);??
  56. ????????canvas.drawARGB(0,?0,?0,?0);??
  57. ????????paint.setColor(color);??
  58. ????????/**?
  59. ?????????*?画一个圆角矩形?
  60. ?????????*?rectF:?矩形?
  61. ?????????*?roundPx?圆角在x轴上或y轴上的半径?
  62. ?????????*/??
  63. ????????canvas.drawRoundRect(rectF,?roundPx,?roundPx+10,?paint);??
  64. ????????//设置两张图片相交时的模式??
  65. ????????//setXfermode前的是?dst?之后的是src??
  66. ????????//在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状。???
  67. ????????//如果新的Paint是完全不透明的,那么它将完全遮挡住下面的Paint;??
  68. ????????//PorterDuffXfermode就可以来解决这个问题??
  69. ????????//canvas原有的图片?可以理解为背景?就是dst??
  70. ????????//新画上去的图片?可以理解为前景?就是src??
  71. //??????paint.setXfermode(new?PorterDuffXfermode(Mode.SRC_OUT));??
  72. ????????paint.setXfermode(new?PorterDuffXfermode(Mode.SRC_IN));??
  73. ????????canvas.drawBitmap(bitmap,?rect,?rect,?paint);??
  74. ???
  75. ????????return?output;??
  76. ????}??
  77. ????//获得带倒影的图片方法??
  78. ????public?static?Bitmap?createReflectionImageWithOrigin(Bitmap?bitmap){??
  79. ????????//?图片与倒影间隔距离????
  80. ????????final?int?reflectionGap?=?4;??
  81. ????????//?图片的宽度????
  82. ????????int?width?=?bitmap.getWidth();??
  83. ?????????//?图片的高度????
  84. ????????int?height?=?bitmap.getHeight();??
  85. ??????????
  86. ????????Matrix?matrix?=?new?Matrix();??
  87. ????????//?图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转??
  88. ????????matrix.preScale(1,?-1);??
  89. ????????//?创建反转后的图片Bitmap对象,图片高是原图的一半。????
  90. ????????Bitmap?reflectionImage?=?Bitmap.createBitmap(bitmap,???
  91. ????????????????0,?height/2,?width,?height/2,?matrix,?false);??
  92. ????????//?创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。?可以理解为这张图将会在屏幕上显示?是原图和倒影的合体????
  93. ????????Bitmap?bitmapWithReflection?=?Bitmap.createBitmap(width,?(height?+?height/2),?Config.ARGB_8888);??
  94. ?????????//?构造函数传入Bitmap对象,为了在图片上画图????
  95. ????????Canvas?canvas?=?new?Canvas(bitmapWithReflection);??
  96. ????????//?画原始图片????
  97. ????????canvas.drawBitmap(bitmap,?0,?0,?null);??
  98. ????????//?画间隔矩形????
  99. ????????Paint?deafalutPaint?=?new?Paint();??
  100. ????????canvas.drawRect(0,?height,width,height?+?reflectionGap,??
  101. ????????????????deafalutPaint);??
  102. ????????//?画倒影图片????
  103. ????????canvas.drawBitmap(reflectionImage,?0,?height?+?reflectionGap,?null);??
  104. ????????//?实现倒影渐变效果????
  105. ????????Paint?paint?=?new?Paint();??
  106. ????????LinearGradient?shader?=?new?LinearGradient(0,??
  107. ????????????????bitmap.getHeight(),?0,?bitmapWithReflection.getHeight()??
  108. ????????????????+?reflectionGap,?0x70ffffff,?0x00ffffff,?TileMode.CLAMP);??
  109. ????????paint.setShader(shader);??
  110. ??????????
  111. ????????//?Set?the?Transfer?mode?to?be?porter?duff?and?destination?in??
  112. ????????//?覆盖效果????
  113. ????????paint.setXfermode(new?PorterDuffXfermode(Mode.DST_IN));??
  114. ????????//?Draw?a?rectangle?using?the?paint?with?our?linear?gradient??
  115. ????????canvas.drawRect(0,?height,?width,?bitmapWithReflection.getHeight()??
  116. ????????????????+?reflectionGap,?paint);??
  117. ???
  118. ????????return?bitmapWithReflection;??
  119. ????}??
  120. ??????
  121. }??



圆角和倒影的效果如图:?

?
<!--EndFragment-->

  相关解决方案