Skip to content

Instantly share code, notes, and snippets.

@korniltsev
Created September 8, 2016 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korniltsev/498d7b0687dfced86964ba6e232dee54 to your computer and use it in GitHub Desktop.
Save korniltsev/498d7b0687dfced86964ba6e232dee54 to your computer and use it in GitHub Desktop.
CenterCropDrawable
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
public class CenterCropDrawable extends Drawable {
private final Drawable drawable;
public CenterCropDrawable(Drawable drawable) {
this.drawable = drawable;
}
@Override
public void draw(Canvas canvas) {
drawable.draw(canvas);
}
@Override
public void setAlpha(int alpha) {
drawable.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
drawable.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return drawable.getOpacity();
}
@Override
protected void onBoundsChange(Rect bounds) {
final int vwidth = bounds.width();
final int vheight = bounds.height();
final int dwidth = drawable.getIntrinsicWidth();
final int dheight = drawable.getIntrinsicHeight();
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
int l = (int) dx;
int t = (int) dy;
int r = (int) (l + (dwidth * scale));
int b = (int) (t + (dheight * scale));
drawable.setBounds(l, t, r, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment