首页 > 代码库 > android 圆角ImageView类,可设置弧度

android 圆角ImageView类,可设置弧度

  1 public class RoundImageView extends ImageView {  2     private Paint paint;  3     private int roundWidth = 50;  4     private int roundHeight = 50;  5     private Paint paint2;  6   7     public RoundImageView(Context context, AttributeSet attrs, int defStyle) {  8         super(context, attrs, defStyle);  9         init(context, attrs); 10     } 11  12     public RoundImageView(Context context, AttributeSet attrs) { 13         super(context, attrs); 14         init(context, attrs); 15     } 16  17     public void SetRoundValue(float roundValue) { 18         roundWidth = (int) roundValue; 19         roundHeight = (int) roundValue; 20     } 21  22     public RoundImageView(Context context) { 23         super(context); 24         init(context, null); 25     } 26  27     @SuppressLint("Recycle") 28     private void init(Context context, AttributeSet attrs) { 29  30         if (attrs != null) { 31             TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView); 32             roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth); 33             roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight); 34         } else { 35             float density = context.getResources().getDisplayMetrics().density; 36             roundWidth = (int) (roundWidth * density); 37             roundHeight = (int) (roundHeight * density); 38         } 39  40         paint = new Paint(); 41         paint.setColor(Color.WHITE); 42         paint.setAntiAlias(true); 43         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); 44  45         paint2 = new Paint(); 46         paint2.setXfermode(null); 47     } 48  49     @Override 50     public void draw(Canvas canvas) { 51         Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); 52         Canvas canvas2 = new Canvas(bitmap); 53         super.draw(canvas2); 54         drawLiftUp(canvas2); 55         drawRightUp(canvas2); 56         drawLiftDown(canvas2); 57         drawRightDown(canvas2); 58         canvas.drawBitmap(bitmap, 0, 0, paint2); 59         bitmap.recycle(); 60         bitmap = null; 61     } 62  63     private void drawLiftUp(Canvas canvas) { 64         Path path = new Path(); 65         path.moveTo(0, roundHeight); 66         path.lineTo(0, 0); 67         path.lineTo(roundWidth, 0); 68         path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90); 69         path.close(); 70         canvas.drawPath(path, paint); 71     } 72  73     private void drawLiftDown(Canvas canvas) { 74         Path path = new Path(); 75         path.moveTo(0, getHeight() - roundHeight); 76         path.lineTo(0, getHeight()); 77         path.lineTo(roundWidth, getHeight()); 78         path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90); 79         path.close(); 80         canvas.drawPath(path, paint); 81     } 82  83     private void drawRightDown(Canvas canvas) { 84         Path path = new Path(); 85         path.moveTo(getWidth() - roundWidth, getHeight()); 86         path.lineTo(getWidth(), getHeight()); 87         path.lineTo(getWidth(), getHeight() - roundHeight); 88         path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0, 89                 90); 90         path.close(); 91         canvas.drawPath(path, paint); 92     } 93  94     private void drawRightUp(Canvas canvas) { 95         Path path = new Path(); 96         path.moveTo(getWidth(), roundHeight); 97         path.lineTo(getWidth(), 0); 98         path.lineTo(getWidth() - roundWidth, 0); 99         path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);100         path.close();101         canvas.drawPath(path, paint);102     }103 104 }

 

android 圆角ImageView类,可设置弧度