标签:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />2、在自定义的悬浮窗控件中,要设置触摸监听器,并根据用户的手势滑动来相应调整窗口位置,以实现悬浮窗的拖动功能;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class FloatView extends View {
private final static String TAG = "FloatView";
private Context mContext;
private WindowManager wm;
private static WindowManager.LayoutParams wmParams;
public View mContentView;
private float mRelativeX;
private float mRelativeY;
private float mScreenX;
private float mScreenY;
private boolean bShow = false;
public FloatView(Context context) {
super(context);
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wmParams == null) {
wmParams = new WindowManager.LayoutParams();
}
mContext = context;
}
public void setLayout(int layout_id) {
mContentView = LayoutInflater.from(mContext).inflate(layout_id, null);
mContentView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
mScreenX = event.getRawX();
mScreenY = event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mRelativeX = event.getX();
mRelativeY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
updateViewPosition();
mRelativeX = mRelativeY = 0;
break;
}
return true;
}
});
}
private void updateViewPosition() {
wmParams.x = (int) (mScreenX - mRelativeX);
wmParams.y = (int) (mScreenY - mRelativeY);
wm.updateViewLayout(mContentView, wmParams);
}
public void show() {
if (mContentView != null) {
wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wmParams.alpha = 1.0f;
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
wmParams.x = 0;
wmParams.y = 0;
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// 显示自定义悬浮窗口
wm.addView(mContentView, wmParams);
bShow = true;
}
}
public void close() {
if (mContentView != null) {
wm.removeView(mContentView);
bShow = false;
}
}
public boolean isShow() {
return bShow;
}
}import com.example.exmfloat.widget.FloatView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StaticActivity extends Activity implements OnClickListener {
private FloatView mFloatView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_static);
Button btn_static_open = (Button) findViewById(R.id.btn_static_open);
Button btn_static_close = (Button) findViewById(R.id.btn_static_close);
btn_static_open.setOnClickListener(this);
btn_static_close.setOnClickListener(this);
mFloatView = new FloatView(MainApplication.getInstance());
mFloatView.setLayout(R.layout.float_static);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_static_open) {
if (mFloatView!=null && mFloatView.isShow()==false) {
mFloatView.show();
}
} else if (v.getId() == R.id.btn_static_close) {
if (mFloatView!=null && mFloatView.isShow()==true) {
mFloatView.close();
}
}
}
}import android.app.Application;
public class MainApplication extends Application {
private static MainApplication mApp;
public static MainApplication getInstance() {
return mApp;
}
@Override
public void onCreate() {
super.onCreate();
mApp = this;
}
}import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.exmfloat.service.TrafficService;
public class TrafficActivity extends Activity implements OnClickListener {
private final static String TAG = "TrafficActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_traffic);
Button btn_traffic_open = (Button) findViewById(R.id.btn_traffic_open);
Button btn_traffic_close = (Button) findViewById(R.id.btn_traffic_close);
btn_traffic_open.setOnClickListener(this);
btn_traffic_close.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_traffic_open) {
Intent intent = new Intent(this, TrafficService.class);
intent.putExtra("type", TrafficService.OPEN);
startService(intent);
} else if (v.getId() == R.id.btn_traffic_close) {
Intent intent = new Intent(this, TrafficService.class);
intent.putExtra("type", TrafficService.CLOSE);
startService(intent);
}
}
}import com.example.exmfloat.MainApplication;
import com.example.exmfloat.R;
import com.example.exmfloat.util.FlowUtil;
import com.example.exmfloat.widget.FloatView;
import android.app.Service;
import android.content.Intent;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
public class TrafficService extends Service {
private final static String TAG = "TrafficService";
public static int OPEN = 0;
public static int CLOSE = 1;
private long curRx;
private long curTx;
private FloatView mFloatView;
private TextView tv_traffic;
private final int delayTime = 2000;
private Handler mHandler = new Handler();
private Runnable mRefresh = new Runnable() {
public void run() {
if (mFloatView != null && mFloatView.isShow() == true &&
(TrafficStats.getTotalRxBytes()>curRx || TrafficStats.getTotalTxBytes()>curTx)) {
long a = ((TrafficStats.getTotalRxBytes() - curRx) + (TrafficStats
.getTotalTxBytes() - curTx)) / 2;
String desc = String.format("当前流量: %s/S", FlowUtil.BToShowStringNoDecimals(a));
tv_traffic.setText(desc);
curRx = TrafficStats.getTotalRxBytes();
curTx = TrafficStats.getTotalTxBytes();
}
mHandler.postDelayed(this, delayTime);
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
if (mFloatView == null) {
mFloatView = new FloatView(MainApplication.getInstance());
mFloatView.setLayout(R.layout.float_traffic);
tv_traffic = (TextView) mFloatView.mContentView.findViewById(R.id.tv_traffic);
}
curRx = TrafficStats.getTotalRxBytes();
curTx = TrafficStats.getTotalTxBytes();
mHandler.postDelayed(mRefresh, 0);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
int type = intent.getIntExtra("type", OPEN);
if (type == OPEN) {
if (mFloatView != null && mFloatView.isShow() == false) {
mFloatView.show();
}
} else if (type == CLOSE) {
if (mFloatView != null && mFloatView.isShow() == true) {
mFloatView.close();
}
stopSelf();
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRefresh);
}
}import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.exmfloat.service.StockService;
public class StockActivity extends Activity implements OnClickListener {
private final static String TAG = "StockActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock);
Button btn_stock_open = (Button) findViewById(R.id.btn_stock_open);
Button btn_stock_close = (Button) findViewById(R.id.btn_stock_close);
btn_stock_open.setOnClickListener(this);
btn_stock_close.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_stock_open) {
Intent intent = new Intent(this, StockService.class);
intent.putExtra("type", StockService.OPEN);
startService(intent);
} else if (v.getId() == R.id.btn_stock_close) {
Intent intent = new Intent(this, StockService.class);
intent.putExtra("type", StockService.CLOSE);
startService(intent);
}
}
}import com.example.exmfloat.MainApplication;
import com.example.exmfloat.R;
import com.example.exmfloat.http.HttpReqData;
import com.example.exmfloat.http.HttpRespData;
import com.example.exmfloat.http.HttpUrlUtil;
import com.example.exmfloat.widget.FloatView;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.TextView;
public class StockService extends Service {
private final static String TAG = "StockService";
public static int OPEN = 0;
public static int CLOSE = 1;
private FloatView mFloatView;
private TextView tv_sh_stock, tv_sz_stock;
private final int delayTime = 5000;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//上证指数,3019.9873,-5.6932,-0.19,1348069,14969598
String desc = (String) msg.obj;
String[] array = desc.split(",");
String stock = array[1];
float distance = Float.parseFloat(array[2]);
String range = array[3];
String text = String.format("%s %s%%", stock, range);
int type = msg.what;
if (type == SHANGHAI) {
tv_sh_stock.setText(text);
if (distance > 0) {
tv_sh_stock.setTextColor(Color.RED);
} else {
tv_sh_stock.setTextColor(Color.GREEN);
}
} else if (type == SHENZHEN) {
tv_sz_stock.setText(text);
if (distance > 0) {
tv_sz_stock.setTextColor(Color.RED);
} else {
tv_sz_stock.setTextColor(Color.GREEN);
}
}
}
};
private Runnable mRefresh = new Runnable() {
@Override
public void run() {
if (mFloatView != null && mFloatView.isShow() == true ) {
new StockThread(SHANGHAI).start();
new StockThread(SHENZHEN).start();
}
mHandler.postDelayed(this, delayTime);
}
};
private static int SHANGHAI = 0;
private static int SHENZHEN = 1;
private class StockThread extends Thread {
private int mType;
public StockThread(int type) {
mType = type;
}
@Override
public void run() {
HttpReqData req_data = new HttpReqData();
if (mType == SHANGHAI) {
req_data.url = "http://hq.sinajs.cn/list=s_sh000001";
} else if (mType == SHENZHEN) {
req_data.url = "http://hq.sinajs.cn/list=s_sz399001";
}
HttpRespData resp_data = HttpUrlUtil.getData(req_data);
//var hq_str_s_sh000001="上证指数,3019.9873,-5.6932,-0.19,1348069,14969598";
String desc = resp_data.content;
Message msg = Message.obtain();
msg.what = mType;
msg.obj = desc.substring(desc.indexOf("\"")+1, desc.lastIndexOf("\""));
mHandler.sendMessage(msg);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
if (mFloatView == null) {
mFloatView = new FloatView(MainApplication.getInstance());
mFloatView.setLayout(R.layout.float_stock);
tv_sh_stock = (TextView) mFloatView.mContentView.findViewById(R.id.tv_sh_stock);
tv_sz_stock = (TextView) mFloatView.mContentView.findViewById(R.id.tv_sz_stock);
}
mHandler.postDelayed(mRefresh, 0);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
int type = intent.getIntExtra("type", OPEN);
if (type == OPEN) {
if (mFloatView != null && mFloatView.isShow() == false) {
mFloatView.show();
}
} else if (type == CLOSE) {
if (mFloatView != null && mFloatView.isShow() == true) {
mFloatView.close();
}
stopSelf();
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRefresh);
}
}标签:
原文地址:http://blog.csdn.net/aqi00/article/details/52173979