Skip to content

Instantly share code, notes, and snippets.

@ElFeesho
Created December 18, 2015 21:53
Show Gist options
  • Select an option

  • Save ElFeesho/9e85af387af7cf66be1d to your computer and use it in GitHub Desktop.

Select an option

Save ElFeesho/9e85af387af7cf66be1d to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ProfileImageView extends ImageView {
private Matrix matrix = null;
public ProfileImageView(Context context) {
this(context, null, 0);
}
public ProfileImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProfileImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
matrix = null;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (matrix == null) {
matrix = new Matrix();
float sx = (getMeasuredWidth() / (float)getDrawable().getIntrinsicWidth());
matrix.setScale(sx, sx);
setImageMatrix(matrix);
}
canvas.setMatrix(matrix);
canvas.save();
canvas.translate(0, -getMeasuredHeight()/2);
getDrawable().draw(canvas);
canvas.restore();
canvas.drawColor(0x880088ff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment