android悬浮球,应用在所有软件之上,点击可以显示菜单
来源:互联网 发布:c语言入门自学免费视频 编辑:程序博客网 时间:2024/05/16 22:46
自定义文件
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; }}
阅读全文
0 0
- android悬浮球,应用在所有软件之上,点击可以显示菜单
- Android 如何让悬浮窗口覆盖显示在导航栏之上?
- WindowManager——悬浮在所有app之上
- Android 如何让某个view悬浮在其他界面之上?
- android 设置背景不跟随通知栏显示和隐藏变化(就如通知栏悬浮在窗口之上)
- android 应用内悬浮框,并在指定页面显示
- 点击图片显示放大的图片,并悬浮于窗体之上
- Android 自定义Toast显示(不限时+在其他应用之上显示)
- 在android应用底部显示菜单
- 让组件悬浮所有页面之上
- WindowManager实现可移动可点击(可只在应用中显示)悬浮窗
- Android PopupWindow在控制之上显示
- Android 悬浮窗在状态栏之上不影响其他操作及悬浮窗自身控制
- Android之上下文菜单
- android AutoCompleteTextView点击显示所有
- 让DIV悬浮在SilverLight之上
- Android悬浮窗置于任务栏之上
- Android MediaPlayer 简单综合应用------列出sdcard里所有.mp3文件,并且可以点击播放!
- lintcode --最后一个单词的长度
- ZooKeeper学习总结(3)——ZooKeeper常见面试题
- Java经典问题算法大全
- C陷阱与缺陷第六章 预处理器
- Java String 类
- android悬浮球,应用在所有软件之上,点击可以显示菜单
- hrbust 1474 球字典序的最大的字串 (贪心)
- DUBBO 详细介绍
- c#笔记(四)
- FEC原理及其实现
- java重点知识
- 块存储、对象存储和文件系统: 它们对容器而言意味着什么?
- php js实现数据库四级分类栏目(可以做到7级)
- String 转码