您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關android如何實現浮層圖片拖動并且可點擊效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
代碼實現如下:
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<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如何實現浮層圖片拖動并且可點擊效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。