Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. public class CrownCircularImageView extends ImageView {
  2.  
  3.     private Bitmap crown;
  4.     private Bitmap image;
  5.     private Drawable drawable;
  6.     private Paint paint;
  7.     private Paint crownPaint;
  8.  
  9.     private int centerX, centerY;
  10.     private float bitmapRadius;
  11.  
  12.     private int crownLeft, crownTop;
  13.  
  14.     public CrownCircularImageView(Context context) {
  15.         this(context, null, 0);
  16.     }
  17.  
  18.     public CrownCircularImageView(Context context, AttributeSet attrs) {
  19.         this(context, attrs, 0);
  20.     }
  21.  
  22.     public CrownCircularImageView(Context context, AttributeSet attrs, int defStyleAttr) {
  23.         super(context, attrs, defStyleAttr);
  24.         init(context, attrs, defStyleAttr);
  25.     }
  26.  
  27.     private void init(Context context, AttributeSet attrs, int defStyleAttr) {
  28.         paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  29.         crownPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  30.         crown = drawableToBitmap(
  31.                 ContextCompat.getDrawable(
  32.                         context,
  33.                         R.drawable.ic_account_balance_white_24dp),
  34.                 50);
  35.     }
  36.  
  37.     private void loadBitmap() {
  38.         if (drawable == getDrawable()) {
  39.             return;
  40.         }
  41.  
  42.         drawable = getDrawable();
  43.         image = drawableToBitmap(this.drawable, 0);
  44.         updateImage();
  45.     }
  46.  
  47.     @Override
  48.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  49.         super.onSizeChanged(w, h, oldw, oldh);
  50.         if (w != oldw || h != oldh) {
  51.             crownLeft = getWidth() - crown.getWidth();
  52.             crownTop = 0;
  53.             if (image != null) {
  54.                 updateImage();
  55.             }
  56.         }
  57.     }
  58.  
  59.     private void updateImage() {
  60.         centerX = getWidth() / 2;
  61.         centerY = getHeight() / 2;
  62.         bitmapRadius = Math.min(image.getWidth(), image.getHeight()) / 2f;
  63.         updateShader();
  64.     }
  65.  
  66.     private void updateShader() {
  67.         if (image == null) {
  68.             return;
  69.         }
  70.  
  71.         BitmapShader shader = new BitmapShader(image,
  72.                 Shader.TileMode.CLAMP,
  73.                 Shader.TileMode.CLAMP);
  74.         paint.setShader(shader);
  75.     }
  76.  
  77.     private Bitmap drawableToBitmap(Drawable drawable, int scale) {
  78.         if (drawable == null) {
  79.             return null;
  80.         } else if (drawable instanceof BitmapDrawable) {
  81.             return ((BitmapDrawable) drawable).getBitmap();
  82.         }
  83.  
  84.         int intrinsicWidth = drawable.getIntrinsicWidth();
  85.         int intrinsicHeight = drawable.getIntrinsicHeight();
  86.  
  87.         if (!(intrinsicWidth > 0 && intrinsicHeight > 0))
  88.             return null;
  89.  
  90.         try {
  91.             Bitmap bitmap = Bitmap.createBitmap(
  92.                     intrinsicWidth + scale,
  93.                     intrinsicHeight + scale,
  94.                     Bitmap.Config.ARGB_8888);
  95.             Canvas canvas = new Canvas(bitmap);
  96.             drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  97.             drawable.draw(canvas);
  98.             return bitmap;
  99.         } catch (OutOfMemoryError e) {
  100.             Log.e(getClass().toString(), "Encountered OutOfMemoryError while generating bitmap!");
  101.             return null;
  102.         }
  103.     }
  104.  
  105.     @Override
  106.     public void onDraw(Canvas canvas) {
  107.         loadBitmap();
  108.         if (image == null) {
  109.             return;
  110.         }
  111.  
  112.         canvas.drawCircle(centerX, centerY, bitmapRadius, paint);
  113.         canvas.drawBitmap(crown, crownLeft, crownTop, crownPaint);
  114.     }
  115.  
  116.     @Override
  117.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  118.         int defSize = 200;
  119.         int width = measureSize(widthMeasureSpec, defSize);
  120.         int height = measureSize(heightMeasureSpec, defSize);
  121.         setMeasuredDimension(width, height);
  122.     }
  123.  
  124.     @Override
  125.     public ScaleType getScaleType() {
  126.         return ScaleType.CENTER_CROP;
  127.     }
  128.  
  129.     private int measureSize(int measureSpec, int def) {
  130.         int result;
  131.         int specMode = MeasureSpec.getMode(measureSpec);
  132.         int specSize = MeasureSpec.getSize(measureSpec);
  133.         if (specMode == MeasureSpec.EXACTLY) {
  134.             result = specSize;
  135.         } else if (specMode == MeasureSpec.AT_MOST) {
  136.             result = Math.min(specSize, def);
  137.         } else {
  138.             result = def;
  139.         }
  140.  
  141.         return result;
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement