วันพฤหัสบดีที่ 3 มกราคม พ.ศ. 2556

Application Resource ใน Android ?

Resource ต่าง ๆ ใน Android Application เช่น Image, Audio, และรวมถึงทุกอย่างที่เกี่ยวกับการแสดงผลบน User Interface (เช่น Animation, Menu, Layout เป็นต้น) จะถูกจัดเก็บแยกออกมาจาก Source Code ทั้งนี้ก็เพื่อให้สามารถจัดการกับความหลากหลายของคุณสมบัติ (Feature) และความสามารถ (Capability) บนหลากหลาย Device ได้ง่าย


โดยวิธีการจัดการกับความหลากหลายของคุณสมบัติ (Feature) และความสามารถ (Capability) บนหลากหลาย Device ก็คือ การจัดเตรียม Alternative Resource ไว้ โดยนักพัฒนาจะต้องระบุว่า Resource Folder ที่ได้สร้างขึ้นจะถูกใช้กับ Device ที่มีคุณสมบัติ (Feature) และความสามารถ (Capability) อย่างไร เช่น มีการระบุใน Resource Folder ว่า Resource นั้นจะถูกใช้เมื่อ Device ได้กำหนดภาษาที่ใช้เป็นภาษาไทยหรือภาษาอังกฤษ, มีการระบุใน Resource Folder ว่า Resource นั้นจะถูกใช้เมื่อ Device ได้แสดงผลในลักษณะแนวนอนหรือแนวตั้ง เป็นต้น




ซึ่งวิธีการระบุว่า Resource Folder ที่ได้สร้างขึ้นจะถูกใช้กับ Device ที่มีคุณสมบัติ (Feature) และความสามารถ (Capability) อะไร ทำได้โดยการระบุ Qualifier บน Resource Folder นั้น เช่น ถ้าต้องการให้ Resource Folder นั้น ถูกใช้งานเมื่อ Device ถูกกำหนดค่าเป็นภาษาไทย ก็ให้กำหนด Qualifier คำว่า "th" ต่อท้าย Resource Folder นั้น เป็นต้น


ทั้งนี้ในทุก ๆ File ที่อยู่ใน Resource Folder ที่ใช้ในการสร้าง Android Application นั้น Android SDK จะทำการสร้าง Unique ID ให้โดยอัตโนมัติ ซึ่งนักพัฒนาสามารถอ้างอิงถึง Resource File เหล่านั้น ภายใน Source Code ได้ เช่น ถ้านักพัฒนาเก็บ Image File ชื่อ logo.png ไว้ภายใน Resource Folder ชื่อ "res/drawable/" แล้ว Android SDK จะทำการสร้าง Unique ID ชื่อ R.drawable.logo ให้ เพื่อใช้อ้างถึง Resource File นั้นภายใน Source Code ได้ เป็นต้น


Resource Types ใน Android มีดังนี้


1. Animation Resources
Define pre-determined animations.
Tween animations are saved in res/anim/ and accessed from the R.anim class.
Frame animations are saved in res/drawable/ and accessed from the R.drawable class.


2. Color State List Resource
Define a color resources that changes based on the View state.
Saved in res/color/ and accessed from the R.color class.


3. Drawable Resources
Define various graphics with bitmaps or XML.
Saved in res/drawable/ and accessed from the R.drawable class.


4. Layout Resource
Define the layout for your application UI.
Saved in res/layout/ and accessed from the R.layout class.


5. Menu Resource
Define the contents of your application menus.
Saved in res/menu/ and accessed from the R.menu class.


6. String Resources
Define strings, string arrays, and plurals (and include string formatting and styling).
Saved in res/values/ and accessed from the R.string, R.array, and R.plurals classes.


7. Style Resource
Define the look and format for UI elements.
Saved in res/values/ and accessed from the R.style class.


8. More Resource Types
Define values such as booleans, integers, dimensions, colors, and other arrays.
Saved in res/values/ but each accessed from unique R sub-classes (such as R.bool, R.integer, R.dimen, etc.).
ที่มา : http://www.softmelt.com

0 ความคิดเห็น:

แสดงความคิดเห็น