package com.cheikh.lazywaimai.util; /* * Copyright (c) 2014,KJFrameForAndroid Open Source Project,张涛. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; import android.util.TypedValue; /** * 系统屏幕的一些操作<br> * * <b>创建时间</b> 2014-8-14 * * @author kymjs (https://github.com/kymjs) * @version 1.1 */ public final class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { Resources r = context.getResources(); float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, r.getDisplayMetrics()); return (int) px; } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 sp */ public static int px2sp(Context context, float pxValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 根据手机的分辨率从 sp 的单位 转成为 px */ public static int sp2px(Context context, float spValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } /** * 获取dialog宽度 */ public static int getDialogW(Context aty) { DisplayMetrics dm = new DisplayMetrics(); dm = aty.getResources().getDisplayMetrics(); int w = dm.widthPixels - 100; return w; } /** * 获取屏幕宽度 */ public static int getScreenW(Context aty) { DisplayMetrics dm = new DisplayMetrics(); dm = aty.getResources().getDisplayMetrics(); int w = dm.widthPixels; return w; } /** * 获取屏幕高度 */ public static int getScreenH(Context aty) { DisplayMetrics dm = new DisplayMetrics(); dm = aty.getResources().getDisplayMetrics(); int h = dm.heightPixels; return h; } }