PS:最近閑着無聊..模仿去寫個QQapp..效果還不錯..並且從中又學習到了一些相關的東西,在這里進行一些相關總結..
學習內容:
Android 中 Drawable 文件夾內部相關屬性..
Android項目的項目結構我們算是再熟悉不過了..但是僅僅知道有什么結構遠遠是不夠的,能夠熟練的去運用其中的內部屬性才是重要的..項目結構就不得不說一下Drawable文件中的相關屬性了..言歸正傳..首先說一下anim..
1.anim.xml
anim.xml..通過這個xml文件,我們可以定義一些動畫效果..然后通過引用我們就可以為一個View設置一個動畫效果了..我們來看一下內部屬性以及結構..
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate/> <alpha/> <scale/> <rotate/> </set>
這是anim的內部結構..內部包含着四個標簽..各個標簽有各個標簽的作用..我們來看一下這幾個標簽的作用..
首先是alpha屬性..
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!--透明度動畫設置 fromAlpha:表示初始的透明度 toAlpha:表示效果結束的最終透明度、 duration:表示動畫的持續時間--> <!--淡入效果--> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300"/> </set>
alpha主要的目的是實現一個View視圖的淡入淡出效果..
接着來看translate屬性..
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 位移動畫的使用 fromXDelta:初始從X的什么位置進行移動 fromYDelta:初始從Y的什么位置進行移動 --> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="100%" android:toYDelta="0%" android:duration="300"/> </set>
這里不得不說的一個知識點就是,toYDelta="0%"..這是一個百分值..

這個100%的由來是根據上面這張圖片來的..我們可以看到Android的手機屏幕..手機屏幕的左上角是坐標的(0,0)..最右下角是手機屏幕的最大坐標..我們的手機屏幕大小是按照(設備獨立像素,其實就是dp)進行分割的..這里把手機屏幕按照百分比進行分割..也非常的好理解..上面的位移動畫表示X的位置是不動的..即X方向上不發生任何的平移效果..豎直方向View從最底部平移到最上部..這樣就可以完成一個View豎直方向上的平移動畫效果..總體理解起來還是非常的簡單的..
scale縮放動畫效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!--縮放動畫效果 fromXScale:這個from表示的是圖片的大小 0.0表示不顯示數據..1.0表示放大至原圖大小.. pivox:表示動畫的位置..也是按照屏幕的百分比來看的 interpolator:表示加速器..當前動畫的播放速度--> <scale android:fromXScale="1" android:toXScale="1.0" android:fromYScale="0" android:toYScale="1.0" android:pivotX="100%" android:pivotY="0%" android:interpolator="@android:anim/decelerate_interpolator" android:duration="300"/> </set>
在這個縮放動畫中放入了一個加速器..加速器在哪個動畫中都可以使用..目的是控制當前的動畫效果以怎樣的速度來完成這個動畫效果..加速器的屬性有很多..在這里列舉一下...
android:interpolator="@android:anim/accelerate_interpolator" 越來越快
android:interpolator="@android:anim/decelerate_interpolator" 越來越慢
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 先快后慢
android:interpolator="@android:anim/anticipate_interpolator" 先后退一小步然后向前加速
android:interpolator="@android:anim/overshoot_interpolator" 快速到達終點超出一小步然后回到終點
android:interpolator="@android:anim/anticipate_overshoot_interpolator" 到達終點超出一小步然后回到終點
android:interpolator="@android:anim/bounce_interpolator" 到達終點產生彈球效果,彈幾下回到終點
android:interpolator="@android:anim/linear_interpolator" 均勻速度
上面這些屬性是加速器的所有屬性..相關的效果大家可以自己去試試...
rotate旋轉動畫效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!--旋轉動畫效果 fromDegrees:旋轉的角度 toDegrees:需要到達的角度
--> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="300" android:toDegrees="-360" android:pivotX="10%" android:pivotY="100%" android:duration="1000" /> </set>
旋轉動畫的實現其實和上面幾種也都差不多..這里只是單一的在一個set標簽中設置一個效果,一個set標簽內部可以放入多種效果..從而實現多重動畫的組合..使得動畫展示的更加的完美...
2.style.xml
style用於定義統一樣式...如果一個layout內部多個控件的布局樣式是一樣的,那么我們就沒有必要一個個的去書寫相關布局..只需要定義一個統一的樣式..然后這多個View去引用這個style樣式其實就可以了..可以免去很多的重復代碼的書寫..因此style就變得非常的重要了...
<style>標簽的基本結構:
style 標簽內部屬性不僅可以添加基本屬性,而且還可以添加drawable,anim的xml文件的屬性數據..
<style name="layout_style"> <item name="android:layout_height">30dp</item> <item name="android:layout_width">0dp</item> <item name="android:layout_weight">1</item> <item name="android:textSize">15sp</item> <item name="android:backgrond">@drawable/layout_style</item> <item name="android:windowEnterAnimation">@anim/pop_enter</item> <item name="android:windowExitAnimation">@anim/pop_exit</item> </style>
style屬性如同網頁中的css樣式一樣..只不過沒有css那樣那么的復雜..定義了統一樣式之后我們就可以去引用了...引用的形式也非常的簡單..只需要通過style屬性去引用相關的style.xml文件就可以了...
<Button android:id="@+id/constact_all
style="@style/top_all" android:text="@string/all"/>
3.selector標簽...
我們在為一個View定義基本樣式的時候是通過布局文件來實現的..有時候控件的基本樣式我們定義好了..還有一些其他樣式是我們需要使用@drawable屬性來完成的..比如說background屬性,src等屬性我們是需要通過調用drawable中對應的xml文件來完成的...
這就涉及到了selector標簽的使用...selector用於設置一個控件或者是一個View中一些屬性...拿一個按鈕來說吧..一個按鈕按下的時候需要定義一種樣式..沒有按下的時候需要另一種樣式..這樣的目的是為了給用戶一種良好的反饋..這樣就需要使用到drawable文件夾中的xml文件中的selector標簽了..這就是selector的應用..
表示一個圖片是否響應點擊事件..點擊后需要顯示的效果..以及不被點擊時需要顯示的效果..還有是否響應觸發事件對應的相應效果..
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="false" android:drawable="@drawable/skin_tab_icon_contact_selected"></item> <item android:state_pressed="true" android:drawable="@drawable/skin_tab_icon_contact_selected"></item> <item android:drawable="@drawable/skin_tab_icon_contact_normal"></item> </selector>
相關屬性還有很多...在這里簡單的列舉一下..並且這些相關屬性是可以進行疊加的..從而處理多個事件的集合...
android:state_pressed 如果是true,當被點擊時顯示該圖片,如果是false沒被按下時顯示默認。
android:state_focused 如果是true,獲得焦點時顯示;如果是false沒獲得焦點顯示默認。
android:state_selected 如果是true,當被選擇時顯示該圖片;是false未被選擇時顯示該圖片。
android:state_checkable 如果值為true,當CheckBox能使用時顯示該圖片;false,當CheckBox不能使用時顯示該圖片。
android:state_checked 如果值為true,當CheckBox選中時顯示該圖片;false,當CheckBox為選中時顯示該圖片。
android:state_enabled 如果值為true,當該組件能使用時顯示該圖片;false,當該組件不能使用時顯示該圖片。
android:state_window_focused 如果值為true,當此activity獲得焦點在最前面時顯示該圖片;false,當沒在最前面時顯示該圖片。
selector這里的屬性中的drawable屬性一方面可以去使用我們放置在內部的圖片..另一種方式就是還可以去自定義圖形去顯示...這樣通過了自定義圖形的方式來設置drawable屬性...
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="false"> <shape > <corners android:topLeftRadius="0dp" android:topRightRadius="5dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp"/> <solid android:color="#499BF7"/> <stroke android:width="1dp" android:color="#499BF7"/> </shape> </item> <item android:state_pressed="true"> <shape > <corners android:topLeftRadius="0dp" android:topRightRadius="5dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp"/> <solid android:color="#499BF7"/> <stroke android:width="1dp" android:color="#499BF7"/> </shape> </item> <item > <shape > <corners android:topLeftRadius="0dp" android:topRightRadius="5dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp"/> <solid android:color="#FFFFFF"/> <stroke android:width="1dp" android:color="#499BF7"/> </shape> </item> </selector>
同樣shape標簽也可以進行單獨的使用..直接為drawable屬性設置shape屬性也是可以的..如同這樣...
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#FFFFFF"/> <stroke android:width="1dp" android:color="#499BF7"/> </shape>
drawable文件內部看着非常的簡單,但是使用的非常靈活,熟練還是沒那么容易的...因此掌握其中內部的技巧..熟練的去運用..這樣在開發當中可以省去很多的事情..
