Refine search
/** * Sets the layout resource for a custom MarkerView. * * @param layoutResource */ private void setupLayoutResource(int layoutResource) { View inflated = LayoutInflater.from(getContext()).inflate(layoutResource, this); inflated.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); inflated.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); // measure(getWidth(), getHeight()); inflated.layout(0, 0, inflated.getMeasuredWidth(), inflated.getMeasuredHeight()); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { mInLayout = true; if (mContentView != null) mContentView.layout(mContentLeft, mContentTop, mContentLeft + mContentView.getMeasuredWidth(), mContentTop + mContentView.getMeasuredHeight()); mInLayout = false; }
public static int getWidth(View view) { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h); return (view.getMeasuredWidth()); }
Display display = getWindowManager().getDefaultDisplay(); View view = findViewById(R.id.YOUR_VIEW_ID); view.measure(display.getWidth(), display.getHeight()); view.getMeasuredWidth(); // view width view.getMeasuredHeight(); //view height
protected Rect getRelativePosition(View child) { View t = child; Rect r = new Rect(t.getLeft(), t.getTop(), 0, 0); while (t.getParent() != null && t != getRootView()) { t = (View) t.getParent(); if (t == this) break; r.left += t.getLeft(); r.top += t.getTop(); } r.right = r.left + child.getMeasuredWidth(); r.bottom = r.top + child.getMeasuredHeight(); return r; }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = 0; int childTop = 0; int childRight = right - left; int childBottom = bottom - top; if(mIsRtl) for(int i = 0, count = getChildCount(); i < count; i++){ View child = getChildAt(i); child.layout(childRight - child.getMeasuredWidth(), childTop, childRight, childBottom); childRight -= child.getMeasuredWidth(); } else for(int i = 0, count = getChildCount(); i < count; i++){ View child = getChildAt(i); child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childBottom); childLeft += child.getMeasuredWidth(); } } }
/** * Measure and layout a component view. * * @param view The component view to measure and layout */ public static void measureAndLayout(View view) { view.measure(makeMeasureSpec(1000, EXACTLY), makeMeasureSpec(0, UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int width = getMeasuredWidth(); int connectorRight = rowMargins + connector.getMeasuredWidth(); connector.layout(rowMargins, 0, connectorRight, connector.getMeasuredHeight()); moreButton.layout(width - rowMargins - moreSize, moreMarginTop, width - rowMargins, moreMarginTop + moreSize); int titleLeft = connectorRight + rowMargins; int titleBottom = titleMarginTop + title.getMeasuredHeight(); title.layout(titleLeft, titleMarginTop, titleLeft + title.getMeasuredWidth(), titleBottom); if (details.getVisibility() != GONE) { details.layout(titleLeft, titleBottom, width - rowMargins, titleBottom + details.getMeasuredHeight()); } } }
int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset; content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode)); heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight(); widthSpecSize = content.getMeasuredWidth(); if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth(); int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset; getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec); widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth(); heightSpecSize = content.getMeasuredHeight(); if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final View thisView = this; final int width = thisView.getMeasuredWidth(); final int height = thisView.getMeasuredHeight(); mScaleX = 1f * width / WIDTH; mScaleY = 1f * (mViewportHeight > 0 ? mViewportHeight : height) / HEIGHT; updateMountainPath(mMoveFactor, height); updateTreePath(mMoveFactor, true); }
public static int getWidth(View view) { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h); return (view.getMeasuredWidth()); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { final int count = getChildCount(); final int width = right - left; final int height = bottom - top; //只支持top、bottom的padding final int padding_top = getPaddingTop(); final int padding_bottom = getPaddingBottom(); int used = 0; for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) { child.layout(width - used - child.getMeasuredWidth(), padding_top, width - used, height - padding_bottom); } else { child.layout(used, padding_top, child.getMeasuredWidth() + used, height - padding_bottom); } used += child.getMeasuredWidth(); } }
public static Bitmap getBitmapFromView(View view) { view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.draw(canvas); return bitmap; }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final View thisView = this; final View imageView = mImageView; final View dropView = mWaterDropView; final int measuredWidth = thisView.getMeasuredWidth(); final int widthWaterDrop = dropView.getMeasuredWidth(); final int heightWaterDrop = dropView.getMeasuredHeight(); final int leftWaterDrop = measuredWidth / 2 - widthWaterDrop / 2; final int topWaterDrop = 0; dropView.layout(leftWaterDrop, topWaterDrop, leftWaterDrop + widthWaterDrop, topWaterDrop + heightWaterDrop); final int widthImage = imageView.getMeasuredWidth(); final int heightImage = imageView.getMeasuredHeight(); final int leftImage = measuredWidth / 2 - widthImage / 2; int topImage = widthWaterDrop / 2 - widthImage / 2; if (topImage + heightImage > dropView.getBottom() - (widthWaterDrop - widthImage) / 2) { topImage = dropView.getBottom() - (widthWaterDrop - widthImage) / 2 - heightImage; } imageView.layout(leftImage, topImage, leftImage + widthImage, topImage + heightImage); } //</editor-fold>
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final View imageView = mImageView; final View dropView = mWaterDropView; LayoutParams lpImage = (LayoutParams) imageView.getLayoutParams(); imageView.measure( makeMeasureSpec(lpImage.width, EXACTLY), makeMeasureSpec(lpImage.height, EXACTLY) ); dropView.measure( makeMeasureSpec(getSize(widthMeasureSpec), AT_MOST), heightMeasureSpec ); int maxWidth = Math.max(imageView.getMeasuredWidth(), dropView.getMeasuredWidth()); int maxHeight = Math.max(imageView.getMeasuredHeight(), dropView.getMeasuredHeight()); super.setMeasuredDimension(View.resolveSize(maxWidth, widthMeasureSpec), View.resolveSize(maxHeight, heightMeasureSpec)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final View thisView = this; super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (Build.VERSION.SDK_INT < 21) { super.setMeasuredDimension( thisView.getMeasuredWidth() + mShadowRadius * 2, thisView.getMeasuredHeight() + mShadowRadius * 2); } }
/** * 获取控件的宽 * @param view * @return */ public static int getWidgetWidth(View view){ int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h);//先度量 int width = view.getMeasuredWidth(); return width; } /**
private Bitmap convertViewToBitmap(View view) { view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { mInLayout = true; if (mContentView != null) { mContentView.layout(mContentLeft, mContentTop, mContentLeft + mContentView.getMeasuredWidth(), mContentTop + mContentView.getMeasuredHeight()); } mInLayout = false; }
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height); view.measure(childWidthSpec, childHeightSpec); measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; recycler.recycleView(view);