Android中浮动窗口案例(续)


在上一篇中我们接触了浮动窗口,可惜窗口不能拖动,那么现在我们实现拖动功能,只需要将调用MyService部分改成FloatingService即可。

1.floating.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">   
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/floating"
        android:gravity="center_vertical">   
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:src="http://zouqinghua11111.blog.163.com/@drawable/upload"/>
     <TextView  
            android:id="@+id/flowing"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"   
            android:layout_weight="0.5"  
            android:gravity="right" 
            android:text="0K/s" 
            android:textColor="#000000"/>
         <ImageView 
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_weight="0.5"  
            android:src="http://zouqinghua11111.blog.163.com/@drawable/download"/> 
          <TextView  
            android:gravity="right"  
            android:id="@+id/flowspeed"  
            android:paddingRight="10.0dip"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_weight="0.5" 
            android:text="17K/s" 
            android:textColor="#000000"/> 
    </LinearLayout>
        <ImageButton  
        android:id="@+id/floating_button_hide"  
        android:background="@drawable/floatingwindowhidebutton"  
        android:visibility="gone" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        style="\?android:buttonStyleSmall"/>
</LinearLayout>


2.FloatingService.java
package com.example.bartest;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager;

public class FloatingService extends Service{
    private int statusBarHeight;
    private View view;
    private boolean viewAdded = false;
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
   
    @Override
    public void onCreate()
    {
        super.onCreate();
        view = LayoutInflater.from(this).inflate(R.layout.floating, null);
        windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
       
        layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_ERROR,
                LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
       
        layoutParams.gravity = Gravity.LEFT |Gravity.TOP;
        view.setOnTouchListener(new OnTouchListener(){
           
            float[] temp = new float[]{0f,0f};
           
            public boolean onTouch(View v,MotionEvent event)
            {
                layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
                int eventaction = event.getAction();
                switch(eventaction)
                {
                case MotionEvent.ACTION_DOWN:
                    temp[0] = event.getX();
                    temp[1] = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    refreshView((int)(event.getRawX() - temp[0]),(int)(event.getRawY() - temp[1]));
                    break;
               
                }
                return true;
            }           
        });
    }

    public void refreshView(int x,int y)
    {
        if(statusBarHeight == 0)
        {
            View rootView = view.getRootView();
            Rect r = new Rect();
            rootView.getWindowVisibleDisplayFrame(r);
            statusBarHeight = r.top;
        }
        layoutParams.x = x;
       
        layoutParams.y = y - statusBarHeight;
        refresh();
       
    }

    private void refresh()
    {
        if(viewAdded)
        {
            windowManager.updateViewLayout(view, layoutParams);
        }else
        {
            windowManager.addView(view, layoutParams);
            viewAdded = true;
        }
    }

    @Override
    public void onStart(Intent intent,int startId)
    {
        super.onStart(intent, startId);
        refresh();
    }

    public void removeView()
    {
        if(viewAdded)
        {
            windowManager.removeView(view);
            viewAdded = false;
        }
    }
   
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        removeView();
    }
   
    class StatusBarReceiver extends BroadcastReceiver{ 
        @Override
        public void onReceive(Context context,Intent intent)
        {
           
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值