Skip to content

Instantly share code, notes, and snippets.

@worker8
Last active November 23, 2018 10:44
Show Gist options
  • Select an option

  • Save worker8/c347121b4b042163a6e6 to your computer and use it in GitHub Desktop.

Select an option

Save worker8/c347121b4b042163a6e6 to your computer and use it in GitHub Desktop.
FrameLayout with a hole punched, intended to be used for tutorial screens. The punched hole is for highlighting the View to be pressed.
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.FrameLayout;
/**
* TODO: document your custom view class.
*/
public class FrameLayoutWithHole extends FrameLayout {
private String mExampleString; // TODO: use a default from R.string...
private int mExampleColor = Color.RED; // TODO: use a default from R.color...
private float mExampleDimension = 0; // TODO: use a default from R.dimen...
private Drawable mExampleDrawable;
private TextPaint mTextPaint;
private float mTextWidth;
private float mTextHeight;
private Paint mOverlayPaint;
public FrameLayoutWithHole(Context context) {
super(context);
init(null, 0);
}
public FrameLayoutWithHole(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public FrameLayoutWithHole(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.FrameLayoutWithHole, defStyle, 0);
mExampleString = a.getString(
R.styleable.FrameLayoutWithHole_exampleString);
if (mExampleString == null){
mExampleString = "Hello custom view";
}
mExampleColor = a.getColor(
R.styleable.FrameLayoutWithHole_exampleColor,
mExampleColor);
// Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
// values that should fall on pixel boundaries.
mExampleDimension = a.getDimension(
R.styleable.FrameLayoutWithHole_exampleDimension,
mExampleDimension);
if (a.hasValue(R.styleable.FrameLayoutWithHole_exampleDrawable)) {
mExampleDrawable = a.getDrawable(
R.styleable.FrameLayoutWithHole_exampleDrawable);
mExampleDrawable.setCallback(this);
}
a.recycle();
// Set up a default TextPaint object
mTextPaint = new TextPaint();
mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextAlign(Paint.Align.LEFT);
// Update TextPaint and text measurements from attributes
invalidateTextPaintAndMeasurements();
setWillNotDraw(false);
bitmapx = Bitmap.createBitmap(1080, 1920, Bitmap.Config.ARGB_8888);
temp = new Canvas(bitmapx);
paint = new Paint();
paint.setColor(0xcc000000);
transparentPaint = new Paint();
transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
Bitmap bitmapx;
private Canvas temp;
private Paint paint;
private Paint p = new Paint();
private Paint transparentPaint;
private void invalidateTextPaintAndMeasurements() {
mTextPaint.setTextSize(mExampleDimension);
mTextPaint.setColor(mExampleColor);
mTextWidth = mTextPaint.measureText(mExampleString);
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
mTextHeight = fontMetrics.bottom;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// TODO: consider storing these as member variables to reduce
// allocations per draw cycle.
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
int contentWidth = getWidth() - paddingLeft - paddingRight;
int contentHeight = getHeight() - paddingTop - paddingBottom;
// Draw the text.
canvas.drawText(mExampleString,
paddingLeft + (contentWidth - mTextWidth) / 2,
paddingTop + (contentHeight + mTextHeight) / 2,
mTextPaint);
temp.drawRect(0, 0, temp.getWidth(), temp.getHeight(), paint);
temp.drawCircle(300,300, 150, transparentPaint);
canvas.drawBitmap(bitmapx, 0, 0, p);
// Draw the example drawable on top of the text.
if (mExampleDrawable != null) {
mExampleDrawable.setBounds(paddingLeft, paddingTop,
paddingLeft + contentWidth, paddingTop + contentHeight);
mExampleDrawable.draw(canvas);
}
}
/**
* Gets the example string attribute value.
*
* @return The example string attribute value.
*/
public String getExampleString() {
return mExampleString;
}
/**
* Sets the view's example string attribute value. In the example view, this string
* is the text to draw.
*
* @param exampleString The example string attribute value to use.
*/
public void setExampleString(String exampleString) {
mExampleString = exampleString;
invalidateTextPaintAndMeasurements();
}
/**
* Gets the example color attribute value.
*
* @return The example color attribute value.
*/
public int getExampleColor() {
return mExampleColor;
}
/**
* Sets the view's example color attribute value. In the example view, this color
* is the font color.
*
* @param exampleColor The example color attribute value to use.
*/
public void setExampleColor(int exampleColor) {
mExampleColor = exampleColor;
invalidateTextPaintAndMeasurements();
}
/**
* Gets the example dimension attribute value.
*
* @return The example dimension attribute value.
*/
public float getExampleDimension() {
return mExampleDimension;
}
/**
* Sets the view's example dimension attribute value. In the example view, this dimension
* is the font size.
*
* @param exampleDimension The example dimension attribute value to use.
*/
public void setExampleDimension(float exampleDimension) {
mExampleDimension = exampleDimension;
invalidateTextPaintAndMeasurements();
}
/**
* Gets the example drawable attribute value.
*
* @return The example drawable attribute value.
*/
public Drawable getExampleDrawable() {
return mExampleDrawable;
}
/**
* Sets the view's example drawable attribute value. In the example view, this drawable is
* drawn above the text.
*
* @param exampleDrawable The example drawable attribute value to use.
*/
public void setExampleDrawable(Drawable exampleDrawable) {
mExampleDrawable = exampleDrawable;
}
}
// Place this in onCreate() of any activity to see the effect
FrameLayoutWithHole childLayout = new FrameLayoutWithHole(mActivity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
((ViewGroup) mActivity.getWindow().getDecorView()).addView(childLayout, params);
@prudhvi1

Copy link
Copy Markdown

HI,
if possible can you also add all the R.Stylable.. ??? please .

@txhuy

txhuy commented Dec 4, 2017

Copy link
Copy Markdown

@prudhvi1 You can recreate it or grab this:
<declare-styleable name="FrameLayoutWithHole"> <attr name="exampleString" format="string"/> <attr name="exampleColor" format="color"/> <attr name="exampleDimension" format="float"/> <attr name="exampleDrawable" format="integer"/> </declare-styleable>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment