| Start |
|---|
| Creating Custom Views If you need a completely customized view, you'll need to subclass Note that onSizeChanged(...) method: this allows you to catch the current height and width of your custom view to properly adjust your rendering code. Here we just calculate our center and our radius: Note that it is recommended to build your project after the view code is finished. Without building it you won't be able to see the view on a preview screen in Android Studio. After putting everything together, you should be greeted with the following screen after launching the activity containing the above layout: |
| Adding attributes to views Custom views can also take custom attributes which can be used in Android layout resource files. To add attributes to your custom view you need to do the following: Tip: Custom attributes do not work with the SmileyView demonstrates how the attributes can be extracted: Which gets applied in our Note that any attribute values set in the inflated layout file (see code in step 2) will override the corresponding values of the default style. smileyStyle: Which we then provide a style for in our app theme (here we just reuse the default style from step 4): |
| CustomView performance tips Do not allocate new objects in onDraw Instead of drawing drawables in canvas... Use a Bitmap for faster drawing: Do not redraw the entire view to update just a small part of it. Instead redraw the specific part of view. If your view is doing some continuous animation, for instance a watch-face showing each and every second, at least stop the animation at Do not do any calculations inside the Rotations The basic operations of a view are translate, rotate, etc... Almost every developer has faced this problem when they use bitmap or gradients in their custom view. If the view is going to show a rotated view and the bitmap has to be rotated in that custom view, many of us will think that it will be expensive. Many think that rotating a bitmap is very expensive because in order to do that, you need to translate the bitmap's pixel matrix. But the truth is that it is not that tough! Instead of rotating the bitmap, just rotate the canvas itself! |
| Creating a compound view A compound view is a custom This means that the surrounding controller code, an Activity, Fragment or Adapter, can simply pass the data object to the view without picking it apart into a number of different UI widgets. This facilitates code reuse and makes for a better design according to SOLID priciples (opens new window). The layout XML This is usually where you start. You have an existing bit of XML that you find yourself reusing, perhaps as an This XML file keeps working in the Layout Editor in Android Studio perfectly fine. You can treat it like any other layout. The compound ViewGroup Once you have the XML file, create the custom view group. The With these pieces in place, you can use it in your app. Usage in XML Here's an example Usage in Code Here's an example |
| Compound view for SVG/VectorDrawable as drawableRight Main motive to develop this compound view is, below 5.0 devices does not support svg in drawable inside TextView/EditText. One more pros is, we can set Module Name : custom_edit_drawable (short name for prefix- c_d_e)"c_d_e_" prefix to use so that app module resources should not override them by mistake. Example : "abc" prefix is used by google in support library. build.gradleuse AppCompat >= 23 Layout file : c_e_d_compound_view.xmlCustom Attributes : attrs.xmlCode : EditTextWithDrawable.javaExample : How to use above viewLayout : activity_main.xmlLayout : activity_main.xmlActivity : MainActivity.java |
| Responding to Touch Events Many custom views need to accept user interaction in the form of touch events. You can get access to touch events by overriding
You can add the following method to your view and then observe the log output when you touch and move your finger around your view. Further reading: |
Read Article
https://develop.horje.com/learn/1434/reference