/** * Calculates control width and creates text layouts * * @param widthSize the input layout width * @param mode the layout mode * @return the calculated control width */ private int calculateLayoutWidth(int widthSize, int mode) { initResourcesIfNecessary(); // TODO: make it static itemsLayout.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); itemsLayout.measure( MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int width = itemsLayout.getMeasuredWidth(); if (mode == MeasureSpec.EXACTLY) { width = widthSize; } else { width += 2 * PADDING; // Check against our minimum width width = Math.max(width, getSuggestedMinimumWidth()); if (mode == MeasureSpec.AT_MOST && widthSize < width) { width = widthSize; } } itemsLayout.measure( MeasureSpec.makeMeasureSpec(width - 2 * PADDING, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); return width; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (!shouldExpand || MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) { return; } int myWidth = getMeasuredWidth(); tabsContainer.measure(MeasureSpec.EXACTLY | myWidth, heightMeasureSpec); /*int childWidth = 0; for (int i = 0; i < tabCount; i++) { childWidth += tabsContainer.getChildAt(i).getMeasuredWidth(); } if (!checkedTabWidths && childWidth > 0 && myWidth > 0) { if (childWidth <= myWidth) { for (int i = 0; i < tabCount; i++) { tabsContainer.getChildAt(i).setLayoutParams(expandedTabLayoutParams); } } checkedTabWidths = true; }*/ }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (!shouldExpand || MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) { return; } int myWidth = getMeasuredWidth(); tabsContainer.measure(MeasureSpec.EXACTLY | myWidth, heightMeasureSpec); }
private void initData() { adapter = new ViewPagerAdapter(pageViews); pager.setAdapter(adapter); pager.setCurrentItem(0); pageIndicator.setViewPager(pager); // 加上小圆圈 int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); layoutImgs.measure(w, h); height = layoutImgs.getMeasuredHeight(); ViewGroup.LayoutParams layoutParamsChild = layoutImgs.getLayoutParams(); layoutParamsChild.height = 0; layoutImgs.setLayoutParams(layoutParamsChild); }
@Click(R.id.actionbar_more) void moreClicked() { mActionbarMore.setSelected(true); LinearLayout moreContentView = (LinearLayout) mInflater.inflate(R.layout.main_more_content, null); moreContentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); final PopupWindow popupWindow = new PopupWindow(this); popupWindow.setBackgroundDrawable( new ColorDrawable(getResources().getColor(R.color.red_dark_washedout))); popupWindow.setContentView(moreContentView); popupWindow.showAsDropDown(mActionbarMore); popupWindow.setTouchable(true); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setOnDismissListener( new OnDismissListener() { public void onDismiss() { mActionbarMore.setSelected(false); } }); Button settingsButton = (Button) moreContentView.findViewById(R.id.main_more_content_settings); settingsButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SettingsActivity.class)); popupWindow.dismiss(); } }); Button aboutButton = (Button) moreContentView.findViewById(R.id.main_more_content_about); aboutButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, AboutActivity_.class)); popupWindow.dismiss(); } }); popupWindow.update(moreContentView.getMeasuredWidth(), moreContentView.getMeasuredHeight()); }
/** * Returns how big this panel's view should be. This method should only be called when the view * has not been attached to a parent. * * @throws IllegalStateException */ public Size measure() { Preconditions.checkState(mContentView.getParent() == null); mContentView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); return new Size(mContentView.getMeasuredWidth(), mContentView.getMeasuredHeight()); }
public void createFloatView() { Log.i(TAG, "Float View created"); if (wmParams != null) return; wmParams = new WindowManager.LayoutParams(); mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); wmParams.type = LayoutParams.TYPE_PHONE; wmParams.format = PixelFormat.RGBA_8888; wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE; wmParams.gravity = Gravity.LEFT | Gravity.TOP; wmParams.x = wmParams.y = 0; wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; LayoutInflater inflater = LayoutInflater.from(mContext); mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null); mFloatbarImage = (ImageView) mFloatLayout.findViewById(R.id.float_bar); mFloatRecordButton = (Button) mFloatLayout.findViewById(R.id.float_record_button); mFloatPlaybackButton = (Button) mFloatLayout.findViewById(R.id.float_playback_button); mFloatPickButton = (Button) mFloatLayout.findViewById(R.id.float_pick_button); mFloatDropButton = (Button) mFloatLayout.findViewById(R.id.float_drop_button); mFloatLayout.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); mFloatLayout.setOnTouchListener( new OnTouchListener() { int lastX, lastY; int paramX, paramY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); paramX = wmParams.x; paramY = wmParams.y; break; case MotionEvent.ACTION_MOVE: int dx = (int) event.getRawX() - lastX; int dy = (int) event.getRawY() - lastY; wmParams.x = paramX + dx; wmParams.y = paramY + dy; mWindowManager.updateViewLayout(mFloatLayout, wmParams); break; } return false; } }); mFloatRecordButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { switch (state) { case -1: // not start new EventRecordWatcher(new EventRecorder(5, scriptName)).start(); state = 5; new Thread() { public void run() { int num = 5; mHandler.post( new Runnable() { @Override public void run() { mFloatRecordButton.setText(Integer.toString(5)); mFloatRecordButton.setBackgroundColor(Color.BLACK); } }); while (num > 0) { try { sleep(1000); final int a = --num; mHandler.post( new Runnable() { @Override public void run() { mFloatRecordButton.setText(Integer.toString(a)); } }); } catch (InterruptedException e) { e.printStackTrace(); } } try { sleep(1000); state = -1; // tobe confirmed mHandler.post( new Runnable() { @Override public void run() { mFloatRecordButton.setText(""); mFloatRecordButton.setBackgroundResource( android.R.drawable.ic_menu_add); mFloatPickButton.setVisibility(View.VISIBLE); mFloatPlaybackButton.setVisibility(View.VISIBLE); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); break; default: break; } } }); mFloatPlaybackButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { new EventSenderForPlayback(scriptName).start(); } }); mFloatPickButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { if (type == 1) { jumpBack("录制成功"); return; } ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; final String name = cn.getClassName(); mHandler.post( new Runnable() { @Override public void run() { DolphinContext context = new DolphinContext(null, name, "still", 0l, 0l); long id = DaoManager.getDaoManager(mContext).addDolphinContext(context, pluginId); if (id == 0) { jumpBack("重复的插件环境"); } else { jumpBack("插件环境选择成功"); } } }); } }); mFloatDropButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { clean(); jumpBack("放弃选取"); } }); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException( getClass().getSimpleName() + " can only be used " + "with android:layout_width=\"match_parent\" (or fill_parent)"); } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode == MeasureSpec.UNSPECIFIED) { throw new IllegalStateException( getClass().getSimpleName() + " can only be used " + "with android:layout_height=\"wrap_content\""); } final int contentWidth = MeasureSpec.getSize(widthMeasureSpec); int maxHeight = mContentHeight > 0 ? mContentHeight : MeasureSpec.getSize(heightMeasureSpec); final int verticalPadding = getPaddingTop() + getPaddingBottom(); int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); final int height = maxHeight - verticalPadding; final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); if (mClose != null) { availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); availableWidth -= lp.leftMargin + lp.rightMargin; } if (mMenuView != null && mMenuView.getParent() == this) { availableWidth = measureChildView(mMenuView, availableWidth, childSpecHeight, 0); } if (mTitleLayout != null && mCustomView == null) { if (mTitleOptional) { final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); mTitleLayout.measure(titleWidthSpec, childSpecHeight); final int titleWidth = mTitleLayout.getMeasuredWidth(); final boolean titleFits = titleWidth <= availableWidth; if (titleFits) { availableWidth -= titleWidth; } mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE); } else { availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); } } if (mCustomView != null) { ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; final int customWidth = lp.width >= 0 ? Math.min(lp.width, availableWidth) : availableWidth; final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; final int customHeight = lp.height >= 0 ? Math.min(lp.height, height) : height; mCustomView.measure( MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), MeasureSpec.makeMeasureSpec(customHeight, customHeightMode)); } if (mContentHeight <= 0) { int measuredHeight = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View v = getChildAt(i); int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; if (paddedViewHeight > measuredHeight) { measuredHeight = paddedViewHeight; } } setMeasuredDimension(contentWidth, measuredHeight); } else { setMeasuredDimension(contentWidth, maxHeight); } }
public void createControls() { // 1. Create view groups and controls statusBar.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.box_top)); rightStack = new StackWidgetView(view.getContext()); leftStack = new StackWidgetView(view.getContext()); // 2. Preparations Rect topRectPadding = new Rect(); view.getResources().getDrawable(R.drawable.box_top).getPadding(topRectPadding); // for measurement statusBar.addView(backToLocation); STATUS_BAR_MARGIN_X = (int) (STATUS_BAR_MARGIN_X * scaleCoefficient); statusBar.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); Rect statusBarPadding = new Rect(); statusBar.getBackground().getPadding(statusBarPadding); // 3. put into frame parent layout controls FrameLayout parent = (FrameLayout) view.getParent(); // status bar hides own top part int topMargin = statusBar.getMeasuredHeight() - statusBarPadding.top - statusBarPadding.bottom; // we want that status bar lays over map stack controls topMargin -= topRectPadding.top; FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT); flp.rightMargin = STATUS_BAR_MARGIN_X; flp.topMargin = topMargin; rightStack.setLayoutParams(flp); flp = new FrameLayout.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.TOP); flp.topMargin = (int) (topMargin + scaleCoefficient * 8); lanesControl.setLayoutParams(flp); flp = new FrameLayout.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.LEFT); flp.leftMargin = STATUS_BAR_MARGIN_X; flp.topMargin = topMargin; leftStack.setLayoutParams(flp); flp = new FrameLayout.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP); flp.leftMargin = STATUS_BAR_MARGIN_X; flp.rightMargin = STATUS_BAR_MARGIN_X; flp.topMargin = -topRectPadding.top; statusBar.setLayoutParams(flp); flp = new FrameLayout.LayoutParams( (int) (78 * scaleCoefficient), (int) (78 * scaleCoefficient), Gravity.RIGHT | Gravity.BOTTOM); flp.rightMargin = (int) (10 * scaleCoefficient); flp.bottomMargin = (int) (85 * scaleCoefficient); alarmControl.setLayoutParams(flp); parent.addView(rightStack); parent.addView(leftStack); parent.addView(statusBar); parent.addView(lanesControl); parent.addView(alarmControl); alarmControl.setVisibility(View.GONE); lanesControl.setVisibility(View.GONE); // update and create controls recreateControls(); }
private int getMeasureHeight() { ll_footer.measure(0, 0); return ll_footer.getMeasuredHeight(); }