/* * Copyright (C) 2012 www.amsoft.cn * * 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. */ package com.yzy.supercleanmaster.utils; import android.content.Context; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.View; import android.view.View.MeasureSpec; import android.view.ViewGroup; import android.view.ViewParent; // TODO: Auto-generated Javadoc public class ViewUtil { /** * 无效值 */ public static final int INVALID = Integer.MIN_VALUE; /** * 描述:dip转换为px. * * @param context * the context * @param dipValue * the dip value * @return px值 */ public static float dip2px(Context context, float dipValue) { DisplayMetrics mDisplayMetrics = AppUtil.getDisplayMetrics(context); return applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, mDisplayMetrics); } /** * 描述:px转换为dip. * * @param context the context * @param pxValue the px value * @return dip值 */ public static float px2dip(Context context, float pxValue) { DisplayMetrics mDisplayMetrics = AppUtil.getDisplayMetrics(context); return pxValue / mDisplayMetrics.density; } /** * TypedValue官方源码中的算法,任意单位转换为PX单位 * @param unit TypedValue.COMPLEX_UNIT_DIP * @param value 对应单位的值 * @param metrics 密度 * @return px值 */ public static float applyDimension(int unit, float value, DisplayMetrics metrics){ switch (unit) { case TypedValue.COMPLEX_UNIT_PX: return value; case TypedValue.COMPLEX_UNIT_DIP: return value * metrics.density; case TypedValue.COMPLEX_UNIT_SP: return value * metrics.scaledDensity; case TypedValue.COMPLEX_UNIT_PT: return value * metrics.xdpi * (1.0f/72); case TypedValue.COMPLEX_UNIT_IN: return value * metrics.xdpi; case TypedValue.COMPLEX_UNIT_MM: return value * metrics.xdpi * (1.0f/25.4f); } return 0; } /** * 测量这个view 最后通过getMeasuredWidth()获取宽度和高度. * * @param view * 要测量的view * @return 测量过的view */ public static void measureView(View view) { ViewGroup.LayoutParams p = view.getLayoutParams(); if (p == null) { p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width); int lpHeight = p.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } view.measure(childWidthSpec, childHeightSpec); } /** * 获得这个View的宽度 测量这个view,最后通过getMeasuredWidth()获取宽度. * * @param view * 要测量的view * @return 测量过的view的宽度 */ public static int getViewWidth(View view) { measureView(view); return view.getMeasuredWidth(); } /** * 获得这个View的高度 测量这个view,最后通过getMeasuredHeight()获取高度. * * @param view * 要测量的view * @return 测量过的view的高度 */ public static int getViewHeight(View view) { measureView(view); return view.getMeasuredHeight(); } /** * 从父亲布局中移除自己 * * @param v */ public static void removeSelfFromParent(View v) { ViewParent parent = v.getParent(); if (parent != null) { if (parent instanceof ViewGroup) { ((ViewGroup) parent).removeView(v); } } } }