How to create Star and Diamond in Android

I am here to share  how to create Star and Diamond in Android:

1. Create Star using Line :

int maxX, maxY, minMaxXY, xCenter, yCenter;
maxX = width – 1;
maxY = height – 1;
minMaxXY = Math.min(maxX, maxY);
xCenter = maxX / 2;
yCenter = maxY / 2;
float side = 0.95F * minMaxXY, sideHalf = 0.5F * side, h = sideHalf
* (float) Math.sqrt(3), xA, yA, xB, yB, xC, yC, xA1, yA1, xB1, yB1, xC1, yC1,xD,yD, p, q;

int ax = (xCenter/2) + 20;
int ay = 20;
int bx = xCenter+20;
int by = (int)(.374*(double)(yCenter)+(double)(20));
int cx = (int)(.825*(double)(xCenter)+(double)(20));
int cy = yCenter+20;
int dx = (int)(.175*(double)(yCenter)+(double)(20));
int dy = yCenter+20;
int ex = 20;
int ey = by;
//Color colorSave = g.getColor(); // save previous color
//g.setColor(c);
g.drawLine(ax,ay,cx,cy);
g.drawLine(bx,by,dx,dy);
g.drawLine(cx,cy,ex,ey);
g.drawLine(dx,dy,ax,ay);
g.drawLine(ex,ey,bx,by);

2.Create Star using Eclipse :

float side = 0.95F * minMaxXY, sideHalf = 0.5F * side, h = sideHalf
* (float) Math.sqrt(4);

xA = xCenter – sideHalf;
yA = yCenter – 0.5F * h;
xB = xCenter + sideHalf;
yB = yA;
xC = xCenter + sideHalf;
yC = (yCenter + 0.5F * h) + 5;
xD = xCenter – sideHalf;
yD = (yCenter + 0.5F * h) + 5;

int r = (int) radius;
Random rd = new Random();
int red = rd.nextInt(255);
int blue = rd.nextInt(255);
int green = rd.nextInt(255);
RectF  oval = new RectF((float)centerX,(float)centerY, (float)centerX+30,(float) centerY+70);
RectF  oval1 = new RectF((float)centerX-25,(float)centerY+20, (float)centerX+60,(float) centerY+50);

c.drawOval(oval, p);
c.drawOval(oval1, p);

3.Create Diamond using Line :

centerX=width/2
centerY=height/2
c.drawLine((float)centerX,(float) (centerY), (float)centerX+20,(float)centerY+20,p);
c.drawLine((float)centerX,(float)centerY,(float)centerX+20,(float)centerY-20,p);
c.drawLine((float)centerX+20,(float)centerY-20,(float)centerX+40, (float)centerY,p);
c.drawLine((float)centerX+40,(float)centerY,(float)centerX+20, (float)centerY+20,p);

 

– By Sonu Odesi

Android Developer