android浮層圖片拖動并且可點擊效果
最近產(chǎn)品出了個新需求,頁面上出現(xiàn)浮層并且可點擊,代碼實現(xiàn)如下:
Activity中實現(xiàn)浮層圖片:
@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<AdFloating>() {//請求數(shù)據(jù) @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()));//設(shè)置圖片的網(wǎng)絡(luò)地址 // floatView.setImageResource(R.drawable.face_icon); // 這里簡單的用自帶的icon來做演示 // 獲取WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // 設(shè)置LayoutParams(全局變量)相關(guān)參數(shù) windowManagerParams = ((MiGuApplication) getApplication()).getWindowParams(); windowManagerParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 設(shè)置window type windowManagerParams.format = PixelFormat.RGBA_8888; // 設(shè)置圖片格式,效果為背景透明 // 設(shè)置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 不可觸摸 */ // 調(diào)整懸浮窗口至左上角,便于調(diào)整坐標 windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP; // 以屏幕左上角為原點,設(shè)置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; // 設(shè)置懸浮窗口長寬數(shù)據(jù) windowManagerParams.width = width;//設(shè)置窗口的寬度為圖片大小 windowManagerParams.height =height;//設(shè)置窗口的高度為圖片大小 // windowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT; // windowManagerParams.height =WindowManager.LayoutParams.WRAP_CONTENT; // 顯示myFloatView圖像 windowManager.addView(floatView, windowManagerParams); return; } } } @Override public void failure(RetrofitError error) {//網(wǎng)絡(luò)請求數(shù)據(jù)失敗 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) { //獲取到狀態(tài)欄的高度 Rect frame = new Rect(); getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; System.out.println("statusBarHeight:"+statusBarHeight); // 獲取相對屏幕的坐標,即以屏幕左上角為原點 x = event.getRawX(); y = event.getRawY() - statusBarHeight; // statusBarHeight是系統(tǒng)狀態(tài)欄的高度 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() { // 更新浮動窗口位置參數(shù) windowManagerParams.x = (int) (x - mTouchX); windowManagerParams.y = (int) (y - mTouchY); windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示 } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 實現(xiàn)永久保存數(shù)據(jù)的方法詳解
本篇文章是對Android實現(xiàn)永久保存數(shù)據(jù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06Android開發(fā)實現(xiàn)的文本折疊點擊展開功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的文本折疊點擊展開功能,涉及Android界面布局與屬性控制相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android 線程之自定義帶消息循環(huán)Looper的實例
這篇文章主要介紹了Android 線程之自定義帶消息循環(huán)Looper的實例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Material Design系列之自定義Behavior支持所有View
這篇文章主要為大家詳細介紹了Material Design系列之自定義Behavior支持所有View,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android Studio三方引用報錯但是項目可以運行的解決方案
今天小編就為大家分享一篇關(guān)于Android Studio三方引用報錯但是項目可以運行的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03android中RecycleView添加下滑到底部的監(jiān)聽示例
本篇文章主要介紹了android中RecycleView添加下滑到底部的監(jiān)聽示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03Android 用RxBinding與RxJava2實現(xiàn)短信驗證碼倒計時功能
這篇文章主要介紹了Android 用RxBinding與RxJava2實現(xiàn)短信倒計時功能示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10