自定义文件
package com.example.view;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import com.example.mydemo.R;
import static android.R.attr.x;
import static android.R.attr.y;
/**
* Created by Administrator on 2017/7/18.
*/
public class FloatBall extends View {
private int scale = 10;
public int width = 25;
public int r = width;
private Paint mPaint;
private final float circleScale = 3.0f / 3;
//中间的圆球
private Bitmap pinWheelBmp;
private float textAlpha = 0;
public FloatBall(Context context) {
this(context, null);
//init();
}
public FloatBall(Context context, AttributeSet attrs) {
this(context, attrs, 0);
//init();
}
public FloatBall(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
/*mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
mPaint.setTextSize(10);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
setBackgroundColor(Color.TRANSPARENT);
r = width = Utils.dp2pix(getContext(), width);*/
//设置悬浮球画笔
mPaint = new Paint();
//mPaint.setColor(Color.BLACK);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
mPaint.setTextSize(5);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
setBackgroundColor(Color.TRANSPARENT);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(2*r,2*r);
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setARGB((int) (255), 0, 0, 0);
int width = getMeasuredWidth() / 2;
int height = getMeasuredHeight() / 2;
// 限定圆球移动的圆圈,该圆圈小于圆球的移动范围
canvas.drawCircle(width, height,r*circleScale, mPaint);
pinWheelBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
// 限定圆球的大小
pinWheelBmp = Bitmap.createScaledBitmap(pinWheelBmp, r, r, true);
// Matrix matrix = new Matrix();
//通过matrix控制圆球的移动
// matrix.preTranslate(getMeasuredWidth() / 2 - pinWheelBmp.getWidth() / 2 + x / scale, getMeasuredHeight() / 2 - pinWheelBmp.getHeight() / 2 + y / scale);
canvas.drawBitmap(pinWheelBmp, null, null);
/* int width = getMeasuredWidth() / 2;
int height = getMeasuredHeight() / 2;
canvas.drawCircle(width,height,r*circleScale,mPaint);*/
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}
package com.example.view;
//悬浮球窗口管理
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.mydemo.LoactionActivity;
import com.example.mydemo.R;
import com.example.mydemo.ShuyeActivity;
/**
* Created by Administrator on 2017/7/18.
*/
public class WindowViewManager
{
private static WindowViewManager viewManager;
private WindowManager windowManager;
private FloatBall floatBall;
private boolean ismove;
private static Vibrator sVibrator;
private WindowViewManager() {}
public static WindowViewManager getViewManagerInstance()
{
if(viewManager == null)
viewManager = new WindowViewManager();
return viewManager;
}
public void show(final Context context)
{
sVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
floatBall = new FloatBall(context.getApplicationContext());
windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
// floatBall = new FloatBall(context);
//floatBall.setBackgroundColor(Color.parseColor("#EBEBEB"));
//layoutParams.type = WindowManager.LayoutParams.TYPE_TOAS
//layoutParams.gravity= Gravity.START |Gravity.TOP;
layoutParams.type=WindowManager.LayoutParams.TYPE_PHONE;
//设置图片格式,效果为背景透明
//layoutParams.format = PixelFormat.RGBA_8888;
/*layoutParams.width = floatBall.width;
layoutParams.height = floatBall.r;*/
final int width = floatBall.getMeasuredWidth() * 2;
final int height = floatBall.getMeasuredHeight() * 2;
layoutParams.width=width;
layoutParams.height=height;
//layoutParams.flags = 40;
//设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.format = PixelFormat.RGBA_8888 | PixelFormat.TRANSLUCENT;
//layoutParams1.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR; //竖屏
//layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;//横屏
//floatBall.setLayoutParams(layoutParams);
windowManager.addView(floatBall, layoutParams);
floatBall.setOnTouchListener(new View.OnTouchListener()
{
float startX;
float startY; // 原位置
@Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
float x = event.getRawX();
float y = event.getRawY(); // 新位置
if(action == MotionEvent.ACTION_DOWN)
{
ismove=true;
startX = x;
startY = y;
floatBall.postDelayed(sRunnable, 50);
}
else if(action == MotionEvent.ACTION_MOVE)
{
x = event.getRawX();
y = event.getRawY();
layoutParams.x += (int)(x - startX);
layoutParams.y += (int)(y - startY);
windowManager.updateViewLayout(floatBall, layoutParams);
startX = x;
startY = y;
floatBall.removeCallbacks(sRunnable);
ismove = true;
floatBall.postInvalidate();
}
else if(action == MotionEvent.ACTION_UP)
{
ismove=false;
startX = x;
startY = y;
}
return false;
}
});
floatBall.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(!ismove){
init(context);
}else {
// init(context);
}
/* final View view = LayoutInflater.from(context).inflate(R.layout.floating_activity, null);
view.setBackgroundColor(Color.parseColor("#ffdddddd"));
final WindowManager.LayoutParams layoutParams1 = new WindowManager.LayoutParams();
layoutParams1.width = 50;
layoutParams1.height =50;
layoutParams1.type = WindowManager.LayoutParams.TYPE_PHONE;
layoutParams1.flags = 40;
windowManager.addView(view, layoutParams1);
// Toast.makeText(context,"你的点击",Toast.LENGTH_LONG).show();
TextView floating_imageView= (TextView) view.findViewById(R.id.floating_imageView);
floating_imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context, ShuyeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});*/
}
});
}
public void close()
{
windowManager.removeView(floatBall);
}
private static Runnable sRunnable = new Runnable() {
@Override
public void run() {
Log.d("ndh--", "vibrate---");
sVibrator.vibrate(50);
}
};
//显示菜单
public void init(final Context context){
final View view = LayoutInflater.from(context).inflate(R.layout.floating_activity, null);
//view.setBackgroundColor(Color.parseColor("#FFFFFF"));
final WindowManager.LayoutParams layoutParams1 = new WindowManager.LayoutParams();
Resources resources=context.getResources();
Drawable drawable=resources.getDrawable(R.drawable.bugg);
view.setBackgroundDrawable(drawable);
// layoutParams1.format=PixelFormat.RGBA_8888;
view.getBackground().setAlpha(100);
//layoutParams1.width =400;
//layoutParams1.height =280;
layoutParams1.width=WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams1.height=WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams1.type = WindowManager.LayoutParams.TYPE_PHONE;
//layoutParams1.type =WindowManager.LayoutParams.TYPE_TOAST;
// layoutParams1.flags = 40;
//设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
layoutParams1.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//layoutParams1.format = PixelFormat.RGBA_8888 | PixelFormat.TRANSLUCENT;
//layoutParams1.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR; //竖屏
layoutParams1.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;//横屏
windowManager.addView(view, layoutParams1);
LinearLayout zhuye= (LinearLayout) view.findViewById(R.id.zhuye);
LinearLayout nengxiao= (LinearLayout) view.findViewById(R.id.nengxiao);
LinearLayout fanhui= (LinearLayout) view.findViewById(R.id.fanhui);
LinearLayout dingwei= (LinearLayout) view.findViewById(R.id.dingwei);
zhuye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// windowManager.removeView(view);
goHome(context);
closeView(view);
}
});
nengxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Intent intent=new Intent(context, ShuyeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);*/
// windowManager.removeView(view);
nengxiaoapp(context);
closeView(view);
}
});
fanhui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// goHome(context);
//windowManager.removeView(view);
closeView(view);
}
});
dingwei.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Intent intent=new Intent(context, LoactionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);*/
appd(context);
closeView(view);
}
});
}
//返回桌面
private void goHome(Context context) {
Intent home = new Intent(Intent.ACTION_MAIN);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.addCategory(Intent.CATEGORY_HOME);
home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(home);
}
//移除弹出框
private void closeView(View view){
windowManager.removeView(view);
}
//跳转定位系统
public void appd(Context context){
// 通过包名获取要跳转的app,创建intent对象
Intent intent =context.getPackageManager().getLaunchIntentForPackage("com.hospital.ruijin");
// 这里如果intent为空,就说名没有安装要跳转的应用嘛
if (intent != null) {
// 这里跟Activity传递参数一样的嘛,不要担心怎么传递参数,还有接收参数也是跟Activity和Activity传参数一样
/* intent.putExtra("name", "Liu xiang");
intent.putExtra("birthday", "1992-7-13");*/
context.startActivity(intent);
} else {
// 没有安装要跳转的app应用,提醒一下
Toast.makeText(context.getApplicationContext(), "哟,赶紧下载安装这个APP吧", Toast.LENGTH_LONG).show();
}
}
//跳转能效系统
public void nengxiaoapp(Context context){
try {
Uri uri = Uri.parse("http:/");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
} catch (Exception e) {
e.printStackTrace();
Intent viewIntent = new
Intent("android.intent.action.VIEW", Uri.parse("http://"));
context.startActivity(viewIntent);
}
}
}
}
开启一个服务
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.example.view.WindowViewManager;
/**
* Created by Administrator on 2017/7/18.
*/
public class ViewService extends Service {
@Override
public void onCreate() {
super.onCreate();
WindowViewManager.getViewManagerInstance().show(this);
}
@Override
public void onDestroy() {
WindowViewManager.getViewManagerInstance().close();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}