Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. public class RoundedCornerLayout extends FrameLayout {
  2. private float CORNER_RADIUS = 2.0f;
  3.  
  4. private Bitmap maskBitmap;
  5. private Paint paint, maskPaint;
  6. private float cornerRadius;
  7.  
  8. public RoundedCornerLayout(Context context) {
  9. super(context);
  10. init(context, null, 0);
  11. }
  12.  
  13. public RoundedCornerLayout(Context context, AttributeSet attrs) {
  14. super(context, attrs);
  15. init(context, attrs, 0);
  16. }
  17.  
  18. public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
  19. super(context, attrs, defStyle);
  20. init(context, attrs, defStyle);
  21. }
  22.  
  23. private void init(Context context, AttributeSet attrs, int defStyle) {
  24. DisplayMetrics metrics = context.getResources().getDisplayMetrics();
  25. cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
  26.  
  27. paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  28.  
  29. maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
  30. maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
  31.  
  32. setWillNotDraw(false);
  33. }
  34.  
  35. @Override
  36. public void draw(Canvas canvas) {
  37. Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
  38. Canvas offscreenCanvas = new Canvas(offscreenBitmap);
  39.  
  40. super.draw(offscreenCanvas);
  41.  
  42. if (maskBitmap == null) {
  43. maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
  44. }
  45.  
  46. offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
  47. canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
  48. }
  49.  
  50. private Bitmap createMask(int width, int height) {
  51. Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
  52. Canvas canvas = new Canvas(mask);
  53.  
  54. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  55. paint.setColor(Color.WHITE);
  56.  
  57. canvas.drawRect(0, 0, width, height, paint);
  58.  
  59. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
  60. canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);
  61.  
  62. return mask;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement