安卓 dp转px

public int dip2px(float dipValue) {
    final float scale = getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
}
{
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,60,getResources().getDisplayMetrics());
}
 
 
private static int dp2px(Context context, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            context.getResources().getDisplayMetrics());
}

猜你喜欢

转载自blog.csdn.net/m0_37358427/article/details/80927804