{"id":628,"date":"2013-05-17T15:57:59","date_gmt":"2013-05-17T15:57:59","guid":{"rendered":"http:\/\/hsiunien.sinaapp.com\/?p=628"},"modified":"2023-07-04T10:07:19","modified_gmt":"2023-07-04T02:07:19","slug":"android%e4%b8%ad%e5%87%a0%e4%b8%aa%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=628","title":{"rendered":"android\u4e2d\u51e0\u4e2a\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"\n<p>1\uff1a \u83b7\u53d6asset\u4e0b\u9762\u7684\u6587\u4ef6 \u83b7\u5f97\u6587\u4ef6IO\u6d41\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(context).getResources().getAssets().open(\"\u6587\u4ef6\u540d\")<\/code><\/pre>\n\n\n\n<p>2: \u901a\u8fc7\u6d41\u521b\u5efaBitmap\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BitmapFactory.decodeStream(is) \u8fd4\u56deBitma\u5bf9\u8c61\uff0c\u83b7\u53d6\u540e\u8981\u8bb0\u5f97\u5173\u95ed<\/code><\/pre>\n\n\n\n<p>3\uff1a\u901a\u8fc7\u6d41\u76f4\u63a5\u83b7\u53d6Drawab<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Bitmap b=new BitmapDrawable(is)<\/code><\/pre>\n\n\n\n<p>Bitmap Config \u53c2\u6570<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Bitmap.Config ALPHA_8\nBitmap.Config ARGB_4444\nBitmap.Config ARGB_8888\nBitmap.Config RGB_565\nA\u3000\u3000R\u3000\u3000G\u3000\u3000B\n\u900f\u660e\u5ea6\u3000\u7ea2\u8272\u3000\u7eff\u8272\u3000\u84dd\u8272\nBitmap.Config ARGB_4444 16 \u6bcf\u4e2a\u50cf\u7d20 \u5360\u56db\u4f4d\nBitmap.Config ARGB_8888 32 \u6bcf\u4e2a\u50cf\u7d20 \u5360\u516b\u4f4d\nBitmap.Config RGB_565 16 R\u53605\u4f4d G\u53606\u4f4d B\u53605\u4f4d \u6ca1\u6709\u900f\u660e\u5ea6\uff08A\uff09\n\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u662f\u7528argb888 \u4f46\u662f\u65e0\u53ef\u539a\u975e \u5b83\u4e5f\u76f8\u5bf9\u7684\u5f88\u5360\u5185\u5b58\n\u56e0\u4e3a\u4e00\u4e2a\u50cf\u7d2032\u4f4d 8\u4f4d\u4e00\u4e2a\u5b57\u8282 \u5982\u679c\u662f800*480\u7684\u56fe\u7247\u7684\u8bdd\u81ea\u5df1\u7b97 \u4f30\u8ba1\u67091M\u591a\u4e86<\/pre>\n\n\n\n<p>4: \u8ba1\u7b97\u5b57\u4f53\u5bbd\u5ea6 \u5728canvas\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Paint p=new Paint();\np.setTextSize(24); \nRect bounds = new Rect(); \np.getTextBounds(str,0, str.length(), bounds); \nint width = bounds.width();<\/pre>\n\n\n\n<p>5. \u00a0\u4ec0\u4e48\u662fdensity<\/p>\n\n\n\n<p>\u5f15\u7528<\/p>\n\n\n\n<p>1) density<br>density\u8868\u793a\u6bcf\u82f1\u5bf8\u6709\u591a\u5c11\u4e2a\u663e\u793a\u70b9(\u903b\u8f91\u503c)\uff0c\u5b83\u7684\u5355\u4f4d\u662fdpi\uff1adot per inch\uff0c\u901a\u5e38\u5c4f\u5e55\u5927\u65f6\uff0cdensity\u5c31\u5927\uff0c\u5c4f\u5e55\u5c0f\u65f6\uff0cdensity\u5c31\u5c0f\uff0c\u901a\u5e38<br>\u5c4f\u5e55\u5b9e\u9645\u5206\u8fa8\u7387\u4e3a240px*400px\u65f6\uff0cdensity=120<br>\u5c4f\u5e55\u5b9e\u9645\u5206\u8fa8\u7387\u4e3a320px*533px\uff0cdensity=160<br>\u5c4f\u5e55\u5b9e\u9645\u5206\u8fa8\u7387\u4e3a480px*800px\uff0cdensity=240<\/p>\n\n\n\n<p>2) &nbsp;\u5206\u8fa8\u7387<br>\u662f\u6574\u4e2a\u5c4f\u662f\u591a\u5c11\u70b9\uff0c\u6bd4\u5982800&#215;480\uff0c\u5b83\u662f\u8f6f\u4ef6\u7684\u663e\u793a\u5355\u4f4d<\/p>\n\n\n\n<p>3) \u4eceAndroid1.6\u7248\u672c\u5f00\u59cb\u652f\u6301density(\u5bf9\u5e94API Level 4)<br>\u7528\u4e8e\u89e3\u51b3\u5e94\u7528\u5728\u5c4f\u5e55\u5927\u5c0f\u4e0d\u540c\u7684\u786c\u4ef6\u4e0a\u6b63\u5e38\u663e\u793a\u7684\u95ee\u9898<\/p>\n\n\n\n<p>6.<strong>\u76f8\u5173\u4ee3\u7801\u53ca\u8bbe\u7f6e<\/strong><\/p>\n\n\n\n<p>\u5f15\u7528<\/p>\n\n\n\n<p>1) &nbsp; &nbsp; &nbsp; &nbsp; AndroidManifest.xml<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u53c2\u6570\u5728API Level 4\u4e5f\u5c31\u662fSDK 1.6\u4ee5\u540e\u624d\u542f\u7528\u7684\uff0c\u800c\u4e141.6\u7248\u672c\u7684API Level 4\u7684\u5e94\u7528\u9ed8\u8ba4\u503c\u5c31\u662fTrue<\/p>\n\n\n\n<p>2) \u8d44\u6e90\u76ee\u5f55\u540d\uff08android 2.0\u4ee5\u540e\uff09<br>res\/xxx-hdpi &nbsp; &nbsp; &nbsp; &nbsp; \u5f53density\u4e3a240\u65f6\uff0c\u4f7f\u7528\u6b64\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90<br>res\/xxx-mdpi &nbsp; &nbsp; &nbsp; &nbsp;\u5f53density\u4e3a160\u65f6\uff0c\u4f7f\u7528\u6b64\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90<br>res\/xxx-ldpi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u5f53density\u4e3a120\u65f6\uff0c\u4f7f\u7528\u6b64\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90<br>res\/xxx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u4e0d\u5e38\u540e\u7f00\uff0c\u4e3a\u9ed8\u8ba4\u8bbe\u7f6e\uff0c\u540cxxx-mdpi<\/p>\n\n\n\n<p>3) \u8d44\u6e90\u5355\u4f4d\uff08xml\u6587\u4ef6\u4e2d\u5b9a\u4e49\u5927\u5c0f\u7684\u5355\u4f4d\uff09<\/p>\n\n\n\n<p>a) &nbsp;dp=dip=dx (Density independent pixel)<br>\u57fa\u4e8e\u5c4f\u5e55\u5bc6\u5ea6\u7684\u62bd\u8c61\u5355\u4f4d\uff0c\u8bbe\u5907\u65e0\u5173\u7684\u70b9\uff0c\u7528\u4e8e\u8bf4\u660e\u4e0e\u5bc6\u5ea6\u65e0\u5173\u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\u3002\u8fd9\u4e9b\u5355\u4f4d\u662f\u76f8\u5bf9\u4e8e\u4e00\u4e2a160dpi\u7684\u5c4f\u5e55\uff0c\u6240\u6709\u4e00\u4e2adp\u662f160dpi\u5c4f\u5e55\u4e0a\u7684\u4e00\u4e2a\u70b9\u3002<\/p>\n\n\n\n<p>b) &nbsp;px<br>px\u6307\u8f6f\u4ef6\u7684\u5355\u4f4d\u70b9\uff0c\u8bbe\u5907\u76f8\u5173\u7684\u70b9<\/p>\n\n\n\n<p>c) &nbsp;\u5177\u4f53\u4f7f\u7528<\/p>\n\n\n\n<p>i.\u5e03\u5c40\u65f6\u5c3d\u91cf\u4f7f\u7528\u5355\u4f4ddip\uff0c\u5c11\u4f7f\u7528px<br>\u82e5\u4f7f\u7528px\uff0c\u5982\u679c\u8bbe\u67d0\u63a7\u4ef6\u5927\u5c0f400x400px\uff0c\u5728800&#215;480\u4e0a\u663e\u793a\u6b63\u5e38\uff0c\u800c\u5728533&#215;320\u7684\u5c4f\u4e0a\u5c31\u8d85\u51fa\u5c4f\u5e55\u4e86<br>\u82e5\u4f7f\u7528dp\uff0c\u5982\u679c\u8bbe\u67d0\u63a7\u4ef6\u5927\u5c0f\u4e3a160x160dp\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u4ece\u7cfb\u7edf\u4e2d\u53d6density\u6765\u7b97\u51fa\u771f\u6b63\u7684\u5927\u5c0f\uff0c\u6bd4\u5982\u5728800&#215;480\u5c4f\u7684density\u8bbe\u4e3a240\uff0c\u800c533&#215;320\u5c4f\u7684density\u8bbe\u4e3a160\uff0c\u501f\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97<br>pixels = dips * (density \/ 160)<br>\u5728800&#215;480\u5728\u5c4f\u4e2d\u663e\u793a240\u8c61\u7d20\uff0c\u800c\u5728533&#215;320\u7684\u5c4f\u4e2d\u663e\u793a\u4e3a160\u8c61\u7d20\uff0c\u63a7\u4ef6\u5728\u5c4f\u4e2d\u663e\u793a\u7684\u6bd4\u4f8b\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<p>ii. \u5185\u90e8\u7684\u5904\u7406\u8fc7\u7a0b\u5206\u4e3a\u4e09\u6b65\uff1a<br>\u53d6screen\u4e2d\u5176\u5b83\u5143\u7d20\uff0c\u8f6c\u4e3a\u5e94\u7528\u7684\u503c\uff0c\u8ba1\u7b97\u5e94\u7528\u5404\u63a7\u4ef6\u4f4d\u7f6e\uff0c\u7136\u540e\u518d\u8f6c\u6210800&#215;480\u4ee5\u4f9b\u771f\u6b63\u663e\u793a<br>\u82e5res-xxx\u4e0d\u5b58\u5728\uff0c\u5219\u8bfb\u53d6res\u6709\u7684\u8d44\u6e90\uff0c\u7136\u540e\u5bf9\u5176\u505a\u76f8\u5e94\u7f29\u653e<\/p>\n\n\n\n<p>7. <strong>\u5b9e\u73b0density\u7684\u5173\u952e\u6e90\u7801<\/strong><\/p>\n\n\n\n<p>1) \u00a0 \u00a0 \u00a0 \u00a0 BitmapFactory.java\uff08\u7528\u4e8e\u7f29\u653e\u56fe\u7247\uff09<\/p>\n\n\n\n<p>2) \u00a0 \u00a0 \u00a0 \u00a0 ComptibilityInfo.java\uff08\u7528\u4e8e\u4f4d\u7f6e\u8ba1\u7b97\uff09<\/p>\n\n\n\n<p>8.<strong>\u53d6\u5f97\u5c4f\u5e55\u4fe1\u606f\uff08\u5bbd\u9ad8\u7b49\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static String getDisplayMetrics(Context cx) { \u00a0\n\n\u00a0String str = \"\"; \u00a0\n\n\u00a0DisplayMetrics dm = new DisplayMetrics(); \u00a0\n\n\u00a0dm = cx.getApplicationContext().getResources().getDisplayMetrics(); \u00a0\n\n\u00a0int screenWidth = dm.widthPixels; \u00a0\n\n\u00a0int screenHeight = dm.heightPixels; \u00a0\n\n\u00a0float density = dm.density; \u00a0\n\n\u00a0float xdpi = dm.xdpi; \u00a0\n\n\u00a0float ydpi = dm.ydpi; \u00a0\n\n\u00a0str += \"The absolute width:\" + String.valueOf(screenWidth) + \"pixelsn\"; \u00a0\n\n\u00a0str += \"The absolute heightin:\" + String.valueOf(screenHeight) \u00a0\n\n\u00a0 \u00a0 \u00a0+ \"pixelsn\"; \u00a0\n\n\u00a0str += \"The logical density of the display.:\" + String.valueOf(density) \u00a0\n\n\u00a0 \u00a0 \u00a0+ \"n\"; \u00a0\n\n\u00a0str += \"X dimension :\" + String.valueOf(xdpi) + \"pixels per inchn\"; \u00a0\n\n\u00a0str += \"Y dimension :\" + String.valueOf(ydpi) + \"pixels per inchn\"; \u00a0\n\n\u00a0return str; \u00a0\n\n} \u00a0<\/pre>\n\n\n\n<p>9. <strong>\u53d6\u5f97\u72b6\u6001\u680f\u548c\u6807\u9898\u680f\u9ad8\u5ea6<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override \u00a0\n\nprotected void onCreate(Bundle savedInstanceState) { \u00a0\n\n\u00a0super.onCreate(savedInstanceState); \u00a0\n\n\u00a0setContentView(R.layout.layout_test2); \u00a0\n\n\u00a0tv1 = (TextView) findViewById(R.id.TextView01); \u00a0\n\n\u00a0tv1.post(new Runnable(){ \u00a0\n\n\u00a0 \u00a0public void run() \u00a0\n\n\u00a0 \u00a0{ \u00a0\n\n\u00a0 \u00a0 \u00a0processLayout(); \u00a0\n\n\u00a0 \u00a0} \u00a0\n\n\u00a0}); \u00a0\n\n} \u00a0\n\n\u00a0\n\nprivate void processLayout(){ \u00a0\n\n\u00a0 \u00a0\n\n\u00a0Rect rect= new Rect(); \u00a0\n\n\u00a0Window window= getWindow(); \u00a0\n\n\u00a0tv1.getWindowVisibleDisplayFrame(rect); \u00a0\n\n\u00a0\/\/\u72b6\u6001\u680f\u9ad8\u5ea6 \u00a0\n\n\u00a0int statusBarHeight= rect.top; \u00a0\n\n\u00a0int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); \u00a0\n\n\u00a0\/\/\u6807\u9898\u680f\u9ad8\u5ea6 \u00a0\n\n\u00a0int titleBarHeight= contentViewTop - statusBarHeight; \u00a0\n\n\u00a0\/\/\u6d4b\u8bd5\u7ed3\u679c\uff1aok\u4e4b\u540e 100\u591a ms \u624d\u8fd0\u884c\u4e86 \u00a0\n\n\u00a0Log.v(\"test\", \"=-init-= statusBarHeight=\"+statusBarHeight+ \u00a0\n\n\u00a0\" contentViewTop=\"+contentViewTop+ \u00a0\n\n\u00a0\" titleBarHeight=\"+titleBarHeight); \u00a0 \u00a0 \u00a0\n\n\u00a0\n\n} \u00a0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\uff1a \u83b7\u53d6asset\u4e0b\u9762\u7684\u6587\u4ef6 \u83b7\u5f97\u6587\u4ef6IO\u6d41\u3002 2: \u901a\u8fc7\u6d41\u521b\u5efaBitmap\u5bf9\u8c61\uff1a 3\uff1a\u901a\u8fc7\u6d41\u76f4\u63a5\u83b7\u53d6Drawab Bitmap Config \u53c2\u6570 Bitmap.Config ALPHA_8 Bitmap.Config ARGB_4444 Bitmap.Config ARGB_8888 Bitmap.Config RGB_565 A\u3000\u3000R\u3000\u3000G\u3000\u3000B \u900f\u660e\u5ea6\u3000\u7ea2\u8272\u3000\u7eff\u8272\u3000\u84dd\u8272 Bitmap.Config ARGB_4444 16 \u6bcf\u4e2a\u50cf\u7d20 \u5360\u56db\u4f4d Bitmap.Config ARGB_8888 32 \u6bcf\u4e2a\u50cf\u7d20 \u5360\u516b\u4f4d Bitmap.Config RGB_565 16 R\u53605\u4f4d G\u53606\u4f4d B\u53605\u4f4d \u6ca1\u6709\u900f\u660e\u5ea6\uff08A\uff09 \u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u662f\u7528argb888 \u4f46\u662f\u65e0\u53ef\u539a\u975e \u5b83\u4e5f\u76f8\u5bf9\u7684\u5f88\u5360\u5185\u5b58 \u56e0\u4e3a\u4e00\u4e2a\u50cf\u7d2032\u4f4d 8\u4f4d\u4e00\u4e2a\u5b57\u8282 \u5982\u679c\u662f800*480\u7684\u56fe\u7247\u7684\u8bdd\u81ea\u5df1\u7b97 \u4f30\u8ba1\u67091M\u591a\u4e86 4: \u8ba1\u7b97\u5b57\u4f53\u5bbd\u5ea6 \u5728canvas\u4e2d Paint p=new Paint(); p.setTextSize(24); Rect bounds = new Rect(); &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.xiunian.wang\/?p=628\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;android\u4e2d\u51e0\u4e2a\u57fa\u7840\u77e5\u8bc6&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":629,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=628"}],"version-history":[{"count":2,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":1799,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/628\/revisions\/1799"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}