android悬浮窗--获取内存.doc
文本预览下载声明
android悬浮窗--获取内存
? ? ? ? ? 首先,得先说明,这个例子并不是我写的,是从eoeAndroid的一个帖子上面看到的,下载了他的源代码,然后分析一下,供学习共享。(另外,对于其代码有所修改,以便于更好的说明问题,另:同时把源代码上传,下载地址:/detail/aomandeshangxiao/3880055)
? ? ? ? ?一开始,我们先看一下运行效果:
其中,
这一块就是悬浮窗,可以随意拖动,动态显示当前内存使用量。
? ? ? ? ?下面看一下代码是如何实现的:
悬浮窗的实现是用了一个service,为什么要用service呢?了解service特点的大体就会明白。下面看一下:
[java] view plaincopyprint?
SPAN?style=FONT-SIZE:?16pxpublic?class?FloatService?extends?Service?{??
??
????WindowManager?wm?=?null;??
????WindowManager.LayoutParams?wmParams?=?null;??
????View?view;??
????private?float?mTouchStartX;??
????private?float?mTouchStartY;??
????private?float?x;??
????private?float?y;??
????int?state;??
????TextView?tx1;??
????TextView?tx;??
????ImageView?iv;??
????private?float?StartX;??
????private?float?StartY;??
????int?delaytime=1000;??
????@Override??
????public?void?onCreate()?{??
????????Log.d(FloatService,?onCreate);??
????????super.onCreate();??
????????view?=?LayoutInflater.from(this).inflate(R.layout.floating,?null);??
????????tx?=?(TextView)?view.findViewById(R.id.memunused);??
????????tx1?=?(TextView)?view.findViewById(R.id.memtotal);??
????????tx.setText(?+?memInfo.getmem_UNUSED(this)?+?KB);??
????????tx1.setText(?+?memInfo.getmem_TOLAL()?+?KB);??
????????iv?=?(ImageView)?view.findViewById(R.id.img2);??
????????iv.setVisibility(View.GONE);??
????????createView();??
????????handler.postDelayed(task,?delaytime);??
????}??
??
????private?void?createView()?{??
????????//?获取WindowManager ??
????????wm?=?(WindowManager)?getApplicationContext().getSystemService(window);??
????????//?设置LayoutParams(全局变量)相关参数 ??
????????wmParams?=?new?WindowManager.LayoutParams();??
????????wmParams.type?=?2002;??
????????wmParams.flags?|=?8;??
????????wmParams.gravity?=?Gravity.LEFT?|?Gravity.TOP;?//?调整悬浮窗口至左上角 ??
????????//?以屏幕左上角为原点,设置x、y初始值 ??
????????wmParams.x?=?0;??
????????wmParams.y?=?0;??
????????//?设置悬浮窗口长宽数据 ??
????????wmParams.width?=?WindowManager.LayoutParams.WRAP_CONTENT;??
????????wmParams.height?=?WindowManager.LayoutParams.WRAP_C
显示全部