系統(tǒng)定義了幾個(gè)標(biāo)準(zhǔn)的DPI值,作為手機(jī)的固定DPI
密度 | ldpi | mdpi | hdpi | xhdpi | xxhdpi |
---|---|---|---|---|---|
密度值 | 120 | 160 | 240 | 320 | 480 |
分辨率 | 240×320 | 320×480 | 480×800 | 720×1280 | 1080×1920 |
注:此表僅供參考,如小米4c手機(jī),分辨率1920×1080,尺寸5英寸,屏幕像素密度441ppi。因在具體應(yīng)用中1dp = 3px,故密度為xxhdpi
由于各種屏幕密度的不同,導(dǎo)致同樣像素大小的長度,在不同密度的屏幕上顯示長度不同 Android系統(tǒng)使用mdpi的屏幕作為標(biāo)準(zhǔn),在這個(gè)屏幕上1px = 1dp 例如同樣是100dp的長度,在mdpi中為100px,而在hdpi中為150px 在hdpi中1dp = 1.5px,在xhdpi中1dp = 2px,在xxhdpi中1dp = 3px
可將dp、sp轉(zhuǎn)換為px的工具類保存到項(xiàng)目中
系統(tǒng)通過提供的Canvas對(duì)象來提供繪圖方法,如drawPoint
(點(diǎn))、drawLine
(直線)、drawLines
(多條直線)、drawRect
(矩形)、drawRoundRect
(圓角矩形)、drawCircle
(圓)、drawVertices
(多邊形)、drawArc
(弧形或扇形)、drawOval
(橢圓)、drawText
(文本)、drawPosText
(指定位置文本)、drawPath
(路徑)等等,例
例子:通過漸變實(shí)現(xiàn)陰影效果
<?xml version="1.0" encoding="uft-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" // 默認(rèn)為rectangle android:shape="rectangle"> <corners // 當(dāng)shape="rectangle"時(shí)使用 // 半徑,會(huì)被topLeftRadius等單個(gè)半徑屬性覆蓋,默認(rèn)為1dp android:radius="8dp"/> <gradient // 漸變 android:startColor="#FF5DA2FF" android:endColor="#805FBBFF" android:angle="45"/> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"/></shape>Selector的作用在于幫助開發(fā)者實(shí)現(xiàn)靜態(tài)繪圖中的事件反饋,通過給不同的事件設(shè)置不同的圖像,從而在程序中根據(jù)用戶輸入,返回不同的效果
<?xml version="1.0" encoding="uft-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默認(rèn)時(shí)的背景圖片 --> <item android:drawable="@drawable/X1" /> <!-- 沒有焦點(diǎn)時(shí)的背景圖片 --> <item android:state_window_focused="false" android:drawable="@drawable/X2" /> <!-- 非觸摸模式下獲得焦點(diǎn)并單擊時(shí)的背景圖片 --> <item android:state_focused="true" android:state_4. Android繪圖技巧Canvas.save()
:保存畫布,將之前的所有已繪制圖像保存起來,讓后續(xù)的操作就好像在一個(gè)新的圖層上操作一樣Canvas.restore()
:合并圖層操作,將在save()
之后繪制的所有圖像與save()
之前的圖像進(jìn)行合并Canvas.translate()
:畫布平移,即坐標(biāo)系平移。在調(diào)用translate(x, y)
方法之后,則將原點(diǎn)(0, 0)移動(dòng)到了(x, y),之后的所有繪圖操作都將以(x, y)為原點(diǎn)執(zhí)行Canvas.rotate()
:畫布翻轉(zhuǎn),即坐標(biāo)系翻轉(zhuǎn),將坐標(biāo)系旋轉(zhuǎn)一定的角度Android通過調(diào)用saveLayer()
方法、saveLayerAlpha()
方法將一個(gè)圖層入棧,使用restore()
方法、restoreToCount()
方法將一個(gè)圖層出棧。入棧的時(shí)候,后面所有的操作都發(fā)生在這個(gè)圖層上,而出棧的時(shí)候,則會(huì)把圖像繪制到上層Canvas上。
(待續(xù)……)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注