摘要:在开发Android App的过程中,经常会遇到内存方面的压力,比如OOM,或者频繁GC。 本文不打算涵盖内存优化的所有方面,只是介绍一下我自己遇到的问题和解决方法。 1.确定频繁分配内存的代码路径。 一般来说,频繁分配内存的路径可能会是绘制(draw)相关的方法,排版(layout)相关的方法,某些回调方法(特别是传感器回调方法)。 你可能会检查这部分代码,然后优化它。但是,内存分配可能发生在调用链的更下面,检查代码非常困难。 这里推荐一个工具,DDMS下的Allocation Tracker。它可以显示出程序运行中频繁分配内存的部分,并准确定位到对应的代码。...
阅读全文
摘要:Android的Touch事件处理机制比较复杂,特别是在考虑了多点触摸以及事件拦截之后。 Android的Touch事件处理分3个层面:Activity层,ViewGroup层,View层。 首先说一下Touch事件处理的几条基本规则。 1.如果在某个层级没有处理ACTION_DOWN事件,那么该层就再也收不到后续的Touch事件了直到下一次ACTION_DOWN事件。 说明:a.某个层级没有处理某个事件指的是它以及它的子View都没有处理该事件。 b.这条规则不适用于Activity层(它是顶层),它们可以收到每一个Touch事件。 c.如...
阅读全文
摘要:TouchDelegates in Android allow to increase the touch area of a View, e.g. Button. This is very useful if you want to make it easier for the user to touch your button. Here is a small example for the usage of a touch delegate. The layout has the ID root and contains a button with the ID delegat...
阅读全文
摘要:There are basically three logs on the system:Log:for short, textual datain-memory ringbuffer, fastephemeral (you'll lose it on a crash, or the ringbuffer scrolls)intended forapp developersEventLog is:for short, binary datain-memory ringbuffer, fastephemeral (you'll lose it on a crash, or the
阅读全文
摘要:从Android 2.3(API Level 9)开始,Android提供了一个程序性能诊断工具,它就是StrictMode。 目前,StrictMode的能力与限制包括: 1.基于线程的对磁盘读写,网络操作,以及自定义耗时操作等的监控; 2.基于VM进程的对对象泄露(Activity对象,SQLite对象,未反注册对象,未关闭对象)的监控; 3.可以检测到跨进程的耗时操作(当然必须是同步操作); 4.当前不支持在jni中发生的网络与磁盘操作。 随着Android的进化,StrictMode的功能也将越来越强大。 当StrictMode...
阅读全文
摘要:What Triggers ANR?In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services. Android will display the ANR dialog for a particular application when it detects one of the following conditions:No response to an input event (e.g. key press, screen tou
阅读全文
摘要:Android从3.0(APILevel11)开始,在绘制View的时候支持硬件加速,充分利用GPU的特性,使得绘制更加平滑,但是会多消耗一些内存。开启或关闭硬件加速:由于硬件加速自身并非完美无缺,所以Android提供选项来打开或者关闭硬件加速,默认是关闭。可以在4个级别上打开或者关闭硬件加速:Application级别:<applicationandroid:hardwareAccelerated="true"...>Activity级别:<activityandroid:hardwareAccelerated="false"...
阅读全文
摘要:Tips and TricksSwitching to hardware accelerated 2D graphics can instantly increase performance, but you should still design your application to use the GPU effectively by following these recommendations:Reduce the number of views in your applicationThe more views the system has to draw, the slower
阅读全文
摘要:大多数Android设备都是触摸屏的,但是实际上Android设备也支持键盘操作,允许通过键盘来完成导航,点击,输入等。 当用户通过键盘(或者轨迹球)操作的时候,有必要聚焦当前接受输入的UI元素,例如,高亮(聚焦)某个按钮,让用户知道当前正在操作的UI元素是哪个。 但是,当用户使用触摸屏与设备交互的时候,始终聚焦当前UI元素就没有必要了,而且很丑陋;用户点击哪个元素,哪个元素就是当前元素,无需高亮标识。并且,通过触摸屏与设备交互的时候,点击某个UI元素也不会导致该元素聚焦,此时的高亮效果是由Pressed状态来完成的。也就是说,在Touch Mode模式之下,UI元素是不会进入聚...
阅读全文
摘要:Android中的View对于ScrollBar和Scroll的支持是非常灵活的,不仅仅是UI样式可变,计算参数的方式也是可变的。 在Android中,任何View都可以显示出ScrollBar,唯一的条件是自身高度不足以显示全部内容。 在UI元素上,ScrollBar由两部分组成,一个是Track(滑道),一个是Thumb(滑块),这两部分都是可以定制的(指定自定义的drawable),另外ScrollBar的宽度(竖向)或高度(横向)也是可以控制的,相关的控制属性是:android:scrollbarThumbHorizontalandroid:scrollbarThumbVert...
阅读全文