Android 实现圆角ImageView

先上效果图:


实现方法一:

[java]  view plain  copy
  1. /** 
  2.  * 获取圆角位图的方法 
  3.  *  
  4.  * @param bitmap 
  5.  *            需要转化成圆角的位图 
  6.  * @param pixels 
  7.  *            圆角的度数,数值越大,圆角越大 
  8.  * @return 处理后的圆角位图 
  9.  */  
  10. public static Bitmap toRoundCornerImage(Bitmap bitmap, int pixels) {  
  11.     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
  12.     Canvas canvas = new Canvas(output);  
  13.     final int color = 0xff424242;  
  14.     final Paint paint = new Paint();  
  15.     final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
  16.     final RectF rectF = new RectF(rect);  
  17.     final float roundPx = pixels;  
  18.     // 抗锯齿  
  19.     paint.setAntiAlias(true);  
  20.     canvas.drawARGB(0000);  
  21.     paint.setColor(color);  
  22.     canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  23.     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  24.     canvas.drawBitmap(bitmap, rect, rect, paint);  
  25.     return output;  
  26. }  

实现方法二:

[java]  view plain  copy
  1. package com.loopfire.meitaotao.view.roundImageView;  
  2.   
  3. import com.loopfire.meitaotao.R;  
  4.   
  5. import android.content.Context;  
  6. import android.content.res.TypedArray;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.Bitmap.Config;  
  9. import android.graphics.Canvas;  
  10. import android.graphics.Color;  
  11. import android.graphics.Paint;  
  12. import android.graphics.Path;  
  13. import android.graphics.PorterDuff;  
  14. import android.graphics.PorterDuffXfermode;  
  15. import android.graphics.RectF;  
  16. import android.util.AttributeSet;  
  17. import android.widget.ImageView;  
  18.   
  19. /** 
  20.  * 实现圆角image 
  21.  *  
  22.  * @author Administrator 
  23.  *  
  24.  */  
  25. public class RoundAngleImageView extends ImageView {  
  26.   
  27.     private Paint paint;  
  28.     /** 
  29.      * 个人理解是 
  30.      *  
  31.      * 这两个都是画圆的半径 
  32.      */  
  33.     private int roundWidth = 20;  
  34.     private int roundHeight = 20;  
  35.     private Paint paint2;  
  36.   
  37.     public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {  
  38.         super(context, attrs, defStyle);  
  39.         init(context, attrs);  
  40.     }  
  41.   
  42.     public RoundAngleImageView(Context context, AttributeSet attrs) {  
  43.         super(context, attrs);  
  44.         init(context, attrs);  
  45.     }  
  46.   
  47.     public RoundAngleImageView(Context context) {  
  48.         super(context);  
  49.         init(context, null);  
  50.     }  
  51.   
  52.     private void init(Context context, AttributeSet attrs) {  
  53.   
  54.         if (attrs != null) {  
  55.             TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);  
  56.             roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);  
  57.             roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);  
  58.         } else {  
  59.             float density = context.getResources().getDisplayMetrics().density;  
  60.             roundWidth = (int) (roundWidth * density);  
  61.             roundHeight = (int) (roundHeight * density);  
  62.         }  
  63.   
  64.         paint = new Paint();  
  65.         paint.setColor(Color.WHITE);  
  66.         paint.setAntiAlias(true);  
  67.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));  
  68.   
  69.         paint2 = new Paint();  
  70.         paint2.setXfermode(null);  
  71.     }  
  72.   
  73.     @Override  
  74.     public void draw(Canvas canvas) {  
  75.         Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);  
  76.         Canvas canvas2 = new Canvas(bitmap);  
  77.         super.draw(canvas2);  
  78.         drawLiftUp(canvas2);  
  79.         drawLiftDown(canvas2);  
  80.         drawRightUp(canvas2);  
  81.         drawRightDown(canvas2);  
  82.         canvas.drawBitmap(bitmap, 00, paint2);  
  83.         bitmap.recycle();  
  84.     }  
  85.   
  86.     private void drawLiftUp(Canvas canvas) {  
  87.         Path path = new Path();  
  88.         path.moveTo(0, roundHeight);  
  89.         path.lineTo(00);  
  90.         path.lineTo(roundWidth, 0);  
  91.         path.arcTo(new RectF(00, roundWidth * 2, roundHeight * 2), -90, -90);  
  92.         path.close();  
  93.         canvas.drawPath(path, paint);  
  94.     }  
  95.   
  96.     private void drawLiftDown(Canvas canvas) {  
  97.         Path path = new Path();  
  98.         path.moveTo(0, getHeight() - roundHeight);  
  99.         path.lineTo(0, getHeight());  
  100.         path.lineTo(roundWidth, getHeight());  
  101.         path.arcTo(new RectF(0, getHeight() - roundHeight * 2, roundWidth * 2, getHeight()), 9090);  
  102.         path.close();  
  103.         canvas.drawPath(path, paint);  
  104.     }  
  105.   
  106.     private void drawRightDown(Canvas canvas) {  
  107.         Path path = new Path();  
  108.         path.moveTo(getWidth() - roundWidth, getHeight());  
  109.         path.lineTo(getWidth(), getHeight());  
  110.         path.lineTo(getWidth(), getHeight() - roundHeight);  
  111.         path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), -090);  
  112.         path.close();  
  113.         canvas.drawPath(path, paint);  
  114.     }  
  115.   
  116.     private void drawRightUp(Canvas canvas) {  
  117.         Path path = new Path();  
  118.         path.moveTo(getWidth(), roundHeight);  
  119.         path.lineTo(getWidth(), 0);  
  120.         path.lineTo(getWidth() - roundWidth, 0);  
  121.         path.arcTo(new RectF(getWidth() - roundWidth * 20, getWidth(), 0 + roundHeight * 2), -9090);  
  122.         path.close();  
  123.         canvas.drawPath(path, paint);  
  124.     }  
  125.   
  126. }  


然后在values下建个xml文件attribute.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="RoundAngleImageView">  
  5.         <attr name="roundWidth" format="dimension" />  
  6.         <attr name="roundHeight" format="dimension" />  
  7.     </declare-styleable>  
  8.   
  9. </resources>  




最后:
首先说明:两种方法都有一个相同点,重写了onDraw方法,方法里面都是采用重绘bitmap的方式实现圆角Image

第二种方法网上有很多相似例子,不过发现它们都有一个错误,就是左下角不能实现圆角,我的第二个方法就是

对网上的错误方法修改而成!

猜你喜欢

转载自blog.csdn.net/weixin_42354735/article/details/80528025