自定義頭像圓角控制元件

米蘭的小鎮發表於2016-11-01

很多時候,專案中頭像需要圓角處理,這時候我們考慮的就是自定義控制元件來實現圓角處理,程式碼如下



public class CircularImage extends MaskedImage {
 public CircularImage(Context paramContext) {
  super(paramContext);
 }
 public CircularImage(Context paramContext, AttributeSet paramAttributeSet) {
  super(paramContext, paramAttributeSet);
 }
 public CircularImage(Context paramContext, AttributeSet paramAttributeSet,
   int paramInt) {
  super(paramContext, paramAttributeSet, paramInt);
 }
 public Bitmap createMask() {
  int i = getWidth();
  int j = getHeight();
  Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
  Bitmap localBitmap = Bitmap.createBitmap(i, j, localConfig);
  Canvas localCanvas = new Canvas(localBitmap);
  Paint localPaint = new Paint(1);
  localPaint.setColor(-16777216);
  float f1 = getWidth();
  float f2 = getHeight();
  RectF localRectF = new RectF(0.0F, 0.0F, f1, f2);
  localCanvas.drawOval(localRectF, localPaint);
  return localBitmap;
 }
}

相關文章