Android开发中这些小技巧你都知道吗?(二)

Formatter public static String formatFileSize (Context context, long number)

Added in  API level 3

  • 将文件的大小由字节转换成KB、MB甚至G。再也不用手动去和1024较真了。

Formats a content size to be in the form of bytes, kilobytes, megabytes, etc

Parameters
context Context to use to load the localized units
number size value to be formatted
Returns
  • formatted string with the number

Linkify public static final boolean addLinks (TextView text, int mask)

Added in  API level 1

  • 可以将TextView中的文字根据设定的mask自动设置成超链接,设置超链接的点击事件时会用到LinkMovementMethod。我还用过,大家可参考这篇博文

    Android应用实例之---使用Linkify + 正则式区分微博文本链接及跳转处理

Scans the text of the provided TextView and turns all occurrences of the link types indicated in the mask into clickable links. If matches are found the movement method for the TextView is set to LinkMovementMethod.

StaticLayout

这个类并不常用,一般只有在自定义View时遇到长串文字需要换行时用到

Activity public void onBackPressed ()

Added in  API level 5

  • 这个就很常见了,用于在Activity中拦截返回键事件。(Fragment中可没有这个方法,要想在Fragment中拦截返回键事件请参考我的另一篇博客优雅的让Fragment监听返回键

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

GestureDetector

用来做一些常见用户交互的监听,比如点击,滑动等。一般用于自定义控件。
Detects various gestures and events using the supplied MotionEvents. The GestureDetector.OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvents reported via touch (don't use for trackball events). To use this class:                                                                                 

ActivityManager public int getMemoryClass ()

Added in  API level 5

  • 通过这个方法可以知道系统还能给APP分配多少内存使用。

Return the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.

SystemClock public static void sleep (long ms)

Added in  API level 1

  • 还在为测试网络延迟烦恼么?用这个方法可以很方便的模拟网络延迟,而且不会抛出InterruptedException

Waits a given number of milliseconds (of uptimeMillis) before returning. Similar to sleep(long), but does not throw InterruptedExceptioninterrupt() events are deferred until the next interruptible operation. Does not return until at least the specified number of milliseconds has elapsed.

Parameters
ms to sleep before returning, in milliseconds of uptime.

ViewStub

  • 开发中经常遇到动态显示布局的需求,一般都通过View.GONE/View.VISIBLE来控制,但这样会比较耗费资源。一个推荐的方法是在布局xml中使用ViewStub标签。ViewStub只有在手动被Inflate时才会被初始化。详见Android实战技巧:ViewStub的应用

DisplayMetrics.density public float density

Added in  API level 1

  • 经常使用DisplayMetrics来获取屏幕高度和宽度,此外,还可以通过它获取屏幕密度

DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);


The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

参考资料:

Android Tips Round-Up, Part 2














你可能感兴趣的:(android)