PixelUtils: pixel Conversion Tool, pixelutils Conversion Tool

Source: Internet
Author: User

PixelUtils: pixel Conversion Tool, pixelutils Conversion Tool

/** Pixel Conversion Tool */public class PixelUtils {/*** The context. */private static Context mContext = CustomApplcation. getInstance ();/*** convert dp to px. ** @ param value the value * @ return the int */public static int dp2px (float value) {final float scale = mContext. getResources (). getDisplayMetrics (). densityDpi; return (int) (value * (scale/160) + 0.5f);}/*** dp to px. ** @ param value the value * @ param context the context * @ return the int */public static int dp2px (float value, Context context) {final float scale = context. getResources (). getDisplayMetrics (). densityDpi; return (int) (value * (scale/160) + 0.5f);}/*** px to dp. ** @ param value the value * @ return the int */public static int px2dp (float value) {final float scale = mContext. getResources (). getDisplayMetrics (). densityDpi; return (int) (value * 160)/scale + 0.5f);}/*** px to dp. ** @ param value the value * @ param context the context * @ return the int */public static int px2dp (float value, Context context) {final float scale = context. getResources (). getDisplayMetrics (). densityDpi; return (int) (value * 160)/scale + 0.5f);}/*** sp to px. ** @ param value the value * @ return the int */public static int sp2px (float value) {Resources r; if (mContext = null) {r = Resources. getSystem ();} else {r = mContext. getResources ();} float spvalue = value * r. getDisplayMetrics (). scaledDensity; return (int) (spvalue + 0.5f);}/*** sp to px. ** @ param value the value * @ param context the context * @ return the int */public static int sp2px (float value, Context context) {Resources r; if (context = null) {r = Resources. getSystem ();} else {r = context. getResources ();} float spvalue = value * r. getDisplayMetrics (). scaledDensity; return (int) (spvalue + 0.5f);}/*** px to sp. ** @ param value the value * @ return the int */public static int px2sp (float value) {final float scale = mContext. getResources (). getDisplayMetrics (). scaledDensity; return (int) (value/scale + 0.5f);}/*** px to sp. ** @ param value the value * @ param context the context * @ return the int */public static int px2sp (float value, Context context) {final float scale = context. getResources (). getDisplayMetrics (). scaledDensity; return (int) (value/scale + 0.5f );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.