Call your file 'res / drawable / my_shape.xml'. The following lines of code will inflate my_shape.xml from XML and draw its contents on the canvas. ImageView is used to prove that this works.
Drawable shape = getResources().getDrawable(R.drawable.my_shape); Bitmap bitmap = Bitmap.createBitmap( shape.getIntrinsicWidth(), shape.getIntrinsicHeight(), Config.ARGB_8888 ); Canvas canvas = new Canvas( bitmap ); shape.setBounds( 0, 0, canvas.getWidth(), canvas.getHeight() ); shape.draw( canvas ); ImageView imageView = (ImageView) findViewById(R.id.my_imageView); imageView.setImageBitmap( bitmap );
Make sure these lines of code are executed after setContentView in your onCreate.
To eliminate as much confusion as possible, this is the functional sample my_shape.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="1.9" android:useLevel="false" > <solid android:color="#DDAAAFFF" /> <size android:height="10dp" android:width="10dp" /> <stroke android:width="1dp" android:color="#AAAAAA" /> </shape>
source share