Saturday, March 16, 2019

Android开发笔记: 3.7 Resource and Drawable


3.7 Resource and Drawable

Refer to resources. Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. So the filename under values/ doesn't matter, resource in all files there will be picked up into the package for pre-v21. Refer compatibility, for Android 5.0/API level 21 and newer, it takes resource from alternative place such as res/values-v21 and res/layout-v21.
For drawable, refer to Working with Drawable, w3 SVG path, google design of icons, drawable resource, and downloadable google icons. When using Android Studio to create a new project, it may create multiple set of drawables, such as from drawable-v21 for target with API v21 and newer, and res/values/drawables.xml for target before Android 5.0/API level 21. The drawables.xml is referring to system icon in SDK android.jar (refer here) like: <item name="ic_menu_mpl" type="drawable">@android:drawable/ic_menu_camera</item>
Same res can be found under SDK data subfolder. Not all icons there could be used directly, such as compass_base, when compile, it complains “Resource is not public”. To use non public icon, will have to copy the icon over to local res folder of your project.
Target before Android 5.0 does not support Vector Drawables, which might lead to InflateException with binary XML file.
If use png or other image files under drawable, use the filename w/o ext as the id of the drawable. Interesting when I use an icon for navigation drawer menu, no matter I pick the white color or black color of the google icon in xml, it displays same. I guess it will change the color bases on the theme. Here is a opensource Android Asset Studio, which can create all size of icons base on user provided png file.
There is strange problem with Android 5.0/API21, android:pathData="M19.43 12.98c.04-.32.07-.64.07-.98s-.03-.66-.07
This will cause runtime exception: java.lang.NumberFormatException: Invalid float: "-.32.07". To get around it, add space between data.
Note the difference between @+id and @id, refer to this SO. The '+' is telling the parser to create a new ID to R.java.
A free resource site: https://commons.wikimedia.org/wiki/Main_Page, include free image, sound and video.
Note of data URI, refer to wikipedia, is frequently used for embedded small png image like: <img src="data:image/x-png;base64,iVBO..."> is a barcode. To display the image in Android, follow this SO: need to convert the Base64 encoded string to a Bitmap like this:
byte[] decodedString = Base64.decode("Your Base64 String", Base64.DEFAULT);
Bitmap bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
For multi-language support, refer to languages and localization.

0 Comments:

Post a Comment