Use XML Drawables.Wherever you can use XML drawables instead of bitmaps. XML drawables won’t let you do everything, but I was surprised by how flexible they are. Android developer docs have a full overview, but here’re some tasters:

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<corners
android:bottomRightRadius="14dp"
android:bottomLeftRadius="14dp"
android:topLeftRadius="14dp"
android:topRightRadius="14dp"/>

<gradient
android:startColor="@color/off_white"
android:endColor="@color/pale_yellow"
android:angle="270"
android:type="linear"/>

<stroke
android:width="4dp"
android:color="@color/osm_darkerblue"/>

</shape>

This defines a rectangle with rounded corners, a border (darker blue) and a gradient. In layout files you can set it whatever width you like and it will look crisp on any screen. Ideal for buttons.


Use more XML Drawables. Just to get you a bit more excited about XML drawables, the radar background below is a rather more complex example:


No bitmaps were harmed in the making of this UI (except the icons).




For the lightbulb we used two PNGs:icon_magnitude_min (an empty lightbulb) andicon_magnitude_max (one filled with light) and we dynamically cropped the latter. To set this up I used:


<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:drawable="@drawable/icon_magnitude_min"
/>

<item >

<clip
android:clipOrientation="vertical"
android:drawable="@drawable/icon_magnitude_max"
android:gravity="top"
/>

</item>

</layer-list>

In the java I get I reference to the clipdrawable and control its level based on light intensity.



Create custom views by overriding onDraw().There are some things XML just isn’t so great at, we draw a lot of graphs in OpenSignal and WeatherSignal and there are libraries for this, but we coded our graphs custom. It’s kind of fun. You might never need to do it, but to make graphics that are highly dynamic and custom it is often the only way to go.

Use SVG where you can’t use XML. Sometimes overriding onDraw() and painstakingly coding up all the lines and arcs you need to draw your custom view is overkill. After all, there is a language for vector graphics, it’s called … Scalable Vector Graphics. It’s what powers one of the coolest Android apps ever – Androidify. In fact they built the library just for that app, and they released it here: SVG for AndroidIt’s what we used to draw the dashboard in OpenSignal.

GZip your SVG files.Makes them smaller and they parse quicker.

The SVG library doesn’t support everything. In particular some alpha channels don’t seem to work, you may even have to edit them out in the code.

The ActionBar and many animation styles didn’t exist pre Honeycomb, use ActionBarSherlock and NineOldAndroids instead.Jake Wharton’s Android libraries are a tour de force of backwards compatibility. As a bonus, itABS also gives you more power of customising the ActionBar.



更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android中的强指针和弱指针
  2. Android 与 H5 之间的互调
  3. 详解Android增量更新
  4. 一篇文章教你读懂UI绘制流程
  5. Looper,Handler,Message
  6. Google董事长:望Android遍布世界 智能机明
  7. Android目录结构(详解)
  8. 五步学会Android的ListView控件
  9. Android Studio:10分钟教会你做百度地图定
  10. android比赛