最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
android浮层图片拖动并且可点击效果
时间:2022-06-25 23:08:16 编辑:袖梨 来源:一聚教程网
最近产品出了个新需求,页面上出现浮层并且可点击,代码实现如下:
Activity中实现浮层图片:
@Override public void onResume() { super.onResume(); createView(); } @Override public 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() {//请求数据 @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 ; int ; 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); // 刷新显示 } }
相关文章
- 《燕云十六声》红尘无眼完成图文攻略 12-25
- 《燕云十六声》阴阳如影完成图文攻略 12-25
- 《燕云十六声》悬檐之下四架椽屋图文攻略 12-25
- 《燕云十六声》2024最新公测时间介绍 12-25
- 《燕云十六声》有没有藏宝阁 12-25
- 《燕云十六声》制作公司介绍 12-25