你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发---制作桌面可移动控件

[日期:2012-08-19] 来源:blog.csdn.net/xn4545945  作者:xn4545945 [字体: ]

Android的应该经常会看见桌面上显示歌词,或者流量监控的悬浮窗。今天通过一个简单的实例来学习。

先看看效果。

1. 先建一个top_window.xml。这个就是用来在桌面上显示的控件。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_width="wrap_content"  
  4. android:layout_height="wrap_content"  
  5. android:background="#ffffff"  
  6. android:orientation="vertical" >  
  7. <TextView  
  8. android:layout_width="wrap_content"  
  9. android:layout_height="wrap_content"  
  10. android:text="点我就能移动~"  
  11. android:textColor="#000000" />  
  12. </LinearLayout>   

2. 建一个类继承自Application

  1. /* 
  2. * 主要用到两个类WindowManager, WindowManager.LayoutParams. 对窗口进行管理. 
  3. */  
  4. package com.orgcent.desktop;  
  5.   
  6. import android.app.Application;  
  7. import android.content.Context;  
  8. import android.view.LayoutInflater;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11. import android.view.WindowManager;  
  12. import android.view.View.OnTouchListener;  
  13.   
  14. public class BaseAppliction extends Application  
  15. {  
  16.     WindowManager mWM;  
  17.     WindowManager.LayoutParams mWMParams;  
  18.   
  19.     @Override  
  20.     public void onCreate()  
  21.     {  
  22.         mWM = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
  23.         final View win = LayoutInflater.from(this).inflate(  
  24.                 R.layout.top_window, null);  
  25.   
  26.         win.setOnTouchListener(new OnTouchListener()  
  27.         {  
  28.             float lastX, lastY;  
  29.   
  30.             public boolean onTouch(View v, MotionEvent event)  
  31.             {  
  32.                 final int action = event.getAction();  
  33.   
  34.                 float x = event.getX();  
  35.                 float y = event.getY();  
  36.                 if (action == MotionEvent.ACTION_DOWN)  
  37.                 {  
  38.                     lastX = x;  
  39.                     lastY = y;  
  40.                 } else if (action == MotionEvent.ACTION_MOVE)  
  41.                 {  
  42.                     mWMParams.x += (int) (x - lastX);  
  43.                     mWMParams.y += (int) (y - lastY);  
  44.                     mWM.updateViewLayout(win, mWMParams);  
  45.                 }  
  46.                 return true;  
  47.             }  
  48.         });  
  49.   
  50.         WindowManager wm = mWM;  
  51.         WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();  
  52.         mWMParams = wmParams;  
  53.         wmParams.type = 2002//type是关键,这里的2002表示系统级窗口,你也可以试试2003。可取查帮助文档   
  54.         wmParams.format = 1;  
  55.         wmParams.flags = 40;  
  56.   
  57.         wmParams.width = 100;//设定大小   
  58.         wmParams.height = 30;  
  59.   
  60.         wm.addView(win, wmParams);  
  61.     }  
  62. }  

其他的不用更改,直接运行即可看到效果。

源码下载:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/8月/19日/Android开发---制作桌面可移动控件

linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款