点击一个ImageView让它至于控件的最前面,该如何处理

如题所述

  先谈一下基本的实现原理,这种桌面悬浮窗的效果很类似与Widget,但是它比Widget要灵活的多。主要是通过WindowManager这个类来实现的,调用这个类的addView方法用于添加一个悬浮窗,updateViewLayout方法用于更新悬浮窗的参数,removeView用于移除悬浮窗。其中悬浮窗的参数有必要详细说明一下。
  WindowManager.LayoutParams这个类用于提供悬浮窗所需的参数,其中有几个经常会用到的变量:
  type值用于确定悬浮窗的类型,一般设为2002,表示在所有应用程序之上,但在状态栏之下。
  flags值用于确定悬浮窗的行为,比如说不可聚焦,非模态对话框等等,属性非常多,大家可以查看文档。

  gravity值用于确定悬浮窗的对齐方式,一般设为左上角对齐,这样当拖动悬浮窗的时候方便计算坐标。
  x值用于确定悬浮窗的位置,如果要横向移动悬浮窗,就需要改变这个值。
  y值用于确定悬浮窗的位置,如果要纵向移动悬浮窗,就需要改变这个值。
  width值用于指定悬浮窗的宽度。
  height值用于指定悬浮窗的高度。
  创建悬浮窗这种窗体需要向用户申请权限才可以的,因此还需要在AndroidManifest.xml中加入<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  原理介绍完了,下面我们开始用代码实现。首先在Eclipse中新建一个Android项目,项目名就叫做360FloatWindowDemo。然后写一下布局文件,布局文件非常简单,只有一个按钮,打开或新建activity_main.xml,加入如下代码:

  [html] view plain copy
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  tools:context=".MainActivity" >
  <Button
  android:id="@+id/start_float_window"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start Float Window" >
  </Button>
  </RelativeLayout>

  然后再新建一个名为float_window_small.xml的布局文件,用于做为小悬浮窗的布局,在其中加入如下代码:

  [html] view plain copy
  <?xml version="1.0" encoding="UTF-8"?>
  <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/small_window_layout"
  android:layout_width="60dip"
  android:layout_height="25dip"
  android:background="@drawable/bg_small"
  >
  <TextView
  android:id="@+id/percent"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:textColor="#ffffff"
  />
  </LinearLayout>
  再新建一个名为float_window_big.xml的布局文件,用于做为大悬浮窗的布局,在其中加入如下代码:
  [html] view plain copy
  <?xml version="1.0" encoding="UTF-8"?>
  <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/big_window_layout"
  android:layout_width="200dip"
  android:layout_height="100dip"
  android:background="@drawable/bg_big"
  android:orientation="vertical"
  >
  <Button
  android:id="@+id/close"
  android:layout_width="100dip"
  android:layout_height="40dip"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="12dip"
  android:text="关闭悬浮窗"
  />
  <Button
  android:id="@+id/back"
  android:layout_width="100dip"
  android:layout_height="40dip"
  android:layout_gravity="center_horizontal"
  android:text="返回"
  />
  </LinearLayout>
  两个悬浮窗布局文件中用到的图片资源,大家可以随便找点图片来代替,同时我会给出源码,大家也可以从源码中取出。

  然后打开或创建MainActivity,这是项目的主界面,在里面加入如下代码:
  [java] view plain copy
  public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button startFloatWindow = (Button) findViewById(R.id.start_float_window);
  startFloatWindow.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View arg0) {
  Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
  startService(intent);
  finish();
  }
  });
  }
  }
  这里可以看到,MainActivity的代码非窗简单,就是对开启悬浮窗的按钮注册了一个点击事件,用于打开一个服务,然后关闭当前Activity。创建悬浮窗的逻辑都交给服务去做了。
温馨提示:答案为网友推荐,仅供参考