package com.stanfy.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

/**
 * Image view that slightly extends possibilities of a standard image view.
 * <p>
 *   Features:
 *   <ul>
 *     <li><b>rounded corners</b> - set cornersRadius > 0 (attribute {@code android:radius})</li>
 *   </ul>
 * </p>
 * @author Roman Mazur - Stanfy (http://www.stanfy.com)
 */
public class ImageView extends android.widget.ImageView {

  /** Round corners flag. */
  private int cornersRadius;

  /** Help color. */
  private static final int CORNERS_HELP_COLOR = 0xff434343;
  /** Xfermode. */
  private static final Xfermode XFERMODE = new PorterDuffXfermode(Mode.SRC_IN);

  /** Paint. */
  private final Paint paint = new Paint();

  public ImageView(final Context context) {
    super(context);
    init(context, null);
  }

  public ImageView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ImageView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    init(context, attrs);
  }

  private void init(final Context context, final AttributeSet attrs) {
    paint.setAntiAlias(true);
    paint.setColor(CORNERS_HELP_COLOR);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageView);
    cornersRadius = a.getDimensionPixelSize(R.styleable.ImageView_android_radius, 0);
    a.recycle();
  }

  /**
   * Set a radius of corners when drawing a bitmap image. Works for {@link BitmapDrawable} only.
   * @param r radius to set
   */
  public void setCornersRadius(final int r) { this.cornersRadius = r; }

  @Override
  protected void onDraw(final Canvas canvas) {
    final Drawable d = getDrawable();
    if (d == null) { return; }
    final int r = cornersRadius;
    if (r > 0 && d instanceof BitmapDrawable) {

      final int pl = getPaddingLeft(), pt = getPaddingTop();
      final int resultW = getMeasuredWidth() - pl - getPaddingRight(), resultH = getMeasuredHeight() - pt - getPaddingBottom();
      if (resultW <= 0 || resultH <= 0) {
        super.onDraw(canvas);
        return;
      }

      final Bitmap bitmap = Bitmap.createBitmap(resultW, resultH, Bitmap.Config.ARGB_8888);
      final Canvas c = new Canvas(bitmap);
      final RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

      c.drawARGB(0, 0, 0, 0);
      final Paint p = paint;
      p.setXfermode(null);
      c.drawRoundRect(rectF, r, r, p);
      p.setXfermode(XFERMODE);
      c.drawBitmap(((BitmapDrawable)d).getBitmap(), null, rectF, p);

      final Rect finalRect = new Rect(pl, pt, pl + bitmap.getWidth(), pt + bitmap.getHeight());
      canvas.drawBitmap(bitmap, null, finalRect, null);
      bitmap.recycle();
    } else {
      super.onDraw(canvas);
    }
  }

}

@@ -1176,24 +1176,113 @@
 Paint();%0D%0A%0D%0A
+  /** Buffer rectangle. */%0D%0A  private static final RectF BUFFER_RECT_F = new RectF();%0D%0A%0D%0A
   public Ima
@@ -2201,16 +2201,759 @@
 r; %7D%0D%0A%0D%0A
+  private static RectF prepareSourceRect(final Bitmap source, final Bitmap destination) %7B%0D%0A    final int sw = source.getWidth(), sh = source.getHeight(), dw = destination.getWidth(), dh = destination.getHeight();%0D%0A    if (sw %3E= dw %7C%7C sh %3E= dh) %7B // scaling / stretching%0D%0A      BUFFER_RECT_F.left = 0;%0D%0A      BUFFER_RECT_F.top = 0;%0D%0A      BUFFER_RECT_F.right = dw;%0D%0A      BUFFER_RECT_F.bottom = dh;%0D%0A    %7D else %7B // no scaling, center%0D%0A      final int topPadding = (dh - sh) %3E%3E 1;%0D%0A      final int leftPadding = (dw - sw) %3E%3E 1;%0D%0A      BUFFER_RECT_F.left = leftPadding;%0D%0A      BUFFER_RECT_F.top = topPadding;%0D%0A      BUFFER_RECT_F.right = leftPadding + sw;%0D%0A      BUFFER_RECT_F.bottom = topPadding + sh;%0D%0A    %7D%0D%0A    return BUFFER_RECT_F;%0D%0A  %7D%0D%0A%0D%0A
   @Overr
@@ -3450,24 +3450,86 @@
 %0A      %7D%0D%0A%0D%0A
+      final Bitmap source = ((BitmapDrawable)d).getBitmap();%0D%0A
       final 
@@ -3649,24 +3649,26 @@
 s(bitmap);%0D%0A
+%0D%0A
       final 
@@ -3685,65 +3685,42 @@
 F = 
-new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());%0D%0A
+prepareSourceRect(source, bitmap);
 %0D%0A  
@@ -3901,39 +3901,14 @@
 map(
-((BitmapDrawable)d).getBitmap()
+source
 , nu

Fix ImageView scaling