How to inflate a figure on canvas?

I have a form stored in an xml file in my drawables directory. I would like to use it in Canvas (I know that I can define the form in the code, but I'm trying to figure out how to implement it more "Androidy").

I am at a loss regarding the syntax for getting the form on canvas. Should I try to convert it to shape or draw? Do I need a Matrix? Paint? and etc.

I don't need a lot of details, just point me in the right direction :)

Thanks.

[edit] My Android XML form is as follows:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:width="3px" android:color="#583010"/> <solid android:color="#ffffff" /> <corners android:bottomRightRadius="3px" android:bottomLeftRadius="3px" android:topLeftRadius="3px" android:topRightRadius="3px"/> </shape> 

I guess there must be some way to get this overpriced, no? [/ Edit]

+4
source share
2 answers

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> 
+5
source

Determined by how? Something like SVG? You will need to write your own parser and translate the / spaths form to calls to Canvas.drawPath()/Rect() , etc. You can write your own performance that knows how to draw yourself, etc. In any case, someone needs to write code for it. Scrolling is simply parsing XML and creating a graph of objects.

0
source

Source: https://habr.com/ru/post/1381247/


All Articles