经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android 图片设置圆角
来源:cnblogs  作者:暴走编程  时间:2022/1/17 10:45:25  对本文有异议

Android 开发中,经常需要对图片进行二次处理,比如添加圆角效果 或 显示圆形图片;

方法一

通过第三方框架 Glide 设置圆角效果;

写法1:

  1. RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30));//图片圆角为30
  2. Glide.with(this).load(URL) //图片地址
  3. .apply(options)
  4. .into(ImagView);

写法2:

  1. RequestOptions requestOptions = new RequestOptions();
  2. requestOptions.placeholder(R.drawable.ic_launcher_background);
  3. requestOptions.circleCropTransform();
  4. requestOptions.transforms( new RoundedCorners(30));
  5. Glide.with(this).load(URL) //图片地址
  6. .apply(options)
  7. .into(ImagView);

写法3:

  1. RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30));
  2. Glide.with(this).load(URL) //图片地址
  3. .apply(options)
  4. .into(ImagView);
  1. public class RoundTransform extends BitmapTransformation {
  2. private static float radius = 0f;
  3. public RoundTransform(Context context) {
  4. this(context, 4);
  5. }
  6. public RoundTransform(Context context, int dp) {
  7. super(context);
  8. this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
  9. }
  10. @Override
  11. protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
  12. Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
  13. return roundCrop(pool, bitmap);
  14. }
  15. private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
  16. if (source == null) return null;
  17. Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  18. if (result == null) {
  19. result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  20. }
  21. Canvas canvas = new Canvas(result);
  22. Paint paint = new Paint();
  23. paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
  24. paint.setAntiAlias(true);
  25. RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
  26. canvas.drawRoundRect(rectF, radius, radius, paint);
  27. return result;
  28. }
  29. public String getId() {
  30. return getClass().getName() + Math.round(radius);
  31. }
  32. @Override
  33. public void updateDiskCacheKey(MessageDigest messageDigest) {
  34. }
  35. }
方法二

自定义ImageView 设置圆角效果;

  1. <ImageView
  2. android:id="@+id/iv"
  3. android:layout_width="300dp"
  4. android:layout_height="300dp"
  5. android:layout_centerHorizontal="true"
  6. />
  1. ImageView iv = findViewById(R.id.iv);
  2. Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.fengjing);
  3. Bitmap outBitmap =getRoundBitmapByShader(bitmap, 500,300,20, 3);
  4. iv.setImageBitmap(outBitmap);
  1. public class RoundRectImageView extends ImageView{
  2. private Paint paint;
  3. public RoundRectImageView(Context context) {
  4. this(context,null);
  5. }
  6. public RoundRectImageView(Context context, AttributeSet attrs) {
  7. this(context, attrs,0);
  8. }
  9. public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
  10. super(context, attrs, defStyle);
  11. paint = new Paint();
  12. }
  13. /**
  14. * 绘制圆角矩形图片
  15. */
  16. @Override
  17. protected void onDraw(Canvas canvas) {
  18. Drawable drawable = getDrawable();
  19. if (null != drawable) {
  20. Bitmap bitmap = getBitmapFromDrawable(drawable);
  21. // Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
  22. Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0);
  23. final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
  24. final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
  25. paint.reset();
  26. canvas.drawBitmap(b, rectSrc, rectDest, paint);
  27. } else {
  28. super.onDraw(canvas);
  29. }
  30. }
  31. /**
  32. * 把资源图片转换成Bitmap
  33. * @param drawable
  34. * 资源图片
  35. * @return 位图
  36. */
  37. public static Bitmap getBitmapFromDrawable(Drawable drawable) {
  38. int width = drawable.getIntrinsicWidth();
  39. int height = drawable.getIntrinsicHeight();
  40. Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
  41. .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  42. : Bitmap.Config.RGB_565);
  43. Canvas canvas = new Canvas(bitmap);
  44. //drawable.setBounds(-4, -4, width + 4, height + 4);
  45. drawable.draw(canvas);
  46. return bitmap;
  47. }
  48. public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
  49. if (bitmap == null) {
  50. return null;
  51. }
  52. int width = bitmap.getWidth();
  53. int height = bitmap.getHeight();
  54. float widthScale = outWidth * 1f / width;
  55. float heightScale = outHeight * 1f / height;
  56. Matrix matrix = new Matrix();
  57. matrix.setScale(widthScale, heightScale);
  58. //创建输出的bitmap
  59. Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
  60. //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上
  61. Canvas canvas = new Canvas(desBitmap);
  62. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  63. //创建着色器
  64. BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  65. //给着色器配置matrix
  66. bitmapShader.setLocalMatrix(matrix);
  67. paint.setShader(bitmapShader);
  68. //创建矩形区域并且预留出border
  69. RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
  70. //把传入的bitmap绘制到圆角矩形区域内
  71. canvas.drawRoundRect(rect, radius, radius, paint);
  72. if (boarder > 0) {
  73. //绘制boarder
  74. Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  75. boarderPaint.setColor(Color.GREEN);
  76. boarderPaint.setStyle(Paint.Style.STROKE);
  77. boarderPaint.setStrokeWidth(boarder);
  78. canvas.drawRoundRect(rect, radius, radius, boarderPaint);
  79. }
  80. return desBitmap;
  81. }
  82. }
方法三

对图片进行处理,还可以加边框;

  1. /**
  2. * 通过BitmapShader实现圆形边框
  3. * @param bitmap
  4. * @param outWidth 输出的图片宽度
  5. * @param outHeight 输出的图片高度
  6. * @param radius 圆角大小
  7. * @param boarder 边框宽度
  8. */
  9. public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
  10. if (bitmap == null) {
  11. return null;
  12. }
  13. int height = bitmap.getHeight();
  14. int width = bitmap.getWidth();
  15. float widthScale = outWidth * 1f / width;
  16. float heightScale = outHeight * 1f / height;
  17. Matrix matrix = new Matrix();
  18. matrix.setScale(widthScale, heightScale);
  19. //创建输出的bitmap
  20. Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
  21. //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上
  22. Canvas canvas = new Canvas(desBitmap);
  23. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  24. //创建着色器
  25. BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  26. //给着色器配置matrix
  27. bitmapShader.setLocalMatrix(matrix);
  28. paint.setShader(bitmapShader);
  29. //创建矩形区域并且预留出border
  30. RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
  31. //把传入的bitmap绘制到圆角矩形区域内
  32. canvas.drawRoundRect(rect, radius, radius, paint);
  33. if (boarder > 0) {
  34. //绘制boarder
  35. Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  36. boarderPaint.setColor(Color.GREEN);
  37. boarderPaint.setStyle(Paint.Style.STROKE);
  38. boarderPaint.setStrokeWidth(boarder);
  39. canvas.drawRoundRect(rect, radius, radius, boarderPaint);
  40. }
  41. return desBitmap;
  42. }

实现圆形和边框:

  1. /**
  2. * 通过BitmapShader实现圆形边框
  3. * @param bitmap
  4. * @param outWidth 输出的图片宽度
  5. * @param outHeight 输出的图片高度
  6. * @param boarder 边框大小
  7. */
  8. public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {
  9. int radius;
  10. int width = bitmap.getWidth();
  11. int height = bitmap.getHeight();
  12. float widthScale = outWidth * 1f / width;
  13. float heightScale = outHeight * 1f / height;
  14. Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
  15. if (outHeight > outWidth) {
  16. radius = outWidth / 2;
  17. } else {
  18. radius = outHeight / 2;
  19. }
  20. //创建canvas
  21. Canvas canvas = new Canvas(desBitmap);
  22. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  23. BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  24. Matrix matrix = new Matrix();
  25. matrix.setScale(widthScale, heightScale);
  26. bitmapShader.setLocalMatrix(matrix);
  27. paint.setShader(bitmapShader);
  28. canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, paint);
  29. if (boarder > 0) {
  30. //绘制boarder
  31. Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  32. boarderPaint.setColor(Color.GREEN);
  33. boarderPaint.setStyle(Paint.Style.STROKE);
  34. boarderPaint.setStrokeWidth(boarder);
  35. canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);
  36. }
  37. return desBitmap;
  38. }

原文链接:http://www.cnblogs.com/herokevin/p/15811691.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号