I am developing small android application in which I m using my custom linear layout class. In that class i tried to draw one small triangle and tried to include it in to my linear layout but I m not able to do that. I tried it in following ways ...
@SuppressLint("DrawAllocation")
public class SimpleLin extends LinearLayout {
public String TAG = "CustomviewActivity";
LinearLayout parentLayout;
public SimpleLin(Context context)
{
super(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(inflater != null){
view = inflater.inflate(R.layout.main2, this);d.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);
}
}
public SimpleLin(Context context, AttributeSet attrs) {
super( context, attrs );
context1= context;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("############################", "inside ondraw");
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setColor(Color.RED);
Point point = new Point();
point.x = 80;
point.y = 80;
Path path = getEquilateralTriangle(point, 70, Direction.SOUTH);
Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas.drawPath(path, p);
}
public static Path getEquilateralTriangle(Point p1, int width, Direction direction) {
Point p2 = null, p3 = null;
if (direction == Direction.NORTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y - width);
}
else if (direction == Direction.SOUTH) {
p2 = new Point(p1.x + width,p1.y);
p3 = new Point(p1.x + (width / 2), p1.y + width);
}
else if (direction == Direction.EAST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x - width, p1.y + (width / 2));
}
else if (direction == Direction.WEST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x + width, p1.y + (width / 2));
}
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
return path;
}
public enum Direction
{
NORTH, SOUTH, EAST, WEST;
}
@SuppressWarnings("deprecation")
public void initialiseImages()
{
invalidate();
}
}
I am calling initialiseImages
method from my activity where i wanted to use this custom layout. So problem is that It not calling my on draw method when i use invalidate(). That's why it not drawing my triangle. and I am also confuse how to include that triangle into my parentlayout
..
Is there wrong in my code..
How to draw such shapes in android...
Need help...
Thank you...