博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对前面的自定义的toast制作拖拽效果,以及双击居中效果
阅读量:6272 次
发布时间:2019-06-22

本文共 9509 字,大约阅读时间需要 31 分钟。

hot3.png

注意:

/*toast窗体类型默认不响应窗体点击事件

params.type = WindowManager.LayoutParams.TYPE_TOAST;*/

/*制定具有电话优先级的窗体类型,需要添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>*/

params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;

代码如下:

package com.zaizai.safty.service; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.PixelFormat; import android.os.IBinder; import android.os.SystemClock; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import com.zaizai.safty.R; import com.zaizai.safty.db.dao.NumberAddressQueryUtils; public class AddressService extends Service {
private static final String TAG = "zaizai"; //窗体管理者 private WindowManager windowManager; private View view; /** * 电话 */ private TelephonyManager telephonyManager; private MyListenerPhone myListenerPhone; private OutCallReceiver outCallReceiver; private WindowManager.LayoutParams params; private SharedPreferences sharedPreferences; @Override public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub return null; } // 服务里面的内部类 //广播接收者的生命周期和服务一样 class OutCallReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
// 这就是我们拿到的播出去的电话号码 String phone = getResultData(); // 查询数据库 String address = NumberAddressQueryUtils.queryNumber(phone); // Toast.makeText(context, address, Toast.LENGTH_LONG).show(); myToast(address); } } private class MyListenerPhone extends PhoneStateListener {
@Override public void onCallStateChanged(int state, String incomingNumber) {
// state:状态,incomingNumber:来电号码 super.onCallStateChanged(state, incomingNumber); switch (state) {
case TelephonyManager.CALL_STATE_RINGING:// 来电铃声响起 // 查询数据库的操作 String address = NumberAddressQueryUtils .queryNumber(incomingNumber); //Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG).show(); myToast(address); break; case TelephonyManager.CALL_STATE_IDLE://电话的空闲状态:挂电话、来电拒绝 //把这个View移除 if (view != null) {
windowManager.removeView(view); } break; default: break; } } } @Override public void onCreate() {
// TODO Auto-generated method stub super.onCreate(); //电话服务 telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 监听来电 myListenerPhone = new MyListenerPhone(); //注册服务去监听电话 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_CALL_STATE); //用代码去注册广播接收者Begin outCallReceiver = new OutCallReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.NEW_OUTGOING_CALL"); registerReceiver(outCallReceiver, filter); //用代码去注册广播接收者End //实例化窗体 windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); } /** * 自定义土司 * * @param address */ private long[] mHits = new long[2]; public void myToast(String address) {
/*绘制UI界面*/ view = View.inflate(this, R.layout.address_show, null); TextView textview = (TextView) view.findViewById(R.id.tv_address); view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
/*双击两次居中*/ System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); mHits[mHits.length - 1] = SystemClock.uptimeMillis(); if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
/*成功双击两次*/ Log.i(TAG, "双击成功:"); params.x = windowManager.getDefaultDisplay().getWidth() / 2 - view.getWidth() / 2; params.y = windowManager.getDefaultDisplay().getHeight() / 2 - view.getHeight() / 2; windowManager.updateViewLayout(view, params); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("lastx", params.x); editor.putInt("lasty", params.y); editor.commit(); } } }); view.setOnTouchListener(new View.OnTouchListener() {
int startX; int startY; @Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://手指按下屏幕 {
startX = (int) event.getRawX(); startY = (int) event.getRawY(); Log.i(TAG, "开始位置:" + startX + "," + startY); break; } case MotionEvent.ACTION_MOVE://手指在屏幕上移动 {
int newX = (int) event.getRawX(); int newY = (int) event.getRawY(); Log.i(TAG, "移动后位置:" + newX + "," + newY); int dx = newX - startX; int dy = newY - startY; Log.i(TAG, "移动后的偏移量:" + dx + "," + dy); Log.i(TAG, "更新ImageView在窗体的偏移量:" + dx + "," + dy); /*重新设置窗体的开始位置,结束位置*/ params.x += dx; params.y += dy; if (params.x < 0) {
params.x = 0; } else if (params.y < 0) {
params.y = 0; } else if (params.x > (windowManager.getDefaultDisplay().getWidth() - view.getWidth())) {
params.x = windowManager.getDefaultDisplay().getWidth() - view.getWidth(); } else if (params.y > (windowManager.getDefaultDisplay().getHeight() - view.getHeight())) {
params.y = windowManager.getDefaultDisplay().getHeight() - view.getHeight(); } windowManager.updateViewLayout(view, params); // 重新初始化手指的开始结束位置。 startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; } case MotionEvent.ACTION_UP://手指离开屏幕 {
//记录控件距离屏幕左上角的坐标 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("lastx", params.x); editor.putInt("lasty", params.y); editor.commit(); break; } } return false;//事件处理完毕,返回ture,将事件拦截掉,不让负空间布局响应触摸事件 } }); //"半透明","活力橙","卫士蓝","金属灰","苹果绿" int[] ids = {R.mipmap.call_locate_white9, R.mipmap.call_locate_orange9, R.mipmap.call_locate_blue9 , R.mipmap.call_locate_gray9, R.mipmap.call_locate_green9}; /*获得用户设置的风格,默认为0半透明*/ sharedPreferences = getSharedPreferences("config", MODE_PRIVATE); view.setBackgroundResource(ids[sharedPreferences.getInt("which", 0)]); textview.setText(address); //窗体的参数就设置好了 params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; /*吐司位置设置 params.gravity = Gravity.TOP; 多个参数使用+号连接*/ params.gravity = Gravity.TOP + Gravity.LEFT; /*左边100 顶部100 */ params.x = sharedPreferences.getInt("lastx", 0); params.y = sharedPreferences.getInt("lasty", 0); params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE /* | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE * 去掉响应时间*/ | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; params.format = PixelFormat.TRANSLUCENT; /*toast窗体类型默认不响应窗体点击事件 params.type = WindowManager.LayoutParams.TYPE_TOAST;*/ /*制定具有电话优先级的窗体类型,需要添加权限
*/ params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE; windowManager.addView(view, params); } @Override public void onDestroy() {
// TODO Auto-generated method stub super.onDestroy(); // 取消监听来电 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_NONE); myListenerPhone = null; //用代码取消注册广播接收者 unregisterReceiver(outCallReceiver); outCallReceiver = null; } }

转载于:https://my.oschina.net/zaizaiangels/blog/527632

你可能感兴趣的文章
Vue.js 中v-for和v-if一起使用,来判断select中的option为选中项
查看>>
Java中AES加密解密以及签名校验
查看>>
定义内部类 继承 AsyncTask 来实现异步网络请求
查看>>
VC中怎么读取.txt文件
查看>>
如何清理mac系统垃圾
查看>>
企业中最佳虚拟机软件应用程序—Parallels Deskto
查看>>
送给“正在纠结”、“准备纠结”的前端开发们
查看>>
Nginx配置文件详细说明
查看>>
怎么用Navicat Premium图标编辑器创建表
查看>>
谈DELL收购EMC
查看>>
Spring配置文件(2)配置方式
查看>>
MariaDB/Mysql 批量插入 批量更新
查看>>
ItelliJ IDEA开发工具使用—创建一个web项目
查看>>
學習 React.js:用 Node 和 React.js 創建一個實時的 Twitter 流
查看>>
solr-4.10.4部署到tomcat6
查看>>
切片键(Shard Keys)
查看>>
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
Git 常用命令
查看>>
驰骋工作流引擎三种项目集成开发模式
查看>>