Advertisement
thomas2411

Android RoundedImageView

Dec 2nd, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package com.text;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Path;
  6. import android.graphics.RectF;
  7. import android.util.AttributeSet;
  8.  
  9. public class RoundedImageView extends ImageView
  10. {
  11.  
  12.     float radius = 10.0f; // angle of round corners
  13.     Path clipPath;
  14.     RectF rect;
  15.    
  16.     public RoundedImageView(Context context)
  17.     {
  18.         this(context, null);
  19.         clipPath = new Path();
  20.         rect = new RectF();
  21.     }
  22.  
  23.     public RoundedImageView(Context context, AttributeSet attrs) {
  24.             this(context, attrs, 0);
  25.             clipPath = new Path();
  26.             rect = new RectF();
  27.     }
  28.  
  29.     public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
  30.             super(context, attrs, defStyle);
  31.             clipPath = new Path();
  32.             rect = new RectF();
  33.     }
  34.    
  35.     @Override
  36.     protected void onDraw(Canvas canvas) {
  37.             rect.set(0, 0, this.getWidth(), this.getHeight());
  38.             clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
  39.             canvas.clipPath(clipPath);
  40.             super.onDraw(canvas);
  41.     }    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement