Forked from rajivnarayana/BubbleBackgroundDrawableDemo.java
Created
September 15, 2017 04:24
-
-
Save gleissonmattos/4544dd11640bf97f301d1216e71e4e44 to your computer and use it in GitHub Desktop.
A custom android drawable to draw bubble shape. Used in http://play.google.com/store/apps/details?id=com.webileapps.resolutiontweet
This file contains hidden or 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
package com.webile.demo; | |
import android.app.Activity; | |
import android.graphics.Canvas; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.Paint.Style; | |
import android.graphics.Path; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
import android.graphics.drawable.Drawable; | |
import android.os.Bundle; | |
import android.widget.RelativeLayout; | |
public class BubbleBackgroundDemoActivity extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
RelativeLayout relativeLayout = new RelativeLayout(this); | |
relativeLayout.setBackgroundDrawable(new Drawable() { | |
private static final int OFFSET = 15; | |
Paint whitePaint = new android.graphics.Paint(); | |
@Override | |
public void setColorFilter(ColorFilter cf) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public int getOpacity() { | |
// TODO Auto-generated method stub | |
return android.graphics.PixelFormat.OPAQUE; | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
Rect r = getBounds(); | |
RectF rect = new RectF(r); | |
rect.inset(OFFSET, OFFSET); | |
//Build a path | |
Path path = new Path(); | |
//up arrow | |
path.moveTo(OFFSET, OFFSET); | |
path.lineTo(OFFSET * 2, 0); | |
path.lineTo(OFFSET * 3, OFFSET); | |
//top horizontal line. | |
path.lineTo(r.width() - OFFSET, OFFSET); | |
//top right arc | |
path.arcTo(new RectF(r.right - OFFSET*2, OFFSET, r.right, OFFSET*3), 270, 90); | |
//right vertical line. | |
path.lineTo(r.width(), r.height()-OFFSET); | |
//bottom right arc. | |
path.arcTo(new RectF(r.right - OFFSET * 2, r.bottom- OFFSET * 2, r.right, r.bottom), 0, 90); | |
//bottom horizontal line. | |
path.lineTo(OFFSET, r.height()); | |
//bottom left arc. | |
path.arcTo(new RectF(0, r.bottom- OFFSET * 2, OFFSET * 2, r.bottom), 90, 90); | |
//left horizontal line. | |
path.lineTo(0, OFFSET); | |
//top right arc. | |
path.arcTo(new RectF(0, OFFSET, OFFSET*2, OFFSET*3), 180, 90); | |
path.close(); | |
whitePaint.setColor(android.graphics.Color.WHITE); | |
whitePaint.setStyle(Style.FILL); | |
canvas.drawPath(path, whitePaint); | |
} | |
}); | |
setContentView(relativeLayout); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment