最近产品出了个新需求,页面上出现浮层并且可点击,代码实现如下:
Activity中实现浮层图片:
@OverrIDe public voID onResume() { super.onResume(); createVIEw(); }@OverrIDepublic voID onPause() { super.onPause(); / 在程序退出(Activity销毁)时销毁悬浮窗口 if(floatVIEw!=null && windowManager !=null) { windowManager.removeVIEw(floatVIEw); floatVIEw=null; windowManager = null; windowManagerParams = null; }}private voID createVIEw() { if(floatVIEw!=null) return ; CMSAPI CMSAPI = RestAdapterUtils.getRestAPI(Config.NEW_CMS_URL,CMSAPI.class,this); CMSAPI.getfloatingAd(new Callback<Adfloating>() {//请求数据 @OverrIDe public voID success(Adfloating adfloating,Response response) { if (adfloating != null && "0".equals(adfloating.getErrorCode())) { long startTime = adfloating.getStarttime(); long endTime = adfloating.getEndtime(); long currentTime = System.currentTimeMillis();// LOGD(startTime + " +++++ "+endTime +" "+currentTime +" "+(currentTime > startTime && currentTime < endTime)); if (currentTime > startTime && currentTime < endTime) {//活动的有效期 floatVIEw = new floatVIEw(getApplicationContext()); floatVIEw.setonClickListener(MainActivity.this); int height = 240; int wIDth = 110; float ratio= 1.35f; if (!TextUtils.isEmpty(adfloating.getimg2())) { try { height = Integer.parseInt(adfloating.getimg2h()); wIDth = Integer.parseInt(adfloating.getimg2w()); ratio = (float) wIDth / height; } catch (Exception e) { ratio = 1.35f; } }// floatVIEw.setAspectRatio(ratio);//图片的大小 floatVIEw.setimageURI(Uri.parse(adfloating.getimg2()));//设置图片的网络地址// floatVIEw.setimageResource(R.drawable.face_icon); // 这里简单的用自带的icon来做演示 // 获取WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // 设置LayoutParams(全局变量)相关参数 windowManagerParams = ((MiGuApplication) getApplication()).getwindowParams(); windowManagerParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 设置window type windowManagerParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明 // 设置Window flag windowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_touch_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; /* * 注意,flag的值可以为: * LayoutParams.FLAG_NOT_touch_MODAL 不影响后面的事件 * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦 * LayoutParams.FLAG_NOT_touchABLE 不可触摸 */ // 调整悬浮窗口至左上角,便于调整坐标 windowManagerParams.gravity = Gravity.left | Gravity.top; // 以屏幕左上角为原点,设置x、y初始值 displayMetrics dm = new displayMetrics(); getwindowManager().getDefaultdisplay().getMetrics(dm); int screenWIDth = dm.wIDthPixels; int screenHeigh = dm.heightPixels; int x = screenWIDth - SystemTools.dip2px(MainActivity.this,100); int y= screenHeigh - SystemTools.dip2px(MainActivity.this,200); windowManagerParams.x = x; windowManagerParams.y = y; // 设置悬浮窗口长宽数据 windowManagerParams.wIDth = wIDth;//设置窗口的宽度为图片大小 windowManagerParams.height =height;//设置窗口的高度为图片大小// windowManagerParams.wIDth = WindowManager.LayoutParams.WRAP_CONTENT;// windowManagerParams.height =WindowManager.LayoutParams.WRAP_CONTENT; // 显示myfloatVIEw图像 windowManager.addVIEw(floatVIEw,windowManagerParams); return; } } } @OverrIDe public voID failure(RetrofitError error) {//网络请求数据失败 LOGE(error.getMessage()); } }); } public voID onClick(VIEw v) {//图片的点击事件 Intent intent = new Intent(MainActivity.this,ActivitIEsDetails.class); startActivity(intent); }
图片控件:
public class floatVIEw extends SimpleDraweeVIEw { private float mtouchX; private float mtouchY; private float x; private float y; private float mStartX; private float mStartY; private OnClickListener mClickListener; private WindowManager windowManager = (WindowManager) getContext() .getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // 此windowManagerParams变量为获取的全局变量,用以保存悬浮窗口的属性 private WindowManager.LayoutParams windowManagerParams = ((MiGuApplication) getContext() .getApplicationContext()).getwindowParams(); public floatVIEw(Context context) { super(context); } public floatVIEw(Context context,AttributeSet attrs) { super(context,attrs); } private long curtime=0; @OverrIDe public boolean ontouchEvent(MotionEvent event) {//获取到状态栏的高度 Rect frame = new Rect(); getwindowVisibledisplayFrame(frame); int statusbarHeight = frame.top; System.out.println("statusbarHeight:"+statusbarHeight);// 获取相对屏幕的坐标,即以屏幕左上角为原点 x = event.getRawX(); y = event.getRawY() - statusbarHeight; // statusbarHeight是系统状态栏的高度 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作// 获取相对VIEw的坐标,即以此VIEw左上角为原点 mtouchX = event.getX(); mtouchY = event.getY(); mStartX = x; mStartY = y; break; case MotionEvent.ACTION_MOVE: // 捕获手指触摸移动动作 updateVIEwposition(); curtime=System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: // 捕获手指触摸离开动作// if(System.currentTimeMillis()-curtime>100){// break;// } updateVIEwposition(); mtouchX = mtouchY = 0; if (Math.abs(x - mStartX) < 5 && Math.abs(y - mStartY) < 5) {//轻微拖动算点击 if(mClickListener!=null) { mClickListener.onClick(this); } } break; } return true; } @OverrIDe public voID setonClickListener(OnClickListener l) { this.mClickListener = l; } private voID updateVIEwposition() {// 更新浮动窗口位置参数 windowManagerParams.x = (int) (x - mtouchX); windowManagerParams.y = (int) (y - mtouchY); windowManager.updateVIEwLayout(this,windowManagerParams); // 刷新显示 }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android浮层图片拖动并且可点击效果全部内容,希望文章能够帮你解决android浮层图片拖动并且可点击效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)