package com.martin.simpledevelop.utils.view;
import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.martin.simpledevelop.utils.global.SaAppConfig;
import com.martin.simpledevelop.utils.log.SaLogUtils;
import com.martin.simpledevelop.utils.screen.SaScreenUtils;
import java.lang.reflect.Method;
/**
* @Description 视图 工具类,对控件的高宽进行调整
* @File SaViewUtils.java
* @Package com.martin.simpledevelop.utils.view
* @Date 2015年6月26日上午1:27:13
* @Author Donghongyu 1358506549@qq.com
* @Version v1.0.0
*/
public class SaViewUtils {
/**
* 无效值
*/
public static final int INVALID = Integer.MIN_VALUE;
/**
* 描述:重置AbsListView的高度. item 的最外层布局要用
* RelativeLayout,如果计算的不准,就为RelativeLayout指定一个高度
*
* @param absListView the abs list view
* @param lineNumber 每行几个 ListView一行一个item
* @param verticalSpace the vertical space
*/
public static void setAbsListViewHeight(AbsListView absListView,
int lineNumber, int verticalSpace) {
int totalHeight = getAbsListViewHeight(absListView, lineNumber,
verticalSpace);
ViewGroup.LayoutParams params = absListView.getLayoutParams();
params.height = totalHeight;
((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
absListView.setLayoutParams(params);
}
/**
* 描述:获取AbsListView的高度.
*
* @param absListView the abs list view
* @param lineNumber 每行几个 ListView一行一个item
* @param verticalSpace the vertical space
* @return the abs list view height
*/
public static int getAbsListViewHeight(AbsListView absListView,
int lineNumber, int verticalSpace) {
int totalHeight = 0;
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
absListView.measure(w, h);
ListAdapter mListAdapter = absListView.getAdapter();
if (mListAdapter == null) {
return totalHeight;
}
int count = mListAdapter.getCount();
if (absListView instanceof ListView) {
for (int i = 0; i < count; i++) {
View listItem = mListAdapter.getView(i, null, absListView);
listItem.measure(w, h);
totalHeight += listItem.getMeasuredHeight();
}
if (count == 0) {
totalHeight = verticalSpace;
} else {
totalHeight = totalHeight
+ (((ListView) absListView).getDividerHeight() * (count - 1));
}
} else if (absListView instanceof GridView) {
int remain = count % lineNumber;
if (remain > 0) {
remain = 1;
}
if (mListAdapter.getCount() == 0) {
totalHeight = verticalSpace;
} else {
View listItem = mListAdapter.getView(0, null, absListView);
listItem.measure(w, h);
int line = count / lineNumber + remain;
totalHeight = line * listItem.getMeasuredHeight() + (line - 1)
* verticalSpace;
}
}
return totalHeight;
}
/**
* 测量这个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);
}
}
}
/**
* 描述:dip转换为px.
*
* @param context the context
* @param dipValue the dip value
* @return px值
*/
public static float dip2px(Context context, float dipValue) {
DisplayMetrics mDisplayMetrics = SaScreenUtils
.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 = SaScreenUtils
.getDisplayMetrics(context);
return pxValue / mDisplayMetrics.density;
}
/**
* 描述:sp转换为px.
*
* @param context the context
* @param spValue the sp value
* @return sp值
*/
public static float sp2px(Context context, float spValue) {
DisplayMetrics mDisplayMetrics = SaScreenUtils
.getDisplayMetrics(context);
return applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue,
mDisplayMetrics);
}
/**
* 描述:px转换为sp.
*
* @param context the context
* @return sp值
*/
public static float px2sp(Context context, float pxValue) {
DisplayMetrics mDisplayMetrics = SaScreenUtils
.getDisplayMetrics(context);
return pxValue / mDisplayMetrics.scaledDensity;
}
/**
* 描述:根据屏幕大小缩放.
*
* @param context the context
* @return the int
*/
public static int scaleValue(Context context, float value) {
DisplayMetrics mDisplayMetrics = SaScreenUtils
.getDisplayMetrics(context);
// 为了兼容尺寸小密度大的情况
if (mDisplayMetrics.scaledDensity > SaAppConfig.UI_DENSITY) {
// 密度
if (mDisplayMetrics.widthPixels > SaAppConfig.UI_WIDTH) {
value = value * (1.3f - 1.0f / mDisplayMetrics.scaledDensity);
} else if (mDisplayMetrics.widthPixels < SaAppConfig.UI_WIDTH) {
value = value * (1.0f - 1.0f / mDisplayMetrics.scaledDensity);
}
}
return scale(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
value);
}
/**
* 描述:根据屏幕大小缩放文本.
*
* @param context the context
* @return the int
*/
public static int scaleTextValue(Context context, float value) {
DisplayMetrics mDisplayMetrics = SaScreenUtils
.getDisplayMetrics(context);
// 为了兼容尺寸小密度大的情况
if (mDisplayMetrics.scaledDensity > 2) {
// 缩小到密度分之一
// value = value*(1.1f - 1.0f/mDisplayMetrics.scaledDensity);
}
return scale(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
value);
}
/**
* 描述:根据屏幕大小缩放.
*
* @param displayWidth the display width
* @param displayHeight the display height
* @param pxValue the px value
* @return the int
*/
public static int scale(int displayWidth, int displayHeight, float pxValue) {
if (pxValue == 0) {
return 0;
}
float scale = 1;
try {
float scaleWidth = (float) displayWidth / SaAppConfig.UI_WIDTH;
float scaleHeight = (float) displayHeight / SaAppConfig.UI_HEIGHT;
scale = Math.min(scaleWidth, scaleHeight);
} catch (Exception e) {
}
return Math.round(pxValue * scale + 0.5f);
}
/**
* 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;
}
/**
* 缩放文字大小
*
* @param textView button
* @param size sp值
* @return
*/
public static void setSPTextSize(TextView textView, float size) {
float scaledSize = scaleTextValue(textView.getContext(), size);
textView.setTextSize(scaledSize);
}
/**
* 缩放文字大小,这样设置的好处是文字的大小不和密度有关, 能够使文字大小在不同的屏幕上显示比例正确
*
* @param textView button
* @param sizePixels px值
* @return
*/
public static void setTextSize(TextView textView, float sizePixels) {
float scaledSize = scaleTextValue(textView.getContext(), sizePixels);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, scaledSize);
}
/**
* 缩放文字大小
*
* @param context
* @param textPaint
* @param sizePixels px值
* @return
*/
public static void setTextSize(Context context, TextPaint textPaint,
float sizePixels) {
float scaledSize = scaleTextValue(context, sizePixels);
textPaint.setTextSize(scaledSize);
}
/**
* 缩放文字大小
*
* @param context
* @param paint
* @param sizePixels px值
* @return
*/
public static void setTextSize(Context context, Paint paint,
float sizePixels) {
float scaledSize = scaleTextValue(context, sizePixels);
paint.setTextSize(scaledSize);
}
/**
* 设置View的PX尺寸
*
* @param view 如果是代码new出来的View,需要设置一个适合的LayoutParams
* @param widthPixels
* @param heightPixels
*/
public static void setViewSize(View view, int widthPixels, int heightPixels) {
int scaledWidth = scaleValue(view.getContext(), widthPixels);
int scaledHeight = scaleValue(view.getContext(), heightPixels);
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params == null) {
SaLogUtils.e(SaViewUtils.class,
"setViewSize出错,如果是代码new出来的View,需要设置一个适合的LayoutParams");
return;
}
if (widthPixels != INVALID) {
params.width = scaledWidth;
}
if (heightPixels != INVALID) {
params.height = scaledHeight;
}
view.setLayoutParams(params);
}
/**
* 设置PX padding.
*
* @param view the view
* @param left the left padding in pixels
* @param top the top padding in pixels
* @param right the right padding in pixels
* @param bottom the bottom padding in pixels
*/
public static void setPadding(View view, int left, int top, int right,
int bottom) {
int scaledLeft = scaleValue(view.getContext(), left);
int scaledTop = scaleValue(view.getContext(), top);
int scaledRight = scaleValue(view.getContext(), right);
int scaledBottom = scaleValue(view.getContext(), bottom);
view.setPadding(scaledLeft, scaledTop, scaledRight, scaledBottom);
}
/**
* 设置 PX margin.
*
* @param view the view
* @param left the left margin in pixels
* @param top the top margin in pixels
* @param right the right margin in pixels
* @param bottom the bottom margin in pixels
*/
public static void setMargin(View view, int left, int top, int right,
int bottom) {
int scaledLeft = scaleValue(view.getContext(), left);
int scaledTop = scaleValue(view.getContext(), top);
int scaledRight = scaleValue(view.getContext(), right);
int scaledBottom = scaleValue(view.getContext(), bottom);
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams mMarginLayoutParams = (ViewGroup.MarginLayoutParams) view
.getLayoutParams();
if (mMarginLayoutParams != null) {
if (left != INVALID) {
mMarginLayoutParams.leftMargin = scaledLeft;
}
if (right != INVALID) {
mMarginLayoutParams.rightMargin = scaledRight;
}
if (top != INVALID) {
mMarginLayoutParams.topMargin = scaledTop;
}
if (bottom != INVALID) {
mMarginLayoutParams.bottomMargin = scaledBottom;
}
view.setLayoutParams(mMarginLayoutParams);
}
}
}
/**
* Convert a translucent themed Activity
* 转换一个半透明的主题活动
* {@link android.R.attr#windowIsTranslucent} to a fullscreen opaque
* Activity.
* <p/>
* Call this whenever the background of a translucent Activity has changed
* to become opaque. Doing so will allow the {@link android.view.Surface} of
* the Activity behind to be released.
* <p/>
* This call has no effect on non-translucent activities or on activities
* with the {@link android.R.attr#windowIsFloating} attribute.
*/
public static void convertActivityFromTranslucent(Activity activity) {
try {
Method method = Activity.class.getDeclaredMethod("convertFromTranslucent");
method.setAccessible(true);
method.invoke(activity);
} catch (Throwable t) {
}
}
/**
* Convert a translucent themed Activity
* 转换一个半透明的主题活动
* {@link android.R.attr#windowIsTranslucent} back from opaque to
* translucent following a call to
* {@link #convertActivityFromTranslucent(android.app.Activity)} .
* <p/>
* Calling this allows the Activity behind this one to be seen again. Once
* all such Activities have been redrawn
* <p/>
* This call has no effect on non-translucent activities or on activities
* with the {@link android.R.attr#windowIsFloating} attribute.
*/
public static void convertActivityToTranslucent(Activity activity) {
try {
Class<?>[] classes = Activity.class.getDeclaredClasses();
Class<?> translucentConversionListenerClazz = null;
for (Class clazz : classes) {
if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
translucentConversionListenerClazz = clazz;
}
}
Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
translucentConversionListenerClazz);
method.setAccessible(true);
method.invoke(activity, new Object[]{
null
});
} catch (Throwable t) {
}
}
}