Android Open Source - FrameLite Dimension Utils






From Project

Back to project page FrameLite.

License

The source code is released under:

GNU General Public License

If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.miku.framelite.utils;
/*from  www .j  av  a 2 s  .  co  m*/
import java.lang.reflect.Method;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class DimensionUtils {
  private static final String TAG = DimensionUtils.class.getSimpleName();

  private DimensionUtils(){/** singleInstance **/}
  
  /**
   * ??????????? dp ?????? ???? px(???)
   */
  public static int dip2px(Context context, float dpValue) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
  }

  /**
   * ??????????? px(???) ?????? ???? dp
   */
  public static int px2dip(Context context, float pxValue) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
  }
  
  /**
   * ???????????sp ?????? ???? px(???)
   */
  public static int sp2px(Context context, float spValue) {
    float scale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * scale + 0.5f);
  }
  
  /**
   * ??????????? px(???) ?????? ???? sp
   */
  public static int px2sp(Context context,float pxValue){
    float scale=context.getResources().getDisplayMetrics().scaledDensity;
    return (int)(pxValue/scale+0.5f);
  }
  
  /**
   * ??????????
   * 
   * @param context
   * @return ??[??,??]
   */
  public static int[] getDisplayScreenResolution(Context context) {
    int ver = Build.VERSION.SDK_INT;
    int screen[] = new int[2];

    DisplayMetrics dm = new DisplayMetrics();

    WindowManager wmanager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    android.view.Display display = wmanager.getDefaultDisplay();
    display.getMetrics(dm);

    screen[0] = dm.widthPixels;
    screen[1] = dm.heightPixels;

    Log.e(TAG, "Run1 first get resolution:" + dm.widthPixels + " * " + dm.heightPixels
        + ", ver " + ver);
    if (ver < 13) {
      screen[1] = dm.heightPixels;
    } else if (ver == 13) {
      try {
        Method mt = display.getClass().getMethod("getRealHeight");
        screen[1] = (Integer) mt.invoke(display);
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else if (ver > 13) {
      try {
        Method mt = display.getClass().getMethod("getRealMetrics",DisplayMetrics.class);
        mt.invoke(display, dm);
        screen[0] = dm.widthPixels;
        screen[1] = dm.heightPixels;
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    Log.e(TAG, "Run2 Calibration  resolution:" + screen[0] + " * " + screen[1]);
    return screen;
  }
}




Java Source Code List

com.miku.framelite.FrameApplication.java
com.miku.framelite.FrameBaseActivity.java
com.miku.framelite.FrameOrmBaseActivity.java
com.miku.framelite.adapter.FrameBaseAdapter.java
com.miku.framelite.annotations.ViewInject.java
com.miku.framelite.api.BaseRequest.java
com.miku.framelite.api.IRequest.java
com.miku.framelite.api.RetResult.java
com.miku.framelite.api.core.Executor.java
com.miku.framelite.api.database.AbstractDatabaseRequest.java
com.miku.framelite.api.database.AbstractOrmDatabaseRequest.java
com.miku.framelite.api.database.DatabaseQueryRequest.java
com.miku.framelite.api.database.DatabaseType.java
com.miku.framelite.api.http.AbstractHttpRequest.java
com.miku.framelite.api.http.HttpStringGetRequest.java
com.miku.framelite.api.http.HttpStringPostRequest.java
com.miku.framelite.api.http.HttpType.java
com.miku.framelite.api.webservice.AbstractWebServiceRequest.java
com.miku.framelite.api.webservice.WebServiceConnectionSE.java
com.miku.framelite.api.webservice.WebServiceHttpTransportSE.java
com.miku.framelite.api.webservice.WebServiceJsonRequest.java
com.miku.framelite.api.webservice.WebServiceStringRequest.java
com.miku.framelite.httpx.IDownloadHandler.java
com.miku.framelite.httpx.IHttpX.java
com.miku.framelite.httpx.core.DownloadHandler.java
com.miku.framelite.httpx.core.HttpX.java
com.miku.framelite.services.CrashHandler.java
com.miku.framelite.utils.BitmapUtils.java
com.miku.framelite.utils.DateUtils.java
com.miku.framelite.utils.DimensionUtils.java
com.miku.framelite.utils.EncryptionUtils.java
com.miku.framelite.utils.HttpUtils.java
com.miku.framelite.utils.Log.java
com.miku.framelite.utils.TelePhoneUtils.java
com.miku.framelite.utils.ViewUtils.java