Android手机可返回主界面的可移动浮动小窗口

http://blog.sina.com.cn/s/blog_46c97a9d0100y7eb.html

 

 早上醒来在床上拿手机开了“天天动听”边听歌边玩,突然感觉“天天动听”的后台做得真的很酷,可以在桌面用手指随意移动它的窗口,还可以轻触点击它回到程序主窗口!然后想起前几天看到的Android浮动窗口实现,不知道这个“点击返回主界面”能不能也在Android实现呢!!!

    想到就动手,马上爬起床。。。。

    经过多次调试。。。

    然后就有了下面的代码!!!

    嘿嘿~~~~成功啦!!![转载]Android手机可返回主界面的可移动浮动小窗口

    我在别人的代码上修改还有增加了点功能

    程序首先进入主界面,然后按手机的“home”键退出主界面;然后创建小窗口,可以用手指移动窗口,长按小窗口1500毫秒会销毁小窗口并返回程序主界面,实现了“点击返回主界面”的功能!

   

给几张截图^__^

[转载]Android手机可返回主界面的可移动浮动小窗口


[转载]Android手机可返回主界面的可移动浮动小窗口

[转载]Android手机可返回主界面的可移动浮动小窗口

 

有三个类分三个文件存放:

 

 

第一个是:MyFloatViewActivity.java

-----------------------------------------------------------------------------

package fazai.wensi;


import net.xsmile.fv.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;

public class MyFloatViewActivity extends Activity {
   
    WindowManager wm=null;
 WindowManager.LayoutParams wmParams=null;
 MyFloatView myFV=null;

 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public void onStop(){
     super.onStop();
     createView();
    }
    @Override
    public void onRestart(){
     super.onRestart();
     wm.removeView(myFV);
    }
   
    private void createView(){
     myFV=new MyFloatView(getApplicationContext(),this);
     myFV.setImageResource(R.drawable.icon);
     //获取WindowManager
     wm=(WindowManager)getApplicationContext().getSystemService("window");
        //设置LayoutParams(全局变量)相关参数
     wmParams = ((MyApplication)getApplication()).getMywmParams();
        wmParams.type=2002;
        wmParams.flags|=8;
       
        wmParams.gravity=Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角
        //以屏幕左上角为原点,设置x、y初始值
        wmParams.x=0;
        wmParams.y=0;
       
        //设置悬浮窗口长宽数据
        wmParams.width=50;
        wmParams.height=50;
       
        //显示myFloatView图像
        wm.addView(myFV, wmParams);

    }
    @Override
    public void onDestroy(){
     super.onDestroy();
     //在程序退出(Activity销毁)时销毁悬浮窗口
     wm.removeView(myFV);
    }
}

 

--------------------------------------------------------------------------------------

 

 

第二个是:MyApplication.java

--------------------------------------------------------------------------------------

package fazai.wensi;

 

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

public class MyApplication extends Application {
 
 
 private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();


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

--------------------------------------------------------------------------------------

第三个是:MyFloatView.java

--------------------------------------------------------------------------------------

package fazai.wensi;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;

public class MyFloatView extends ImageView {
 
 private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    public Timer timer;
    int state;
    MyFloatViewActivity my;//引用Activity

   
    //对窗口的引用,为下面的窗口移动提供引用
    private WindowManager wm=(WindowManager)getContext().getApplicationContext().getSystemService("window");
    private WindowManager.LayoutParams wmParams = ((MyApplication)getContext().getApplicationContext()).getMywmParams();

 public MyFloatView(Context context,MyFloatViewActivity my) {
  super(context);
  this.my=my;//传入引用
  // 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:
          state = MotionEvent.ACTION_DOWN;
          panTime();
          //获取相对View的坐标,即以此View左上角为原点
          mTouchStartX =  event.getX(); 
                mTouchStartY =  event.getY();
                Log.i("startP", "startX"+mTouchStartX+"====startY"+mTouchStartY);//调试信息
            
             break;
         case MotionEvent.ACTION_MOVE:
          state =MotionEvent.ACTION_MOVE;
             updateViewPosition();
             break;

         case MotionEvent.ACTION_UP:
          state = MotionEvent.ACTION_UP;
          updateViewPosition();
          mTouchStartX=mTouchStartY=0;
          break;
         }
         return true;
  }
 
     private void panTime(){
         final Handler handler=new Handler(){
             public void handleMessage(Message msg){
                 switch(msg.what){
                 case 1:                 
                     Log.i("11111111111","jjjjjjjjjjjjjjjjjjjj");//调试信息
                     timer.cancel();                             //取消定时
                     Intent intent = my.getIntent();             //为了重启Activity返回Intent
                     my.finish();                                //关闭Activity
                        my.startActivity(intent);                   //重启Activity
                     break;
                 case 2:
                  Log.i("22222222222","22222222222222222222");//调试信息
                  timer.cancel();
                  break;
                 }
                 super.handleMessage(msg);
             }
         };            
         TimerTask task=new TimerTask(){
             public void run(){
                 Message message=new Message();
                 if(state == MotionEvent.ACTION_DOWN){
                     message.what=1;
                 }else {
                  message.what=2;
                 }
                 handler.sendMessage(message);
             }
         };            
         timer=new Timer();
         timer.schedule(task,1500);                              //定时
     }
    

 
  private void updateViewPosition(){
  //更新浮动窗口位置参数
  wmParams.x=(int)( x-mTouchStartX);
  wmParams.y=(int) (y-mTouchStartY);
     wm.updateViewLayout(this, wmParams);
    
  }

}
-------------------------------------------------------------------------------------------------

 

 

其实还可以实现在小窗口内提示信息的,就像“QQ”“飞信”来信息会闪烁图片那样,只要在后台开个线程,然后每隔一段时间更换小窗口的图片就可以

posted @ 2013-05-27 09:26  水——云——间  阅读(713)  评论(0编辑  收藏  举报