android 浮窗

效果:

1. 运行

2.点击图中叹号浮窗

 

代码如下:

1.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.xsmile.fv"
      android:versionCode="1"
      android:versionName="1.0">
    
    <uses-sdk android:minSdkVersion="8" />
    
    <application android:name=".MyApplication" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyFloatViewActivity"
                  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>
    
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

</manifest> 

 

2.MyApplication.java

package net.xsmile.fv;

import android.app.Application;
import android.view.WindowManager;

public class MyApplication extends Application {
	
	/**
	 * 创建全局变量
	 * 全局变量一般都比较倾向于创建一个单独的数据类文件,并使用static静态变量
	 * 
	 * 这里使用了在Application中添加数据的方法实现全局变量
	 * 注意在AndroidManifest.xml中的Application节点添加android:name=".MyApplication"属性
	 * 
	 */
	private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();

	public WindowManager.LayoutParams getMywmParams(){
		return wmParams;
	}
}


3.MyFloatViewActivity.java

package net.xsmile.fv;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class MyFloatViewActivity extends Activity {
    /** Called when the activity is first created. */
	/*用windowManager创建的View,当不需要时,务必记住使用windowManager的removeView方法来移除,请在Activity相关生命周期中自行添加扫尾工作*/
	private WindowManager wm=null;
	/*悬浮窗口坐标的移动实际是windowMananager.LayoutParams中x和y的变换,但是要注意设置相应的gravity*/
	private WindowManager.LayoutParams wmParams=null;
	/*悬浮View的代码,这里用一个ImageView作为演示*/
	private MyFloatView myFV=null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //创建悬浮窗口
        createView();
    }
  
    private void createView(){
    	myFV=new MyFloatView(getApplicationContext());
    	myFV.setImageResource(R.drawable.warn);
    	//获取WindowManager
    	wm=(WindowManager)getApplicationContext().getSystemService("window");
        //设置LayoutParams(全局变量)相关参数
    	wmParams = ((MyApplication)getApplication()).getMywmParams();

         /**
         *以下都是WindowManager.LayoutParams的相关属性
         * 具体用途可参考SDK文档
         */
        wmParams.type=LayoutParams.TYPE_PHONE;   //设置window type
        wmParams.format=PixelFormat.RGBA_8888;   //设置图片格式,效果为背景透明

        //设置Window flag
        /*
         * FLAG_NOT_TOUCH_MODAL    = 0x00000020;(十六进制)-->32(十进制)-->100000(二进制)
         * FLAG_NOT_FOCUSABLE      = 0x00000008;(十六进制)--> 8(十进制)-->001000(二进制)
         * LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE = 101000(二进制)-->40(十进制)-->0x0028(十六进制)
         **/
        wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
                              | LayoutParams.FLAG_NOT_FOCUSABLE;
        /*
         * 下面的flags属性的效果形同“锁定”。
         * 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。
         wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL 
                               | LayoutParams.FLAG_NOT_FOCUSABLE
                               | LayoutParams.FLAG_NOT_TOUCHABLE;
        */
        
        wmParams.gravity=Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角
        //以屏幕左上角为原点,设置x、y初始值
        wmParams.x=0;
        wmParams.y=0;
        
        //设置悬浮窗口长宽数据
        wmParams.width=80;
        wmParams.height=80;
    
        //显示myFloatView图像
        wm.addView(myFV, wmParams);
    	
    }
    
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	//在程序退出(Activity销毁)时销毁悬浮窗口
    	wm.removeView(myFV);
    }
    
}


4.MyFloatView.java

package net.xsmile.fv;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyFloatView extends ImageView {
	Context context = null;
	
	private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    
    private WindowManager wm=(WindowManager)getContext().getApplicationContext().getSystemService("window");
    
    //此wmParams为获取的全局变量,用以保存悬浮窗口的属性
    private WindowManager.LayoutParams wmParams = ((MyApplication)getContext().getApplicationContext()).getMywmParams();

	public MyFloatView(Context context) {
		super(context);		
		this.context = context;
		// TODO Auto-generated constructor stub
	}
	
	 @Override
	 public boolean onTouchEvent(MotionEvent event) {
		 //获取相对屏幕的坐标,即以屏幕左上角为原点		 
	     x = event.getRawX();   
	     y = event.getRawY()-25;   //25是系统状态栏的高度
	     Log.i("currP", "currX"+x+"====currY"+y);
	     switch (event.getAction()) {
	        case MotionEvent.ACTION_DOWN:
	        	//获取相对View的坐标,即以此View左上角为原点
	        	mTouchStartX =  event.getX();  
                mTouchStartY =  event.getY();
                
	            Log.i("startP", "startX"+mTouchStartX+"====startY"+mTouchStartY);
	            //
	            showToast();

	            break;
	        case MotionEvent.ACTION_MOVE:	            
	            updateViewPosition();
	            break;

	        case MotionEvent.ACTION_UP:
	        	updateViewPosition();
	        	mTouchStartX=mTouchStartY=0;
	        	break;
	        }
	        return true;
		}
	 
	 private void updateViewPosition(){
		//更新浮动窗口位置参数
		wmParams.x=(int)( x-mTouchStartX);
		wmParams.y=(int) (y-mTouchStartY);
	    wm.updateViewLayout(this, wmParams);
	    
	 }
	 
	 private void showToast(){  
		LayoutInflater li=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
		View view=li.inflate(R.layout.toast,null);  
		//把布局文件toast.xml转换成一个view  
		Toast toast=new Toast(context);  
		toast.setView(view);  
		//载入view,即显示toast.xml的内容  
		TextView tv=(TextView)view.findViewById(R.id.tv1);  
		tv.setText("Toast显示View内容");  
		//修改TextView里的内容  
		toast.setDuration(Toast.LENGTH_SHORT);  
		//设置显示时间,长时间Toast.LENGTH_LONG,短时间为Toast.LENGTH_SHORT,不可以自己编辑  
		toast.show();  
	}  

}


 

5.layout --> toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:src="@drawable/kindly_reminder" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

 

6.res 图片略。

 

源码下载URL:http://download.csdn.net/detail/xufenghappy6/4470206

如果没有资源分的可以给我留言,我会提供完整代码(其实此篇文章已经很完整了)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值