Quantcast
Viewing latest article 3
Browse Latest Browse All 50

Custom Views in Android

Some lessons i learned using the Eclipse custom view preview feature:

  • Many of the Android versions you can choose in the UI editor of Eclipse do not render custom UIs correctly. The one I use now is Android 2.2
  • Add all 3 default constructors to the custom view and set them to @Deprecated if you want the developer to use your custom constructor
  • Call an init() method from all constructor methods and do the field init etc there
  • In init() use something like "if (isInEditMode()) loadDemoValues();" to set example values only if the view is displayed in the UI preview and not in the real application
  • Don't use a fixed size for your UI instead get the values in onMeasure and store the size desired by the view in a field
  • Add a debug field and display it in onDraw if its not null to allow simple debug information inside the UI preview
  • Don't create objects in the onDraw() method
  • Use linear algebra for rotation etc. (e.g. write your own small Vec2d class)
  • You cannot access your res resources (like R.drawable.icon) so a function to create a dummy Bitmap is usefull
  • Check as often as possible if your custom view is still working (e.g. open a second editor window of Eclipse and display it on a second screen)
  • First learn how Canvas works and what methods you should use for which task
  • Learn how to use Animations
  • Use ARGB_4444 if you run into memory problems when creating many Bitmap objects

A nice video about Bitmap drawing, Canvas etc.: http://youtu.be/jF6Ad4GYjRU

Viewing latest article 3
Browse Latest Browse All 50

Trending Articles