Java Code Examples for android.view.ViewGroup#getMeasuredWidth()

The following examples show how to use android.view.ViewGroup#getMeasuredWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CenterLinearLayoutManager.java    From ProjectX with Apache License 2.0 6 votes vote down vote up
@Override
public void layoutDecorated(@NonNull View child, int left, int top, int right, int bottom) {
    if (!mCenter) {
        super.layoutDecorated(child, left, top, right, bottom);
        return;
    }
    final int leftDecorationWidth = getLeftDecorationWidth(child);
    final int topDecorationHeight = getTopDecorationHeight(child);
    final int rightDecorationWidth = getRightDecorationWidth(child);
    final int bottomDecorationHeight = getBottomDecorationHeight(child);
    final ViewGroup parent = (ViewGroup) child.getParent();
    final int offset;
    if (getOrientation() == HORIZONTAL) {
        final int contentHeight = parent.getMeasuredHeight() -
                parent.getPaddingTop() - parent.getPaddingBottom();
        offset = (contentHeight - (bottom - top)) / 2;
        child.layout(left + leftDecorationWidth, top + topDecorationHeight + offset,
                right - rightDecorationWidth, bottom - bottomDecorationHeight + offset);
    } else {
        final int contentWidth = parent.getMeasuredWidth() -
                parent.getPaddingLeft() - parent.getPaddingRight();
        offset = (contentWidth - (right - left)) / 2;
        child.layout(left + leftDecorationWidth + offset, top + topDecorationHeight,
                right - rightDecorationWidth + offset, bottom - bottomDecorationHeight);
    }
}
 
Example 2
Source File: FreeRadioGroup.java    From FreeRadioGroup with Apache License 2.0 6 votes vote down vote up
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (moveable) {
        ViewGroup parentView = ((ViewGroup) getParent());
        MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
        viewWidth = getRight() - getLeft();
        viewHight = getBottom() - getTop();
        parentWidth = parentView.getMeasuredWidth();
        parentHeight = parentView.getMeasuredHeight();
        minLeftMargin = lp.leftMargin;
        leftPadding = parentView.getPaddingLeft();
        rightDistance = lp.rightMargin + parentView.getPaddingRight();
        maxLeftMargin = parentWidth - rightDistance - viewWidth - leftPadding;
        minTopMargin = lp.topMargin;
        topPadding = parentView.getPaddingTop();
        bottomDistance = lp.bottomMargin + parentView.getPaddingBottom();
        maxTopMargin = parentHeight - bottomDistance - viewHight - topPadding;
    }
}
 
Example 3
Source File: ViewToImageUtil.java    From ViewToImage with Apache License 2.0 6 votes vote down vote up
/**
     * viewGroup 转换为 bitmap集合
     *
     * @param viewGroup
     * @return
     */
    public static List<BitmapWithHeight> getWholeViewToBitmap(final ViewGroup viewGroup, List<BitmapWithHeight> list) {
        int width = viewGroup.getMeasuredWidth();
        if (viewGroup instanceof ListView) {
            list.addAll(getWholeListViewItemsToBitmap((ListView) viewGroup));
        } else if (viewGroup instanceof RecyclerView) {
//                RecyclerView recyclerView = (RecyclerView) child;
//                list.addAll(getWholeRecyclerViewItemsToBitmap(recyclerView));
            list.add(getWholeViewToBitmap(viewGroup, width));
        } else if (viewGroup instanceof AbsListView) {
            // TODO scrollView, GridView等
            list.add(getWholeViewToBitmap(viewGroup, width));
        } else {
            int count = viewGroup.getChildCount();
            for (int i = 0; i < count; i++) {
                View child = viewGroup.getChildAt(i);
                if (isScrollableView(child)) {
                    getWholeViewToBitmap((ViewGroup) child, list);
                } else {
                    list.add(getWholeViewToBitmap(child, width));
                }
            }
        }

        return list;
    }
 
Example 4
Source File: ViewToImageUtil.java    From ViewToImage with Apache License 2.0 6 votes vote down vote up
/**
 * viewGroup转换Bitmap
 *
 * @param viewGroup
 * @return
 */
public static Bitmap generateBigBitmap(final ViewGroup viewGroup, int width) {
    if (width > 0) {
        viewGroup.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    } else if (viewGroup.getWidth() <= 0) {
        viewGroup.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    } else {
        width = viewGroup.getWidth();
    }

    width = width > 0 ? width : viewGroup.getMeasuredWidth();
    LogUtils.d(width + "");
    LogUtils.d(String.format("width=%s, measuredWidth=%s, height=%s, measuredHeight=%s",
            viewGroup.getWidth(), viewGroup.getMeasuredWidth(), viewGroup.getHeight(), viewGroup.getMeasuredHeight()));
    int height = 0;
    List<BitmapWithHeight> list = getWholeViewToBitmap(viewGroup, new ArrayList<BitmapWithHeight>());
    for (BitmapWithHeight item : list) {
        height += item.height;
    }
    return generateBigBitmap(list, width, height);
}
 
Example 5
Source File: ExplosionFragment.java    From Backboard with Apache License 2.0 5 votes vote down vote up
private static void createCircle(Context context, ViewGroup rootView,
                                 SpringSystem springSystem,
                                 SpringConfig coasting,
                                 SpringConfig gravity,
                                 int diameter,
                                 Drawable backgroundDrawable) {

	final Spring xSpring = springSystem.createSpring().setSpringConfig(coasting);
	final Spring ySpring = springSystem.createSpring().setSpringConfig(gravity);

	// create view
	View view = new View(context);

	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(diameter, diameter);
	params.addRule(RelativeLayout.CENTER_IN_PARENT);
	view.setLayoutParams(params);
	view.setBackgroundDrawable(backgroundDrawable);

	rootView.addView(view);

	// generate random direction and magnitude
	double magnitude = Math.random() * 1000 + 3000;
	double angle = Math.random() * Math.PI / 2 + Math.PI / 4;

	xSpring.setVelocity(magnitude * Math.cos(angle));
	ySpring.setVelocity(-magnitude * Math.sin(angle));

	int maxX = rootView.getMeasuredWidth() / 2 + diameter;
	xSpring.addListener(new Destroyer(rootView, view, -maxX, maxX));

	int maxY = rootView.getMeasuredHeight() / 2 + diameter;
	ySpring.addListener(new Destroyer(rootView, view, -maxY, maxY));

	xSpring.addListener(new Performer(view, View.TRANSLATION_X));
	ySpring.addListener(new Performer(view, View.TRANSLATION_Y));

	// set a different end value to cause the animation to play
	xSpring.setEndValue(2);
	ySpring.setEndValue(9001);
}
 
Example 6
Source File: StickerModel.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
public void setCanvasSize(final Bitmap b, final ViewGroup imageGroup) {
    if (imageGroup.getMeasuredWidth() == 0) {
        imageGroup.post(new Runnable() {
            @Override
            public void run() {
                setSize(b, imageGroup);
            }
        });
    } else {
        setSize(b, imageGroup);
    }
}
 
Example 7
Source File: SimpleImageAdapter.java    From Android-ImagesPickers with Apache License 2.0 5 votes vote down vote up
public SimpleImageAdapter(ViewGroup container, boolean isDelete, int rowCount) {
	super(container.getContext(), null, R.layout.activity_gradview_item);
       this.containerWidth = container.getMeasuredWidth();
	this.isDelete = isDelete;
       this.rowCount = rowCount;
       initImgSize();
}
 
Example 8
Source File: MainActivity.java    From CameraBlur with Apache License 2.0 5 votes vote down vote up
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
    if(parent.getMeasuredHeight()>parent.getMeasuredWidth()) {
        viewHeight =(int)((parent.getMeasuredWidth()/spanCount)*4*1.0/3);
    }
    else  viewHeight =(int)((parent.getMeasuredWidth()/spanCount)*4*1.0/3);
    return new ViewHolder(view);
}
 
Example 9
Source File: StickerModel.java    From imsdk-android with MIT License 5 votes vote down vote up
public void setCanvasSize(final Bitmap b, final ViewGroup imageGroup) {
    if (imageGroup.getMeasuredWidth() == 0) {
        imageGroup.post(new Runnable() {
            @Override
            public void run() {
                setSize(b, imageGroup);
            }
        });
    } else {
        setSize(b, imageGroup);
    }
}
 
Example 10
Source File: StickerModel.java    From imsdk-android with MIT License 5 votes vote down vote up
private void setSize(Bitmap b, ViewGroup v) {
        int bW = b.getWidth();
        int bH = b.getHeight();

        int vW = v.getMeasuredWidth();
        int vH = v.getMeasuredHeight();

        float scalW = (float) vW / (float) bW;
        float scalH = (float) vH / (float) bH;

        ViewGroup.LayoutParams params = v.getLayoutParams();
        //如果图片小于viewGroup的宽高则把viewgroup设置为图片宽高
//        if (bW < vW && bH < vH) {
//            params.width = bW;
//            params.height = bH;
//            v.setLayoutParams(params);
//            return;
//        }
        if (bW >= bH) {
            params.width = vW;
            params.height = (int) (scalW * bH);
        } else {
            params.width = (int) (scalH * bW);
            params.height = vH;
        }
        if (params.width > vW) {
            float tempScaleW = (float) vW / (float) params.width;
            params.width = vW;
            params.height = (int) (params.height * tempScaleW);
        }
        if (params.height > vH) {
            float tempScaleH = (float) vH / (float) params.height;
            params.height = vH;
            params.width = (int) (params.width * tempScaleH);
        }
        v.setLayoutParams(params);
    }
 
Example 11
Source File: DimensionConverter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public static int stringToDimensionPixelSize(String dimension, DisplayMetrics metrics, ViewGroup parent, boolean horizontal) {
    if (dimension.endsWith("%")) {
        float pct = Float.parseFloat(dimension.substring(0, dimension.length() - 1)) / 100.0f;
        return (int) (pct * (horizontal ? parent.getMeasuredWidth() : parent.getMeasuredHeight()));
    }
    return stringToDimensionPixelSize(dimension, metrics);
}
 
Example 12
Source File: ShowTimeAllActivity.java    From ZLoading with MIT License 5 votes vote down vote up
private void initSize(ViewGroup parent)
{
    int measuredWidth = parent.getMeasuredWidth();
    itemView.setMinimumHeight(measuredWidth / 3);
    itemView.setPadding(measuredWidth / 9, measuredWidth / 9, measuredWidth / 9, measuredWidth / 9);
    int rgb = Color.rgb(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255));
    itemView.setBackgroundColor(rgb);
}
 
Example 13
Source File: ParentAdapter.java    From EpisodeListView with Apache License 2.0 5 votes vote down vote up
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.parent_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    parentWidth = parent.getMeasuredWidth();
    itemWidth = (parentWidth -
            (holder.textView.getPaddingLeft() + holder.textView.getPaddingRight()) * (GROUPS_COLUMN_COUNT))
            / GROUPS_COLUMN_COUNT + 1;
    return holder;
}
 
Example 14
Source File: ChildrenAdapter.java    From EpisodeListView with Apache License 2.0 5 votes vote down vote up
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.child_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    parentWidth = parent.getMeasuredWidth();
    itemWidth = (parentWidth -
            (holder.textView.getPaddingLeft() + holder.textView.getPaddingRight()) * (EPISODES_COLUMN_COUNT))
            / EPISODES_COLUMN_COUNT + 1;
    return holder;
}
 
Example 15
Source File: PagerSlidingTabStrip.java    From FragmentMixViewPager with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	if (!allowWidthFull)
		return;
	ViewGroup tabsLayout = getTabsLayout();
	if (tabsLayout == null
			|| tabsLayout.getMeasuredWidth() >= getMeasuredWidth())
		return;
	if (tabsLayout.getChildCount() <= 0)
		return;

	if (tabViews == null) {
		tabViews = new ArrayList<View>();
	} else {
		tabViews.clear();
	}
	for (int w = 0; w < tabsLayout.getChildCount(); w++) {
		tabViews.add(tabsLayout.getChildAt(w));
	}

	adjustChildWidthWithParent(
			tabViews,
			getMeasuredWidth() - tabsLayout.getPaddingLeft()
					- tabsLayout.getPaddingRight(), widthMeasureSpec,
			heightMeasureSpec);

	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
 
Example 16
Source File: MarginAdapterHelper.java    From Aurora with Apache License 2.0 4 votes vote down vote up
public void onCreateViewHolder(ViewGroup parent, View itemView) {
    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) itemView.getLayoutParams();
    lp.width = parent.getMeasuredWidth() - UiUtils.dip2px(itemView.getContext(), 2 * (mPagePadding + mShowLeftCardWidth));
    itemView.setLayoutParams(lp);
}
 
Example 17
Source File: MenuNavigationItemsAdapter.java    From fluentAppBar with Apache License 2.0 4 votes vote down vote up
@Override
public MenuNavItem onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_nav_item, parent, false);
    v.getLayoutParams().width = parent.getMeasuredWidth() / navItems.size();
    return new MenuNavItem(v);
}
 
Example 18
Source File: PictureInPictureGestureHelper.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private Point calculateTopRightCoordinates(@NonNull ViewGroup parent) {
  return new Point(parent.getMeasuredWidth() - pipWidth - framePadding,
                   framePadding + extraPaddingTop);
}
 
Example 19
Source File: RecyclerTabLayout.java    From RecyclerTabLayout with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public DefaultAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    TabTextView tabTextView = new TabTextView(parent.getContext());

    if (mTabSelectedTextColorSet) {
        tabTextView.setTextColor(tabTextView.createColorStateList(
                tabTextView.getCurrentTextColor(), mTabSelectedTextColor));
    }

    ViewCompat.setPaddingRelative(tabTextView, mTabPaddingStart, mTabPaddingTop,
            mTabPaddingEnd, mTabPaddingBottom);
    tabTextView.setTextAppearance(parent.getContext(), mTabTextAppearance);
    tabTextView.setGravity(Gravity.CENTER);
    tabTextView.setMaxLines(MAX_TAB_TEXT_LINES);
    tabTextView.setEllipsize(TextUtils.TruncateAt.END);

    if (mTabOnScreenLimit > 0) {
        int width = parent.getMeasuredWidth() / mTabOnScreenLimit;
        tabTextView.setMaxWidth(width);
        tabTextView.setMinWidth(width);

    } else {
        if (mTabMaxWidth > 0) {
            tabTextView.setMaxWidth(mTabMaxWidth);
        }
        tabTextView.setMinWidth(mTabMinWidth);
    }

    tabTextView.setTextAppearance(tabTextView.getContext(), mTabTextAppearance);
    if (mTabSelectedTextColorSet) {
        tabTextView.setTextColor(tabTextView.createColorStateList(
                tabTextView.getCurrentTextColor(), mTabSelectedTextColor));
    }
    if (mTabBackgroundResId != 0) {
        tabTextView.setBackgroundDrawable(
                AppCompatResources.getDrawable(tabTextView.getContext(), mTabBackgroundResId));
    }
    tabTextView.setLayoutParams(createLayoutParamsForTabs());
    return new ViewHolder(tabTextView);
}
 
Example 20
Source File: PictureInPictureGestureHelper.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private Point calculateBottomRightCoordinates(@NonNull ViewGroup parent) {
  return new Point(parent.getMeasuredWidth() - pipWidth - framePadding,
                   parent.getMeasuredHeight() - pipHeight - framePadding - extraPaddingBottom);
}