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


转载请注明出处:http://blog.csdn.net/guxiao1201/article/details/40712217

PopupWindow

  •  在一些经常用到的控件中都能看到PopupWindow的身影,比如actionbar、autocompleteTextView等。PopupWindow可用于创建悬浮窗口

ThumbnailUtils

  • 可以很方便的创建视频的缩略图,甚至还可以指定缩略图的尺寸
Public Constructors
ThumbnailUtils()
Public Methods
static  Bitmap createVideoThumbnail( String filePath, int kind)
Create a video thumbnail for a video.
static  Bitmap extractThumbnail( Bitmap source, int width, int height, int options)
Creates a centered bitmap of the desired size.
static  Bitmap extractThumbnail( Bitmap source, int width, int height)
Creates a centered bitmap of the desired size.

SparseArray

  • SparseArray是Android为<Integer,Object>类型的HashMap专门写的类,目的是为了提供效率,其核心算法是折半查找。

SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.

Note that this container keeps its mappings in an array data structure, using a binary search to find keys. The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

To help with performance, the container includes an optimization when removing keys: instead of compacting its array immediately, it leaves the removed entry marked as deleted. The entry can then be re-used for the same key, or compacted later in a single garbage collection step of all removed entries. This garbage collection will need to be performed at any time the array needs to be grown or the the map size or entry values are retrieved.

It is possible to iterate over the items in this container using keyAt(int) and valueAt(int). Iterating over the keys using keyAt(int) with ascending values of the index will return the keys in ascending order, or the values corresponding to the keys in ascending order in the case of valueAt(int).

PackageManger public abstract void setComponentEnabledSetting (ComponentName componentName, int newState, int flags)

Added in  API level 1

  • 使用这个方法可以开启和禁用四大组件。开始我很纳闷为什么要禁用组件?后来通过查看android 禁用和开启四大组件的方法博文,发现这个方法可以作为后期性能优化方法之一。

Set the enabled setting for a package component (activity, receiver, service, provider). This setting will override any enabled state which may have been set by the component in its manifest.

Parameters
componentName The component to enable
newState The new enabled state for the component. The legal values for this state are: COMPONENT_ENABLED_STATE_ENABLEDCOMPONENT_ENABLED_STATE_DISABLED andCOMPONENT_ENABLED_STATE_DEFAULT The last one removes the setting, thereby restoring the component's state to whatever was set in it's manifest (or enabled, by default).
flags Optional behavior flags: DONT_KILL_APP or 0.

View public static int generateViewId ()

Added in  API level 17

  • 这简直是动态生成控件的福利啊,以后妈妈再也不用担心动态控件id冲突了。

Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.

Returns
  • a generated ID value

ActivityManager public boolean clearApplicationUserData ()

Added in  API level 19

  • 一键清除应用数据,不用再手动一个个clear了,但比较悲催的是API 19才提供的接口。

Permits an application to erase its own data from disk. This is equivalent to the user choosing to clear the app's data from within the device settings UI. It erases all dynamic data associated with the app -- its private data and data in its private area on external storage -- but does not remove the installed application itself, nor any OBB files.

Returns
  • true if the application successfully requested that the application's data be erased; false otherwise.

ActivityOptions 

  • 不知道大家有没有注意到startActivity(Intent,Bundle),那么ActivityOptions就是这个Bundle的原型,负责Activity跳转时的动画。
 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(view, 0, 0,
            view.getWidth(), view.getHeight());
      // Request the activity be started, using the custom animation options.
      startActivity(new Intent(MainActivity.this, AnimationActivity.class),
            opts.toBundle());

Helper class for building an options Bundle that can be used with  Context.startActivity(Intent, Bundle)  and related methods.


参考资料:

Android Tips Round-Up, Part 5


你可能感兴趣的:(android)