Skip to content

Instantly share code, notes, and snippets.

@kolipass
Created April 28, 2015 04:23
Show Gist options
  • Save kolipass/b6b6ba44de853adfaec3 to your computer and use it in GitHub Desktop.
Save kolipass/b6b6ba44de853adfaec3 to your computer and use it in GitHub Desktop.
HoleSurfaceView for make vignette effect
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.SurfaceView;
public class HoleSurfaceView extends SurfaceView {
private Paint backgroundPaint;
private Paint holePaint;
private Paint borderPaint;
private int padding;
public HoleView(Context context) {
super(context);
init();
}
public HoleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
setWillNotDraw(false);
Resources resources = getContext().getResources();
padding = (int)resources.getDimension(R.dimen.photo_shadow_padding);
backgroundPaint = new Paint();
backgroundPaint.setColor(resources.getColor(R.color.photo_shadow_background));
borderPaint = new Paint();
borderPaint.setColor(resources.getColor(R.color.black));
holePaint = new Paint();
holePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getHeight();
int width = getWidth();
int borderHeight = (height - width) / 2;
canvas.drawPaint(backgroundPaint);
canvas.drawCircle(width / 2, height / 2, (width - padding * 2) / 2, holePaint);
canvas.drawRect(0, 0, width, borderHeight, borderPaint);
canvas.drawRect(0, height - borderHeight, width, height, borderPaint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment