Java tutorial
//package com.java2s; import android.annotation.SuppressLint; import android.graphics.Point; import android.os.Build; import android.view.Display; import android.view.WindowManager; public class Main { /** * @param wm * @param baseY * @param baseX * @return */ public static float sizeYByX(WindowManager wm, float baseY, float baseX) { final Display disp = wm.getDefaultDisplay(); Point mPoint = new Point(); getSize(disp, mPoint); float displayWidth = mPoint.x; float scaleX = displayWidth / baseX; return baseY * scaleX + 0.5f; } public static float sizeYByX(WindowManager windowManager, float baseY, float basex, int exceptDis) { final Display display = windowManager.getDefaultDisplay(); Point mPoint = new Point(); getSize(display, mPoint); if (exceptDis > mPoint.x) { return 0.0f; } float displayWidth = mPoint.x - exceptDis; float scaleX = displayWidth / basex; return baseY * scaleX; } @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(); } } }