Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CrownCircularImageView extends ImageView {
- private Bitmap crown;
- private Bitmap image;
- private Drawable drawable;
- private Paint paint;
- private Paint crownPaint;
- private int centerX, centerY;
- private float bitmapRadius;
- private int crownLeft, crownTop;
- public CrownCircularImageView(Context context) {
- this(context, null, 0);
- }
- public CrownCircularImageView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public CrownCircularImageView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(context, attrs, defStyleAttr);
- }
- private void init(Context context, AttributeSet attrs, int defStyleAttr) {
- paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- crownPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- crown = drawableToBitmap(
- ContextCompat.getDrawable(
- context,
- R.drawable.ic_account_balance_white_24dp),
- 50);
- }
- private void loadBitmap() {
- if (drawable == getDrawable()) {
- return;
- }
- drawable = getDrawable();
- image = drawableToBitmap(this.drawable, 0);
- updateImage();
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- if (w != oldw || h != oldh) {
- crownLeft = getWidth() - crown.getWidth();
- crownTop = 0;
- if (image != null) {
- updateImage();
- }
- }
- }
- private void updateImage() {
- centerX = getWidth() / 2;
- centerY = getHeight() / 2;
- bitmapRadius = Math.min(image.getWidth(), image.getHeight()) / 2f;
- updateShader();
- }
- private void updateShader() {
- if (image == null) {
- return;
- }
- BitmapShader shader = new BitmapShader(image,
- Shader.TileMode.CLAMP,
- Shader.TileMode.CLAMP);
- paint.setShader(shader);
- }
- private Bitmap drawableToBitmap(Drawable drawable, int scale) {
- if (drawable == null) {
- return null;
- } else if (drawable instanceof BitmapDrawable) {
- return ((BitmapDrawable) drawable).getBitmap();
- }
- int intrinsicWidth = drawable.getIntrinsicWidth();
- int intrinsicHeight = drawable.getIntrinsicHeight();
- if (!(intrinsicWidth > 0 && intrinsicHeight > 0))
- return null;
- try {
- Bitmap bitmap = Bitmap.createBitmap(
- intrinsicWidth + scale,
- intrinsicHeight + scale,
- Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
- drawable.draw(canvas);
- return bitmap;
- } catch (OutOfMemoryError e) {
- Log.e(getClass().toString(), "Encountered OutOfMemoryError while generating bitmap!");
- return null;
- }
- }
- @Override
- public void onDraw(Canvas canvas) {
- loadBitmap();
- if (image == null) {
- return;
- }
- canvas.drawCircle(centerX, centerY, bitmapRadius, paint);
- canvas.drawBitmap(crown, crownLeft, crownTop, crownPaint);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int defSize = 200;
- int width = measureSize(widthMeasureSpec, defSize);
- int height = measureSize(heightMeasureSpec, defSize);
- setMeasuredDimension(width, height);
- }
- @Override
- public ScaleType getScaleType() {
- return ScaleType.CENTER_CROP;
- }
- private int measureSize(int measureSpec, int def) {
- int result;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- if (specMode == MeasureSpec.EXACTLY) {
- result = specSize;
- } else if (specMode == MeasureSpec.AT_MOST) {
- result = Math.min(specSize, def);
- } else {
- result = def;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement