พื้นฐาน android - ความหนาแน่น (Density) ของหน้าจอบน Android App




รู้จักกับ Screens density ที่เราจะเจอกันบน Hanset หรือ Tablet ก่อน

-mdpi       Resources for medium-density screens (~160dpi). (This is the baseline density.)
-hdpi        Resources for high-density screens (~240dpi).
-xhdpi      Resources for extra-high-density screens (~320dpi).
-xxhdpi    Resources for extra-extra-high-density screens (~480dpi).
-xxxhdpi  Resources for extra-extra-extra-high-density uses (~640dpi).


วิธีแรก - Provide alternative bitmaps

วิธีนี้ข้อดีจะทำให้เราเปลี่ยนรูปภาพ (แต่ใช้ชื่อเดิม) ในแต่ละหน้าจอได้
อย่างเช่นการใช้ภาพที่รายละเอียดเยอะสำหรับ Device ที่มี Density สูงๆ
และภาพรายละเอียดน้อยๆสำหรับ Device ที่มี Density ต่ำ

Example

drawable มีหลายประเภท (ในบล็อคผมมีตัวอย่างอยู่หลายตัว)
หรืออยากอ่านรายละเอียดลึกๆ > Drawable resources

res/
  drawable-xxxhdpi/image.png 
  drawable-xxhdpi/image.png
  drawable-xhdpi/image.png
  drawable-hdpi/image.png
  drawable-mdpi/image.png


โฟลเดอร์ mipmap ใช้สำหรับวางไอคอนแอพ

res/
  mipmap-xxxhdpi/icon.png
  mipmap-xxhdpi/icon.png
  mipmap-xhdpi/icon.png
  mipmap-hdpi/icon.png
  mipmap-mdpi/icon.png



วิธีที่ 2 - Use vector graphics
(vector เหมาะเฉพาะไอคอนไม่ใช่ภาพถ่าย)
กราฟิกแบบเวกเตอร์มักจะให้เป็น SVG (Scalable Vector Graphics) ไฟล์ แต่ Android ไม่สนับสนุนรูปแบบนี้ คุณสามารถแปลง SVG เป็นเวกเตอร์ที่สามารถวาดได้จาก Android Studio โดยใช้ Vector Asset Studio





ไฟล์นี้จะอยู่ในไดเรกทอรี drawables เริ่มต้น
(ไม่จำเป็นต้องใช้ไดเรคทอรีเฉพาะของความหนาแน่น):


res / 
  drawable / 
    ic_android_launcher.xml



ท่าไม้ตาย!! 
อ่านบทความ Plugin : Android Drawable Importer เพื่อชีวิตที่สดใส :)



Ref -
https://developer.android.com/training/multiscreen/screendensities
http://www.akexorcist.com/2013/03/android-design-size-density.html