Example usage for android.view View measure

List of usage examples for android.view View measure

Introduction

In this page you can find the example usage for android.view View measure.

Prototype

public final void measure(int widthMeasureSpec, int heightMeasureSpec) 

Source Link

Document

This is called to find out how big a view should be.

Usage

From source file:Main.java

public static void getListViewSize(ListView listView, Context context) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null || listAdapter.getCount() < 2) {
        // pre-condition
        return;//from w  w  w  .  ja  v a 2s . c  o  m
    }

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int totalHeight = 0;
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(size.x, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, listItem.getHeight());
        totalHeight += listItem.getMeasuredHeight();
    }

    totalHeight += listView.getPaddingTop() + listView.getPaddingBottom();
    totalHeight += (listView.getDividerHeight() * (listAdapter.getCount() + 1));
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight;
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:Main.java

public static int getMeasuredHeight(View view) {
    if (view == null) {
        throw new IllegalArgumentException("view is null");
    }//  w w w.  j  av a2 s  . co  m

    view.measure(0, 0);
    return view.getMeasuredHeight();
}

From source file:Main.java

private static Bitmap loadBitmapFromView(View view) {
    int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
    view.measure(specWidth, specWidth);
    int questionWidth = view.getMeasuredWidth();
    int questionHeight = view.getMeasuredHeight();
    Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(canvas);//from   w w w . ja v  a2  s  . co  m
    return bitmap;
}

From source file:Main.java

public static int getWidth(View view) {
    if (view == null) {
        throw new IllegalArgumentException("view is null");
    }/* w ww. j a  v  a  2 s  .  c  o  m*/

    view.measure(0, 0);
    return view.getMeasuredWidth();
}

From source file:Main.java

public static int getMeasuredWidth(View view) {
    if (view == null) {
        throw new IllegalArgumentException("view is null");
    }/*www  . j  a v  a2s . c  om*/

    view.measure(0, 0);
    return view.getMeasuredWidth();
}

From source file:Main.java

public static int setListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return 0;
    }/* www.jav  a  2  s. co  m*/
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem != null) {
            listItem.measure(0, 0);
            //                CommonUtils.LOGV("samton", "listItem Height === " + listItem.getMeasuredHeight());
            totalHeight += listItem.getMeasuredHeight();
        }
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
    listView.setLayoutParams(params);
    //        CommonUtils.LOGV("samton", "totalHeight Height === " +totalHeight
    //        );
    return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
}

From source file:Main.java

public static Drawable convertViewToDrawable(View view) {
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);
    view.layout(UPPER_LEFT_X, UPPER_LEFT_Y, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);/*from  w  w  w  . ja v a 2 s .  c  o  m*/
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(viewBmp);
}

From source file:Main.java

/**
 * Collapsing the view into wrapping their contents
 *
 * @param v the target view// w  ww .  j ava2 s  .com
 */
public static void collapse(final View v, Animation.AnimationListener listener) {
    final int initialHeight = v.getMeasuredHeight();
    v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    collapse(v, initialHeight, v.getMeasuredHeight(), 500, listener);
}

From source file:Main.java

public static void setListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        // do nothing return null
        return;/*from   w  w w. j  a  va  2 s . co m*/
    }
    // set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        if (listItem != null) {
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
    }
    // setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    if (params != null) {
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
    }
}

From source file:Main.java

public static Animation getExpandViewAnimation(final View view, int duration) {

    view.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    final int targetHeight = view.getMeasuredHeight();

    view.getLayoutParams().height = 0;/*ww w .  j av  a2  s .c o m*/
    view.setVisibility(View.VISIBLE);
    Animation animation = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            view.getLayoutParams().height = interpolatedTime == 1 ? RelativeLayout.LayoutParams.WRAP_CONTENT
                    : (int) (targetHeight * interpolatedTime);
            view.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    // animation.setDuration((int) (targetHeight /
    // view.getContext().getResources().getDisplayMetrics().density));
    animation.setDuration(duration);
    return animation;
}