@Override public void update() { super.update(); LayoutParams lp = (LayoutParams) getLayoutParams(); lp.leftMargin = bounds.displayLeft; lp.topMargin = bounds.displayTop; setLayoutParams(lp); Log.d("Chevron", "new size:" + this.getWidth() + "," + this.getHeight()); updatePath(); }
private void initUI() { mWM = (WindowManager) getSystemService(Context.WINDOW_SERVICE); mScreenWidth = mWM.getDefaultDisplay().getWidth(); mScreenHeight = mWM.getDefaultDisplay().getHeight(); // 1找控件 final ImageView iv_drag = (ImageView) findViewById(R.id.iv_drag); final Button bt_notify_top = (Button) findViewById(R.id.bt_notify_top); final Button bt_notify_bottom = (Button) findViewById(R.id.bt_notify_bottom); // 2获取归属地提示框保存的位置 int locationX = SpUtil.getInt(this, ConstantValues.TOAST_ADDRESS_LOCATIONX, 0); int locationY = SpUtil.getInt(this, ConstantValues.TOAST_ADDRESS_LOCATIONY, 0); mLayoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); mLayoutParams.leftMargin = locationX; mLayoutParams.topMargin = locationY; iv_drag.setLayoutParams(mLayoutParams); if (locationY < mScreenHeight / 2) { bt_notify_top.setVisibility(View.INVISIBLE); bt_notify_bottom.setVisibility(View.VISIBLE); } else { bt_notify_top.setVisibility(View.VISIBLE); bt_notify_bottom.setVisibility(View.INVISIBLE); } // 4设置触摸监听事件 iv_drag.setOnTouchListener( new OnTouchListener() { private int startX; private 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(); break; case MotionEvent.ACTION_MOVE: // 获取移动过程中的坐标 int moveX = (int) event.getRawX(); int moveY = (int) event.getRawY(); // 移动的距离 int disX = moveX - startX; int disY = moveY - startY; int left = iv_drag.getLeft() + disX; int top = iv_drag.getTop() + disY; int right = iv_drag.getRight() + disX; int bottom = iv_drag.getBottom() + disY; if (top < mScreenHeight / 2) { bt_notify_top.setVisibility(View.INVISIBLE); bt_notify_bottom.setVisibility(View.VISIBLE); } else { bt_notify_top.setVisibility(View.VISIBLE); bt_notify_bottom.setVisibility(View.INVISIBLE); } // 1容错处理 if (left < 0 || top < 0 || right > mScreenWidth || bottom > mScreenHeight - 22) { return true; } // 2将移动后的坐标展示 System.out.println(left + "----" + top + "----" + right + "----" + bottom); iv_drag.layout(left, top, right, bottom); // 3重置起始坐标 startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: // 4保存吐司位置坐标 SpUtil.putInt( getApplicationContext(), ConstantValues.TOAST_ADDRESS_LOCATIONX, iv_drag.getLeft()); SpUtil.putInt( getApplicationContext(), ConstantValues.TOAST_ADDRESS_LOCATIONY, iv_drag.getTop()); break; } return false; } }); // 5设置双击监听事件 iv_drag.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); mHits[mHits.length - 1] = SystemClock.uptimeMillis(); if (mHits[mHits.length - 1] - mHits[0] < 500) { // 满足双击事件后,调用代码 int left = mScreenWidth / 2 - iv_drag.getWidth() / 2; int top = mScreenHeight / 2 - iv_drag.getHeight() / 2; int right = mScreenWidth / 2 + iv_drag.getWidth() / 2; int bottom = mScreenHeight / 2 + iv_drag.getHeight() / 2; // 控件按以上规则显示 iv_drag.layout(left, top, right, bottom); // 存储最终位置 SpUtil.putInt( getApplicationContext(), ConstantValues.TOAST_ADDRESS_LOCATIONX, iv_drag.getLeft()); SpUtil.putInt( getApplicationContext(), ConstantValues.TOAST_ADDRESS_LOCATIONY, iv_drag.getTop()); } } }); }
private synchronized boolean initUI() { boolean z; try { if (getIntent() != null) { this.mbHideHeader = getIntent().getBooleanExtra("HIDE_HEADER", false); this.mbSendAllChecksumResponseParametersToPGServer = getIntent() .getBooleanExtra("SEND_ALL_CHECKSUM_RESPONSE_PARAMETERS_TO_PG_SERVER", false); } PaytmUtility.debugLog("Hide Header " + this.mbHideHeader); PaytmUtility.debugLog("Initializing the UI of Transaction Page..."); RelativeLayout RootView = new RelativeLayout(this); RelativeLayout TopBar = new RelativeLayout(this); TopBar.setLayoutParams(new LayoutParams(-1, -2)); TopBar.setId(1); TopBar.setBackgroundColor(-3355444); Button Cancel = new Button(this, null, 16842825); LayoutParams CancelParams = new LayoutParams(-2, -2); CancelParams.addRule(15); CancelParams.leftMargin = (int) (getResources().getDisplayMetrics().density * 5.0f); Cancel.setOnClickListener( new OnClickListener() { public void onClick(View v) { PaytmUtility.debugLog("User pressed back button which is present in Header Bar."); PaytmPGActivity.this.cancelTransaction(); } }); Cancel.setLayoutParams(CancelParams); Cancel.setText("Cancel"); TextView AppName = new TextView(this); LayoutParams AppNameParams = new LayoutParams(-2, -2); AppNameParams.addRule(13); AppName.setLayoutParams(AppNameParams); AppName.setTextColor(-16777216); AppName.setText("Paytm Payments"); TopBar.addView(Cancel); TopBar.addView(AppName); RelativeLayout WebLayout = new RelativeLayout(this); LayoutParams WebLayoutParams = new LayoutParams(-1, -1); WebLayoutParams.addRule(3, TopBar.getId()); WebLayout.setLayoutParams(WebLayoutParams); this.mWV = new PaytmWebView(this, this.mParams); this.mWV.setVisibility(8); this.mWV.setLayoutParams(new LayoutParams(-1, -1)); mProgress = new ProgressBar(this, null, 16842873); LayoutParams ProgressParams = new LayoutParams(-2, -2); ProgressParams.addRule(13); mProgress.setLayoutParams(ProgressParams); WebLayout.addView(this.mWV); WebLayout.addView(mProgress); RootView.addView(TopBar); RootView.addView(WebLayout); if (this.mbHideHeader) { TopBar.setVisibility(8); } requestWindowFeature(1); setContentView(RootView); PaytmUtility.debugLog("Initialized UI of Transaction Page."); z = true; } catch (Exception inEx) { PaytmUtility.debugLog("Some exception occurred while initializing UI."); PaytmUtility.printStackTrace(inEx); z = false; } return z; }