【原創】安卓開發中,不管是背景圖仍是按鈕圖或者其餘圖,都不但願隨着移動設備的改變而失真,若是想要實現背景圖片應用於各類尺寸的屏幕且不失真,須要先對圖片進行一下處理。 android
在這裏,我使用了安卓sdk的tool中自帶的工具draw 9-patch對圖片進行簡單處理。 工具
本次處理的圖片的原圖是 spa
draw 9-patch路徑以下: xml
雙擊點開以後,稍等會出現如下頁面: 圖片
點擊file引入以上原圖,這是頁面會被分爲左右兩側,左側是可操做區域,右側是預覽區域,預覽中有三張圖片,第一張是豎向拉伸的圖片,第二張是橫向拉伸的圖片,第三張是全方位拉伸的圖片,咱們使用9-patch作圖片的目的,就是讓圖片的邊緣可被拉伸,而圖片的重點區域不可被拉伸,這樣能夠起到不失真的效果。在左側經過鼠標框選區域,能夠設置圖片的某些部位可被拉伸,框選後在圖片邊緣可見黑線,黑線就表明此區域可被拉伸,例子以下: 開發
這樣一來,縱向拉伸此圖片則變成: string
橫向拉伸圖片爲: it
所有拉伸爲: file
這時,就實現了保留重點區域圖片,拉伸其邊緣的目的,點擊file,能夠保存圖片,圖片保存後,格式爲demo1.9.png,將圖片放在安卓項目的drawable包下,就能夠正常引用了,例如: sdk
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/demo1" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 大功告成!