I am developing a small Android application in which I use my own linear layout class. In this class, I tried to draw one small triangle and tried to include it in my linear layout, but I could not do it. I tried this in the 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 call the initialiseImages method from my activity, where I wanted to use this custom layout. So the problem is that it does not call my draw method when using invalidate (). That's why he does not draw my triangle. and I am also confused how to include this triangle in my parentlayout .. There are errors in my code. How to draw such shapes in android ... Need help ... Thanks ...
source share