Android development skills 4: Mobile resolution processing tools and Android development skills

Source: Internet
Author: User

Android development skills 4: Mobile resolution processing tools and Android development skills

/***** @ Description: Mobile resolution processing tool class * @ author: * @ see: * @ since: * @ copyright©* @ Date: August 13, 2014 */public class DensityUtil {/*** convert the unit from dp to px (pixel) */public static int dip2px (Context context, float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);}/*** based on the resolution of the phone) to dp */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f);}/*** convert the px value to the sp value, ensure that the text size remains unchanged ** @ param pxValue * @ param fontScale (attribute scaledDensity in the DisplayMetrics class) * @ return */public static int px2sp (float pxValue, float fontScale) {return (int) (pxValue/fontScale + 0.5f);}/*** convert the sp value to the px value, ensure that the text size remains unchanged ** @ param spValue * @ param fontScale (attribute scaledDensity in the DisplayMetrics class) * @ return */public static int sp2px (float spValue, float fontScale) {return (int) (spValue * fontScale + 0.5f);}/*** obtain screen density * @ param context * @ return */public static int getScreenDensityDpi (Activity context) {DisplayMetrics metric = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (metric); int density = metric. densityDpi; CLog. d ("DensityUtil density =", density + ""); return density ;} /*** obtain the screen height * @ param context * @ return */public static int getScreenHeight (Activity context) {DisplayMetrics metric = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (metric); int screenHeight = metric. heightPixels; CLog. d ("DensityUtil screenHight =", screenHeight + ""); return screenHeight ;} /*** get screen width * @ param context * @ return */public static int getScreenWidth (Activity context) {DisplayMetrics metric = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (metric); int screenWidth = metric. widthPixels; CLog. d ("DensityUtil screenWidth =", screenWidth + ""); return screenWidth ;} /*** return the height of the status bar/notification bar ** @ param activity * @ return */public static int getStatusHeight (Activity activity) {Rect frame = new Rect (); activity. getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int statusBarHeight = frame. top; return statusBarHeight ;}}

Related Article

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.