Created
July 29, 2016 11:37
-
-
Save ed-george/b94990d6c70df339b5799d1d4be982bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Outline; | |
import android.graphics.Paint; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffXfermode; | |
import android.graphics.RectF; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
import android.view.View; | |
import android.view.ViewOutlineProvider; | |
import android.widget.FrameLayout; | |
/** | |
* Created by edgeorge on 29/07/2016. | |
*/ | |
public class RoundedCornerLayout extends FrameLayout { | |
private final static float CORNER_RADIUS = 40.0f; | |
private Bitmap maskBitmap; | |
private Paint paint, maskPaint; | |
private float cornerRadius; | |
public RoundedCornerLayout(Context context) { | |
super(context); | |
init(context, null, 0); | |
} | |
public RoundedCornerLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs, 0); | |
} | |
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs, defStyle); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyle) { | |
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); | |
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics); | |
paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); | |
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | |
setWillNotDraw(false); | |
setOutlineProvider(new ButtonOutlineProvider()); | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas offscreenCanvas = new Canvas(offscreenBitmap); | |
super.draw(offscreenCanvas); | |
if (maskBitmap == null) { | |
maskBitmap = createMask(canvas.getWidth(), canvas.getHeight()); | |
} | |
offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint); | |
canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint); | |
} | |
private Bitmap createMask(int width, int height) { | |
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); | |
Canvas canvas = new Canvas(mask); | |
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
paint.setColor(Color.WHITE); | |
canvas.drawRect(0, 0, width, height, paint); | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | |
canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint); | |
return mask; | |
} | |
//Fixes incorrect outline drawn by default | |
private class ButtonOutlineProvider extends ViewOutlineProvider{ | |
@Override | |
public void getOutline(View view, Outline outline) { | |
outline.setRoundRect(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight(), cornerRadius); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment