You must implement your own View , which will draw a circle, square or triangle on the screen. The shape must be selected from the markup file. Here are the attributes from the markup file:
<resources> <declare-styleable name="PainView"> <attr name="figure" format="enum"> <enum name="circle" value = "0"/> <enum name="square" value = "1"/> <enum name="triangle" value = "2"/> <enum name="line" value = "3"/> </attr> </declare-styleable> Here is the markup:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app= "http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.MainActivity"> <com.example.Widgets.PainView android:layout_width="match_parent" android:layout_height="match_parent" app:figure="square" /> </RelativeLayout> Here is a CustomView:
public class PainView extends View { private Paint mPaint = null; private int cellSize = 30; public PainView(Context context) { super(context); this.initView(); } public PainView(Context context, AttributeSet attrs) { super(context, attrs); this.initView(); TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.PainView); Integer circle = attributes.getInteger(R.styleable.PainView_figure, 0); Integer square = attributes.getInteger(R.styleable.PainView_figure, 1); Integer triangle = attributes.getInteger(R.styleable.PainView_figure, 2); attributes.recycle(); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); int h = this.getHeight(); int w = this.getWidth(); int radius = (h / cellSize) + 1; mPaint.setAntiAlias(true); canvas.drawCircle(0, 0, radius, mPaint); canvas.drawRect(0, 0, 0, 0, mPaint); canvas.drawPath(triangle(), mPaint); } private void initView(){ mPaint = new Paint(); mPaint.setColor(Color.BLACK); } private Path triangle(){ Point a = new Point(0, 100); Point b = new Point(50, 0); Point c = new Point(100, 100); Path triangle = new Path(); triangle.setFillType(Path.FillType.EVEN_ODD); triangle.moveTo(ax, ay); triangle.lineTo(bx, by); triangle.lineTo(cx, cy); triangle.lineTo(ax, ay); triangle.close(); return triangle; } } I can not think how to transfer a choice of a marking from the designer to the onDraw method.