88 lines
2.5 KiB
Java
88 lines
2.5 KiB
Java
|
|
package com.pukka.jiecheng.utils;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.view.ViewGroup;
|
||
|
|
|
||
|
|
import com.pukka.jiecheng.R;
|
||
|
|
|
||
|
|
import java.math.BigDecimal;
|
||
|
|
|
||
|
|
public class UiUtils {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 赛事卡片推荐位类型
|
||
|
|
*/
|
||
|
|
public static int POSITION_TYPE_SPORT_CARD = 1;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 把数字转化成Dimen文件里对应的值
|
||
|
|
*
|
||
|
|
* @param mContext
|
||
|
|
* @param number 原始数值
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static int numberConvertDimenValue(Context mContext, int number) {
|
||
|
|
int dimenId = mContext.getResources().getIdentifier("qb_px_" + number, "dimen", mContext.getPackageName());
|
||
|
|
int value = mContext.getResources().getDimensionPixelSize(dimenId);
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 重新设置宽高
|
||
|
|
*
|
||
|
|
* @param mContext
|
||
|
|
* @param width
|
||
|
|
* @param height
|
||
|
|
* @param params
|
||
|
|
*/
|
||
|
|
public static void resetLayoutParams(Context mContext, int width, int height, ViewGroup.LayoutParams params) {
|
||
|
|
resetLayoutParams(mContext,width,height,0,params);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 重新设置宽高
|
||
|
|
*
|
||
|
|
* @param mContext
|
||
|
|
* @param width
|
||
|
|
* @param height
|
||
|
|
* @param recommendedPositionType 推荐位类型 1赛事卡片 0 其他类型
|
||
|
|
* @param params
|
||
|
|
*/
|
||
|
|
public static void resetLayoutParams(Context mContext, int width, int height, int recommendedPositionType,ViewGroup.LayoutParams params) {
|
||
|
|
int realWith = UiUtils.numberConvertDimenValue(mContext, width);
|
||
|
|
int realHeight = UiUtils.numberConvertDimenValue(mContext, height);
|
||
|
|
double rate = UiUtils.divNum(realWith, realHeight);
|
||
|
|
//1赛事卡片推荐位固定高度22 其他类型23
|
||
|
|
int showHeight = mContext.getResources().getDimensionPixelSize(recommendedPositionType == POSITION_TYPE_SPORT_CARD ? R.dimen.qb_px_22 : R.dimen.qb_px_23);
|
||
|
|
params.width = (int) (rate * showHeight);
|
||
|
|
params.height = showHeight;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 提供精确的除法运算
|
||
|
|
*
|
||
|
|
* @param v1 被除数
|
||
|
|
* @param v2 除数
|
||
|
|
* @return 两个参数的商
|
||
|
|
*/
|
||
|
|
public static double divNum(int v1, int v2) {
|
||
|
|
try {
|
||
|
|
if (0 == v1 || 0 == v2) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
BigDecimal b1 = BigDecimal.valueOf(v1);
|
||
|
|
BigDecimal b2 = BigDecimal.valueOf(v2);
|
||
|
|
return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||
|
|
} catch (IllegalArgumentException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|