今天我们接着昨天的继续
1、实现控制球的点击拖动
2、点击控制球后d出菜单选择界面
3、菜单选择功能
其实这几个都不难,思路如下:1、要实现我们的控制球悬浮在所有App界面之上:通过WindowManager来设置App的显示方式
2、控制球的拖动:重写ontouchListener即可,这个相信很多人都做过了
3、d出菜单界面和菜单选择功能:这个就是一个比较好看的PopupWindow
通过这几条分析,我们可以发现,这个项目其实还是比较简单的哈,所以说,我一直坚信的,思路很重要
下面我们一点点来实现吧:so easy
程序核心必然是一个Service,相信我不要多说了:
MainService:
package com.xys.xtouchassistant.service;import com.xys.xtouchassistant.R;import androID.app.Service;import androID.content.Context;import androID.content.Intent;import androID.graphics.color;import androID.graphics.PixelFormat;import androID.graphics.drawable.colorDrawable;import androID.graphics.drawable.Drawable;import androID.os.IBinder;import androID.provIDer.Settings;import androID.util.displayMetrics;import androID.vIEw.Gravity;import androID.vIEw.LayoutInflater;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEw.OntouchListener;import androID.vIEw.WindowManager;import androID.Widget.button;import androID.Widget.PopupWindow;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainService extends Service implements OnClickListener, OntouchListener { // 控制球VIEw private VIEw touchBallVIEw; // d出菜单VIEw private VIEw menuVIEw; // 控制球 private button touchBall; // WindowManager WindowManager wm; // WindowManagerParams WindowManager.LayoutParams params; private PopupWindow popup; // 点击坐标 private float x, y; private float touchX, touchY; // 移动Flag private boolean isMoving; // Menu上的组件 private TextVIEw tvApps; private TextVIEw tvHomeScreen; private TextVIEw tvSetting; private TextVIEw tvLockScreen; private TextVIEw tvFavor; @OverrIDe public voID onCreate() { super.onCreate(); // 初始化VIEws initVIEw(); // 显示touchBall createtouchBallVIEw(); // 监听事件 regListener(); } /** * 监听事件 */ private voID regListener() { touchBall.setontouchListener(this); touchBall.setonClickListener(this); tvApps.setonClickListener(this); tvHomeScreen.setonClickListener(this); tvSetting.setonClickListener(this); tvLockScreen.setonClickListener(this); tvFavor.setonClickListener(this); } /** * 初始化VIEws */ private voID initVIEw() { // 加载控制球VIEw touchBallVIEw = LayoutInflater.from(this).inflate(R.layout.touch_ball, null); touchBall = (button) touchBallVIEw.findVIEwByID(R.ID.touch_ball); // 初始化MenuVIEw menuVIEw = LayoutInflater.from(this).inflate(R.layout.shown_menu, null); tvApps = (TextVIEw) menuVIEw.findVIEwByID(R.ID.tv_apps); tvHomeScreen = (TextVIEw) menuVIEw.findVIEwByID(R.ID.tv_home_screen); tvSetting = (TextVIEw) menuVIEw.findVIEwByID(R.ID.tv_setting); tvLockScreen = (TextVIEw) menuVIEw.findVIEwByID(R.ID.tv_lock_screen); tvFavor = (TextVIEw) menuVIEw.findVIEwByID(R.ID.tv_favor); } /** * 显示touchBall */ private voID createtouchBallVIEw() { wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); params = new WindowManager.LayoutParams(); params.type = WindowManager.LayoutParams.TYPE_PHONE; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.gravity = Gravity.left | Gravity.top; params.x = 0; params.y = 0; params.wIDth = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.format = PixelFormat.RGBA_8888; wm.addVIEw(touchBallVIEw, params); } @OverrIDe public IBinder onBind(Intent intent) { return null; } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.touch_ball: // 显示Popup菜单 showMenuWindow(); break; case R.ID.tv_apps: // 显示Apps菜单 Toast.makeText(this, "APPS", Toast.LENGTH_SHORT).show(); popup.dismiss(); break; case R.ID.tv_favor: Toast.makeText(this, "FAVOR", Toast.LENGTH_SHORT).show(); popup.dismiss(); break; case R.ID.tv_home_screen: // 显示主菜单 Toast.makeText(this, "HOME", Toast.LENGTH_SHORT).show(); popup.dismiss(); break; case R.ID.tv_lock_screen: // 锁屏 Toast.makeText(this, "LOCK", Toast.LENGTH_SHORT).show(); popup.dismiss(); break; case R.ID.tv_setting: // 显示Setting界面 Toast.makeText(this, "SETTING", Toast.LENGTH_SHORT).show(); popup.dismiss(); break; default: popup.dismiss(); break; } } /** * 显示Popup菜单 */ private voID showMenuWindow() { displayMetrics dm = getResources().getdisplayMetrics(); popup = new PopupWindow(menuVIEw, (int) (dm.wIDthPixels * 0.7), (int) (dm.heightPixels * 0.5)); Drawable transpent = new colorDrawable(color.transparent); popup.setBackgroundDrawable(transpent); popup.setFocusable(true); popup.setoutsIDetouchable(true); popup.showAtLocation(touchBallVIEw, Gravity.CENTER, 0, 0); popup.update(); } /** * touch监听事件 */ @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { x = event.getRawX(); y = event.getRawY(); // 触摸事件 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isMoving = false; // 触摸坐标赋值 touchX = event.getX(); touchY = event.getY(); break; case MotionEvent.ACTION_MOVE: isMoving = true; // 触摸坐标赋值 params.x = (int) (x - touchX); params.y = (int) (y - touchY); wm.updateVIEwLayout(touchBallVIEw, params); break; case MotionEvent.ACTION_UP: // 触摸坐标赋值 touchX = touchY = 0; break; default: break; } if (isMoving) { return true; } else { return false; } }}
开启Service的主程序:
MainActivity:
package com.xys.xtouchassistant.activity;import com.xys.xtouchassistant.R;import com.xys.xtouchassistant.service.MainService;import androID.os.Bundle;import androID.app.Activity;import androID.content.Intent;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Intent service = new Intent(); service.setClass(this, MainService.class); startService(service); }}
这里只是简单的用一个Activity去开启这个服务,后面我们会重新实现开启服务的方法:
1、通过注册一个开机广播事件去开启我们的服务
2、通过进程守护来实现我们的Service不被kill(也可以通过在通知面板显式显示服务的方式来防止被kill)
3、实现菜单界面功能的可配置化
ps:需要源码的请留意哦
总结以上是内存溢出为你收集整理的仿iPhone assistivetouch小工具实现详解1全部内容,希望文章能够帮你解决仿iPhone assistivetouch小工具实现详解1所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)