Last active
August 25, 2017 09:58
-
-
Save AkshatAgrawal05/a334ca0522c7362c66d474872e204d2e to your computer and use it in GitHub Desktop.
Code Snippet to create Google Space like navigation tab in Android
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
public final class FabTabBackgroundView extends View { | |
Paint paint; | |
Path path; | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public FabTabBackgroundView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
public FabTabBackgroundView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
public FabTabBackgroundView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public FabTabBackgroundView(Context context) { | |
super(context); | |
init(); | |
} | |
void init() { | |
paint = new Paint(); | |
Resources localResources = getContext().getResources(); | |
paint.setStyle(Paint.Style.FILL_AND_STROKE); | |
paint.setAntiAlias(true); | |
paint.setColor(localResources.getColor(R.color.tab)); | |
paint.setAlpha(244); | |
} | |
@Override | |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | |
super.onLayout(changed, left, top, right, bottom); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
Resources localResources = getContext().getResources(); | |
int width = View.MeasureSpec.getSize(widthMeasureSpec); | |
int offset = localResources.getDimensionPixelOffset(R.dimen.offset); | |
int size = localResources.getDimensionPixelSize(R.dimen.size); | |
int j = size + 2 * offset; | |
int k = (int) (0.7D * j); | |
int m = width / 2 - size; | |
int n = width - m; | |
int i1 = j - k; | |
path = new Path(); | |
path.reset(); | |
path.moveTo(0.0F, i1); | |
path.lineTo(m, i1); | |
//System.out.println("width-->" + width + "offset--->" + offset + "size-->" + size + "j-->" + j + "k-->" + k + "m--->" + m + "n-->" + n + "il--->" + i1); | |
//System.out.println(); | |
path.cubicTo(m + size / 2, i1, (width - size) / 2, 0.0F, width / 2, 0.0F); | |
path.cubicTo((width + size) / 2, 0.0F, n - size / 2, i1, n, i1); | |
path.lineTo(width, i1); | |
path.lineTo(width, j); | |
path.lineTo(0.0F, j); | |
path.close(); /*the anchors you want, the curve will tend to reach these anchor points; look at the wikipedia article to understand more */ | |
this.setMeasuredDimension(width, j); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
canvas.drawPath(path, paint); | |
} | |
@Override | |
protected boolean awakenScrollBars() { | |
return super.awakenScrollBars(); | |
} | |
@Override | |
protected boolean awakenScrollBars(int startDelay) { | |
return super.awakenScrollBars(startDelay); | |
} | |
@Override | |
protected boolean awakenScrollBars(int startDelay, boolean invalidate) { | |
return super.awakenScrollBars(startDelay, invalidate); | |
} | |
@Override | |
protected boolean dispatchGenericFocusedEvent(MotionEvent event) { | |
return super.dispatchGenericFocusedEvent(event); | |
} | |
@Override | |
protected boolean dispatchGenericPointerEvent(MotionEvent event) { | |
return super.dispatchGenericPointerEvent(event); | |
} | |
@Override | |
protected boolean dispatchHoverEvent(MotionEvent event) { | |
return super.dispatchHoverEvent(event); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thankyou so much for this!!! I've been trying for this for months, and thanks to you, I can finally implement it! :D 👍 💯