Android 浮动按钮的实现

先看看效果图

这个android的小图标就是可以随意拖动并且浮动在所有界面上的View

 

 

1.先建一个类继承Application。

  1)我们自定义Appliction类的作用一般是用来数据的传递和放一些全局的和一些上下文都要用到变量和方法之类的。

  2)FloatApplication的作用就是返回WindowManager.LayoutParams

1 public class FloatApplication  extends Application{
2     private WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams(); 
3     public WindowManager.LayoutParams getWindowParams() { 
4     return windowParams; 
5     } 
6 }

2.重写一下ImageView,这个ImageView就是用来显示那个android图标并且会浮动在界面上的一个View

这个FloatView是最重要的,只要在Activity使用FloatView这个空间就可以实现漂浮按钮的功能了。

public class FloatView extends ImageView{
    private float x; 
    private float y; 
    
    private float mTouchX; 
    private float mTouchY; 
    
    private float mStartX; 
    private float mStartY; 
    
    private OnClickListener mClickListener; 
    
    private WindowManager windowManager = (WindowManager) getContext() 
            .getApplicationContext().getSystemService(Context.WINDOW_SERVICE); 
    private WindowManager.LayoutParams windowManagerParams = ((FloatApplication) getContext() 
            .getApplicationContext()).getWindowParams(); 

    public FloatView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    @Override 
    public boolean onTouchEvent(MotionEvent event){

        Rect frame = new Rect(); 
        getWindowVisibleDisplayFrame(frame); 
        int statusBarHeight = frame.top;
        System.out.println("statusBarHeight:"+statusBarHeight); 

        x = event.getRawX(); 
        y = event.getRawY() - statusBarHeight; 
        Log.i("tag", "currX" + x + "====currY" + y); 
        
        switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
        mTouchX = event.getX(); 
        mTouchY = event.getY(); 
        mStartX = x; 
        mStartY = y; 
        Log.i("tag", "startX" + mTouchX + "====startY" 
        + mTouchY); 
        break; 
        case MotionEvent.ACTION_MOVE: 
        updateViewPosition(); 
        break; 
        case MotionEvent.ACTION_UP: 
        updateViewPosition(); 
        mTouchX = mTouchY = 0; 
        if ((x - mStartX) < 5 && (y - mStartY) < 5) { 
        if(mClickListener!=null) { 
        mClickListener.onClick(this); 
        } 
        } 
        break; 
        } 
        return false;
        
    }

    @Override 
    public void setOnClickListener(OnClickListener l) { 
        this.mClickListener = l; 
    }
    
    private void updateViewPosition() { 
        windowManagerParams.x = (int) (x - mTouchX); 
        windowManagerParams.y = (int) (y - mTouchY); 
        windowManager.updateViewLayout(this, windowManagerParams); 
        } 
}

3.在MainActivity上测试FloatView

public class MainActivity extends Activity implements OnClickListener{

    private WindowManager windowManager = null; 
    private WindowManager.LayoutParams windowManagerParams = null; 
    private FloatView floatView = null; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏 
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , 
        WindowManager.LayoutParams. FLAG_FULLSCREEN);//全屏 
        setContentView(R.layout.activity_main);
        createView() ;
    }
    private void createView() { 
        floatView = new FloatView(getApplicationContext()); 
        floatView.setOnClickListener(this); 
        floatView.setImageResource(R.drawable.ic_launcher); 
        windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); 
        windowManagerParams = ((FloatApplication) getApplication()).getWindowParams(); 
        windowManagerParams.type = LayoutParams.TYPE_PHONE; 
        windowManagerParams.format = PixelFormat.RGBA_8888; 
        windowManagerParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL 
        | LayoutParams.FLAG_NOT_FOCUSABLE; 
        windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP; 
        windowManagerParams.x = 0; 
        windowManagerParams.y = 0; 
        windowManagerParams.width = LayoutParams.WRAP_CONTENT; 
        windowManagerParams.height = LayoutParams.WRAP_CONTENT; 
        windowManager.addView(floatView, windowManagerParams); 
        }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show(); 
    } 
}

4.在配置文件上添加权限和添加样式

添加权限  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
添加样式  android:theme="@style/AppTheme" android:name="FloatApplication" 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:name="FloatApplication" >
        <activity
            android:name="com.example.test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

关于悬浮窗口更详细的一篇文章  http://hi.baidu.com/419848854/item/7e4d86c8e2b1c6300931c683

posted @ 2013-05-09 17:43  大暴雨  阅读(9385)  评论(3编辑  收藏  举报