Java tutorial
//package com.java2s; import android.annotation.SuppressLint; import android.graphics.Point; import android.os.Build; import android.util.Log; import android.view.Display; import android.view.View; public class Main { public static int getCentWidthByView(View view) { final int mMeasuredWidth = View.MeasureSpec.getSize(view.getMeasuredWidth()); Log.i("width", "mywidth--" + mMeasuredWidth); final int mWidth = View.MeasureSpec.getSize(view.getWidth()); Log.i("width", "mywidth--" + mWidth); return (int) ((float) mWidth / 2 + 0.5f); } @SuppressLint("NewApi") public static void getSize(Display display, Point outSize) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { display.getSize(outSize); } else { outSize.x = display.getWidth(); outSize.y = display.getHeight(); } } }