Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created July 18, 2012 18:29
Show Gist options
  • Save rajivnarayana/3137921 to your computer and use it in GitHub Desktop.
Save rajivnarayana/3137921 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
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);
}
}
@amolsawant88
Copy link

Hello Rajiv,
Thank you so much for code stuff its help me lot.

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