实现圆形图片

逃离我推掉我的手 2022-07-16 15:07 303阅读 0赞

SouthEast

实现原理:

  1. 得到原图的宽高,计算出圆心,取图片的短边为基准半径。
  2. 创建空白Bitmap,大小为基准半径的正方形。
  3. 位移原图,使其中心点和创建的空白图中心点重合。
  4. 利用Android的Paint绘制叠加图,只绘制重叠部分
  5. 效果完成。

    public class CircleImageView extends ImageView{

    1. public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
    2. super(context, attrs, defStyle);
    3. }
    4. public CircleImageView(Context context, AttributeSet attrs) {
    5. super(context, attrs);
    6. }
    7. public CircleImageView(Context context) {
    8. super(context);
    9. }
    10. @Override
    11. public void setBackgroundDrawable(Drawable background) {
    12. super.setBackgroundDrawable(getCircleDrawable(getResources(), background));
    13. }
    14. @Override
    15. public void setBackgroundResource(int resid) {
    16. //Don't worry, we don't need to override it,because it will be call
    17. //setBackgroundDrawable(Drawable background)
    18. super.setBackgroundResource(resid);
    19. }
    20. @Override
    21. public void setImageBitmap(Bitmap bm) {
    22. //Don't worry, we don't need to override it,because it will be call
    23. //setImageDrawable(Drawable drawable)
    24. super.setImageBitmap(bm);
    25. }
    26. @Override
    27. public void setImageDrawable(Drawable drawable) {
    28. super.setImageDrawable(getCircleDrawable(getResources(), drawable));
    29. }
    30. @Override
    31. public void setImageURI(Uri uri) {
    32. //cheat it, let's change the way to implement
    33. super.setImageURI(uri);
    34. Drawable img = getCircleDrawable(getResources(), getDrawable());
    35. super.setImageDrawable(img);
    36. }
    37. @Override
    38. public void setImageResource(int resId) {
    39. //cheat it, let's change the way to implement
    40. Drawable img = getCircleDrawable(getResources(), resId);
    41. super.setImageDrawable(img);
    42. }
    43. private static final int SPACING_LINE = 2;
    44. private static Paint mCirclePaint = null;
    45. private static Paint mLinePaint = null;
    46. private static Paint getCirclePaint(){
    47. if(mCirclePaint == null){
    48. mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    49. }
    50. return mCirclePaint;
    51. }
    52. private static Paint getLinePaint(){
    53. if(mLinePaint == null){
    54. mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    55. mLinePaint.setStyle(Style.STROKE);
    56. //You can use it to change the width of the line
    57. mLinePaint.setStrokeWidth(1);
    58. //You can use it to change the color of the line
    59. mLinePaint.setColor(Color.BLACK);
    60. }
    61. return mLinePaint;
    62. }
    63. /** * You can call this method to generate the circular bitmap, * even if you don't use this class */
    64. public static Bitmap getCircleBitmap(Bitmap src){
    65. if(src == null){
    66. return null;
    67. }
    68. int width = src.getWidth();
    69. int height = src.getHeight();
    70. int centerX = width / 2;
    71. int centerY = height / 2;
    72. int radius = Math.min(centerX, centerY) / 2;
    73. Bitmap result = Bitmap.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);
    74. Canvas canvas = new Canvas(result);
    75. canvas.drawCircle(radius, radius, radius - SPACING_LINE, getCirclePaint());
    76. getCirclePaint().setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    77. canvas.drawBitmap(src, -(centerX - radius), -(centerY - radius), getCirclePaint());
    78. //outer
    79. canvas.drawCircle(radius, radius, radius, getLinePaint());
    80. //inner
    81. canvas.drawCircle(radius, radius, radius - SPACING_LINE, getLinePaint());
    82. //reset
    83. getCirclePaint().setXfermode(null);
    84. //recycle
    85. src.recycle();
    86. return result;
    87. }
    88. public static Bitmap getCircleBitmap(Drawable src){
    89. if(src instanceof BitmapDrawable){
    90. return getCircleBitmap(((BitmapDrawable)src).getBitmap());
    91. }else{
    92. //now, i don't know how to do...
    93. throw new UnsupportedException("Unsupported");
    94. }
    95. }
    96. public static Bitmap getCircleBitmap(Resources res,int id){
    97. return getCircleBitmap(BitmapFactory.decodeResource(res, id));
    98. }
    99. public static Drawable getCircleDrawable(Resources res, Bitmap src){
    100. return new BitmapDrawable(res,getCircleBitmap(src));
    101. }
    102. public static Drawable getCircleDrawable(Resources res, Drawable src){
    103. return new BitmapDrawable(res,getCircleBitmap(src));
    104. }
    105. public static Drawable getCircleDrawable(Resources res, int id) {
    106. return new BitmapDrawable(res, getCircleBitmap(res, id));
    107. }
    108. static class UnsupportedException extends RuntimeException{
    109. private static final long serialVersionUID = 1L;
    110. public UnsupportedException(String str){
    111. super(str);
    112. }
    113. }

    }

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读