android开源项目---RoundedImageView制作圆角矩形,椭圆形以其圆形控件 一、项目概述

一、项目概述

在项目中美工提供给我们的图片通常是矩形的,我们如何把矩形的图片做成圆角矩形、椭圆形以其圆形效果的图形显示效果,这就是我这篇文章要讲的内容。

效果如下:



我这里使用的是开源项目  RoundedImageView

 github上面的开源项目 官方地址为:   https://github.com/vinc3m1/RoundedImageView 



二、搭建RoundedImageView的使用环境

下载后,我们得到RoundedImageView文件夹。

将RoundedImageView\roundedimageview\src\main\java\com\makeramen下的三个java文件复制到自己项目中。这是会出现一些错误。

1、直接复制java文件导致包引入的错误,这个只需要修改导入的包名即可。

2、RoundedImageView依赖于picasso-2.3.4.jar,这个需要下载放在制剂项目的libs目录下。下载地址为点击打开链接

3、缺少attrs.xml文件,将\RoundedImageView\roundedimageview\src\main\res\values\attrs.xml复制到自己项目的values目录下。


按照要求排版后,项目结构大致如下:



注意必要文件是 libs下面的picasso.jar  , values下面的attrs.xml  、和view包下面的三个java文件(RoundedDrawable,RoundedImageView和RoundedTransformationBuilder)



三、使用RoundedImageView

这里主要是要注意它的几个自定义属性的作用 。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 圆角矩形 椭圆的自定义属性
  4. corner_radius : 边角弧度。这个值越大,圆角越明显
  5. border_width : 边框宽度
  6. border_color : 边框颜色
  7. mutate_background : 背景是否变化(这里在demo中改变不明显)
  8. oval : 是否以椭圆形式显示 true为以椭圆形式显示 flase以矩形形式显示
  9. -->
  10. <declare-styleable name="RoundedImageView">
  11. <attr name="corner_radius" format="dimension" />
  12. <attr name="border_width" format="dimension" />
  13. <attr name="border_color" format="color" />
  14. <attr name="mutate_background" format="boolean" />
  15. <attr name="oval" format="boolean" />
  16. <attr name="android:scaleType" />
  17. </declare-styleable>
  18. </resources>


本项目的layout.xml为

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app= "http://schemas.android.com/apk/res-auto"
  4. android:layout_width= "match_parent"
  5. android:layout_height= "match_parent"
  6. android:background= "@color/grey"
  7. android:orientation= "vertical" >
  8. <ScrollView
  9. android:layout_width= "match_parent"
  10. android:layout_height= "match_parent">
  11. <LinearLayout
  12. android:layout_width= "match_parent"
  13. android:layout_height= "wrap_content"
  14. android:orientation= "vertical" >
  15. <RelativeLayout
  16. android:layout_width= "match_parent"
  17. android:layout_height= "0dp"
  18. android:layout_weight= "1"
  19. android:background= "@color/light" >
  20. <edu.njupt.javer.view.RoundedImageView
  21. android:layout_width= "wrap_content"
  22. android:layout_height= "wrap_content"
  23. android:layout_centerInParent= "true"
  24. android:padding= "10dip"
  25. android:scaleType= "center"
  26. android:src= "@drawable/mm1"
  27. app:border_color= "@color/dark"
  28. app:border_width= "3dip"
  29. app:corner_radius= "30dip"
  30. app:mutate_background= "true"
  31. app:oval= "false" />
  32. </RelativeLayout>
  33. <RelativeLayout
  34. android:layout_width= "match_parent"
  35. android:layout_height= "0dp"
  36. android:layout_weight= "1"
  37. android:background= "@color/dark" >
  38. <edu.njupt.javer.view.RoundedImageView
  39. android:layout_width= "wrap_content"
  40. android:layout_height= "wrap_content"
  41. android:layout_centerInParent= "true"
  42. android:padding= "10dip"
  43. android:scaleType= "center"
  44. android:src= "@drawable/mm2"
  45. app:border_color= "@color/light"
  46. app:border_width= "3dip"
  47. app:corner_radius= "30dip"
  48. app:mutate_background= "true"
  49. app:oval= "true" />
  50. </RelativeLayout>
  51. <RelativeLayout
  52. android:layout_width= "match_parent"
  53. android:layout_height= "0dp"
  54. android:layout_weight= "1"
  55. android:background= "@color/light" >
  56. <edu.njupt.javer.view.RoundedImageView
  57. android:layout_width= "160dp"
  58. android:layout_height= "160dp"
  59. android:layout_centerInParent= "true"
  60. android:padding= "10dip"
  61. android:scaleType= "center"
  62. android:src= "@drawable/mm3"
  63. app:border_color= "@color/dark"
  64. app:border_width= "3dip"
  65. app:corner_radius= "30dip"
  66. app:mutate_background= "true"
  67. app:oval= "true" />
  68. </RelativeLayout>
  69. </LinearLayout>
  70. </ScrollView>
  71. </LinearLayout>

四、项目下载地址:

点击打开链接


五、补充

除了在xml中设置属性外,RoundedImageView也可以在代码中设置属性。

Define in xml:

 
    
  1. <com.makeramen.RoundedImageView
  2. xmlns:app= "http://schemas.android.com/apk/res-auto"
  3. android:id= "@+id/imageView1"
  4. android:src= "@drawable/photo1"
  5. android:scaleType= "centerCrop"
  6. app:corner_radius= "30dip"
  7. app:border_width= "2dip"
  8. app:border_color= "#333333"
  9. app:mutate_background= "true"
  10. app:oval= "true" />

Or in code:

 
    
  1. RoundedImageView iv = new RoundedImageView(context);
  2. iv.setScaleType(ScaleType.CENTER_CROP);
  3. iv.setCornerRadius( 10);
  4. iv.setBorderWidth( 2);
  5. iv.setBorderColor(Color.DKGRAY);
  6. iv.setMutateBackground( true);
  7. iv.setImageDrawable(drawable);
  8. iv.setBackground(backgroundDrawable);
  9. iv.setOval( true);

Or make a Transformation for Picasso:

  1. Transformation transformation = new RoundedTransformationBuilder()
  2. .borderColor(Color.BLACK)
  3. .borderWidthDp( 3)
  4. .cornerRadiusDp( 30)
  5. .oval( false)
  6. .build();
  7. Picasso.with(context)
  8. .load(url)
  9. .fit()
  10. .transform(transformation)
  11. .into(imageView);




在项目中美工提供给我们的图片通常是矩形的,我们如何把矩形的图片做成圆角矩形、椭圆形以其圆形效果的图形显示效果,这就是我这篇文章要讲的内容。

效果如下:



我这里使用的是开源项目  RoundedImageView

 github上面的开源项目 官方地址为:   https://github.com/vinc3m1/RoundedImageView 



二、搭建RoundedImageView的使用环境

下载后,我们得到RoundedImageView文件夹。

将RoundedImageView\roundedimageview\src\main\java\com\makeramen下的三个java文件复制到自己项目中。这是会出现一些错误。

1、直接复制java文件导致包引入的错误,这个只需要修改导入的包名即可。

2、RoundedImageView依赖于picasso-2.3.4.jar,这个需要下载放在制剂项目的libs目录下。下载地址为点击打开链接

3、缺少attrs.xml文件,将\RoundedImageView\roundedimageview\src\main\res\values\attrs.xml复制到自己项目的values目录下。


按照要求排版后,项目结构大致如下:



注意必要文件是 libs下面的picasso.jar  , values下面的attrs.xml  、和view包下面的三个java文件(RoundedDrawable,RoundedImageView和RoundedTransformationBuilder)



三、使用RoundedImageView

这里主要是要注意它的几个自定义属性的作用 。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 圆角矩形 椭圆的自定义属性
  4. corner_radius : 边角弧度。这个值越大,圆角越明显
  5. border_width : 边框宽度
  6. border_color : 边框颜色
  7. mutate_background : 背景是否变化(这里在demo中改变不明显)
  8. oval : 是否以椭圆形式显示 true为以椭圆形式显示 flase以矩形形式显示
  9. -->
  10. <declare-styleable name="RoundedImageView">
  11. <attr name="corner_radius" format="dimension" />
  12. <attr name="border_width" format="dimension" />
  13. <attr name="border_color" format="color" />
  14. <attr name="mutate_background" format="boolean" />
  15. <attr name="oval" format="boolean" />
  16. <attr name="android:scaleType" />
  17. </declare-styleable>
  18. </resources>


本项目的layout.xml为

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app= "http://schemas.android.com/apk/res-auto"
  4. android:layout_width= "match_parent"
  5. android:layout_height= "match_parent"
  6. android:background= "@color/grey"
  7. android:orientation= "vertical" >
  8. <ScrollView
  9. android:layout_width= "match_parent"
  10. android:layout_height= "match_parent">
  11. <LinearLayout
  12. android:layout_width= "match_parent"
  13. android:layout_height= "wrap_content"
  14. android:orientation= "vertical" >
  15. <RelativeLayout
  16. android:layout_width= "match_parent"
  17. android:layout_height= "0dp"
  18. android:layout_weight= "1"
  19. android:background= "@color/light" >
  20. <edu.njupt.javer.view.RoundedImageView
  21. android:layout_width= "wrap_content"
  22. android:layout_height= "wrap_content"
  23. android:layout_centerInParent= "true"
  24. android:padding= "10dip"
  25. android:scaleType= "center"
  26. android:src= "@drawable/mm1"
  27. app:border_color= "@color/dark"
  28. app:border_width= "3dip"
  29. app:corner_radius= "30dip"
  30. app:mutate_background= "true"
  31. app:oval= "false" />
  32. </RelativeLayout>
  33. <RelativeLayout
  34. android:layout_width= "match_parent"
  35. android:layout_height= "0dp"
  36. android:layout_weight= "1"
  37. android:background= "@color/dark" >
  38. <edu.njupt.javer.view.RoundedImageView
  39. android:layout_width= "wrap_content"
  40. android:layout_height= "wrap_content"
  41. android:layout_centerInParent= "true"
  42. android:padding= "10dip"
  43. android:scaleType= "center"
  44. android:src= "@drawable/mm2"
  45. app:border_color= "@color/light"
  46. app:border_width= "3dip"
  47. app:corner_radius= "30dip"
  48. app:mutate_background= "true"
  49. app:oval= "true" />
  50. </RelativeLayout>
  51. <RelativeLayout
  52. android:layout_width= "match_parent"
  53. android:layout_height= "0dp"
  54. android:layout_weight= "1"
  55. android:background= "@color/light" >
  56. <edu.njupt.javer.view.RoundedImageView
  57. android:layout_width= "160dp"
  58. android:layout_height= "160dp"
  59. android:layout_centerInParent= "true"
  60. android:padding= "10dip"
  61. android:scaleType= "center"
  62. android:src= "@drawable/mm3"
  63. app:border_color= "@color/dark"
  64. app:border_width= "3dip"
  65. app:corner_radius= "30dip"
  66. app:mutate_background= "true"
  67. app:oval= "true" />
  68. </RelativeLayout>
  69. </LinearLayout>
  70. </ScrollView>
  71. </LinearLayout>

四、项目下载地址:

点击打开链接


五、补充

除了在xml中设置属性外,RoundedImageView也可以在代码中设置属性。

Define in xml:

 
  
  1. <com.makeramen.RoundedImageView
  2. xmlns:app= "http://schemas.android.com/apk/res-auto"
  3. android:id= "@+id/imageView1"
  4. android:src= "@drawable/photo1"
  5. android:scaleType= "centerCrop"
  6. app:corner_radius= "30dip"
  7. app:border_width= "2dip"
  8. app:border_color= "#333333"
  9. app:mutate_background= "true"
  10. app:oval= "true" />

Or in code:

 
  
  1. RoundedImageView iv = new RoundedImageView(context);
  2. iv.setScaleType(ScaleType.CENTER_CROP);
  3. iv.setCornerRadius( 10);
  4. iv.setBorderWidth( 2);
  5. iv.setBorderColor(Color.DKGRAY);
  6. iv.setMutateBackground( true);
  7. iv.setImageDrawable(drawable);
  8. iv.setBackground(backgroundDrawable);
  9. iv.setOval( true);

Or make a Transformation for Picasso:

  1. Transformation transformation = new RoundedTransformationBuilder()
  2. .borderColor(Color.BLACK)
  3. .borderWidthDp( 3)
  4. .cornerRadiusDp( 30)
  5. .oval( false)
  6. .build();
  7. Picasso.with(context)
  8. .load(url)
  9. .fit()
  10. .transform(transformation)
  11. .into(imageView);



猜你喜欢

转载自blog.csdn.net/daimengs/article/details/80943865