轉載聲明:Ryan的博客文章歡迎您的轉載,但在轉載的同時,請註明文章的來源出處,不勝感激! :-) html
http://my.oschina.net/ryanhoo/blog/86874
android
譯者:Ryan Hoo git
來源:http://blog.stylingandroid.com/archives/378# github
譯者按:在外企工做的半年多中花了很多時間在國外的網站上搜尋資料,其中有一些至關有含金量的文章,我會陸陸續續翻譯成中文,與你們共享之。初次翻譯,「信達雅」三境界恐怕只到信的層次,望你們見諒! 工具
------------------------------------------------------------------------------------- post
譯文: 字體
一般一些像Photoshop這樣的工具能夠用來建立各類各樣的文字效果,而且咱們常常用到的一種效果就是陰影。Android是支持字體陰影的,在這片文章中,咱們將會探討各類建立陰影的方式。在TextView中實現字體陰影效果比在位圖元素中的效率更高,而且使得設計可適配多種屏幕尺寸。相同條件下,Android的LayoutManager縮放TextView控件可比在ImageView中縮放位圖要簡單多了。 網站
字體陰影須要四個相關參數:
1. android:shadowColor:陰影的顏色
2. android:shadowDx:水平方向上的偏移量
3. android:shadowDy:垂直方向上的偏移量
4. android:shadowRadius:陰影的範圍
字體陰影是一種誤稱,由於實際上咱們能夠實現一些與陰影看起來絕不相關的效果,這個咱們將在後面看到。不管如何,讓咱們從一個簡單的相似陰影的效果開始: google
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/White" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:textColor="@color/Black" android:layout_width="wrap_content" android:text="Simple Shadow" android:layout_height="wrap_content" android:padding="2dp" android:shadowColor="@color/TransparentGrey" android:shadowDx="3" android:shadowDy="3" android:shadowRadius="0.01" /> </LinearLayout>
這裏有一些定義了顏色的支持文件,咱們將在本文中用到它們。它們是 res/values/colours.xml: spa
以及res/drawables/gradient.xml:
若是運行程序,咱們將會看到一個簡單的投影效果。
這裏只有一個方向的陰影而且有可能引發一些問題:陰影的範圍必須始終存在,而且不能設置爲'0',不然陰影是不會被繪製出來的。在這個例子中,我想要硬邊緣的陰影效果,因此我將shadowRadius設置爲一個較小的數字以達到此效果。
好了,這個例子實現了一個灰色的陰影,水平垂直方向上偏移3個像素(彷佛應該是dp?可是不是3dp。),而且用一個很是很是小的陰影半徑實現了硬邊緣的陰影。
咱們能夠經過增長陰影半徑來軟化陰影:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/White" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:textColor="@color/Black" android:layout_width="wrap_content" android:text="Soft Shadow" android:layout_height="wrap_content" android:padding="2dp" android:shadowColor="@color/TransparentGrey" android:shadowDx="3" android:shadowDy="3" android:shadowRadius="1.5" /> </LinearLayout>
這個效果以下:
還有一件事情是,咱們能夠經過改變偏移量來有效的改變光源的方向:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/White" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:textColor="@color/Black" android:layout_width="wrap_content" android:text="Soft Shadow (Below)" android:layout_height="wrap_content" android:padding="2dp" android:shadowColor="@color/TransparentGrey" android:shadowDx="3" android:shadowDy="-3" android:shadowRadius="1.5" /> </LinearLayout>
效果以下:
效果如圖:
經過簡單的改變陰影的顏色,咱們也能夠改變發光的效果。
附錄:+Kirill Grouchnikov 這篇文章中的方法有一個優勢是:Android中爲添加文本陰影並不會使文本邊框增大。
文章中全部的例子都是android:padding="2dp",設置其餘的陰影或許會出現陰影被裁減的現象(譯者注:陰影偏移量過多,超出文本框範圍的現象。所以偏移量要適中。:-))。一個具備大範圍的陰影,或者較大的偏移量須要更多的padding以防止這種現象。(譯者注:意爲增長TextView的padding)
這篇文章的全部源碼均可以在這裏找到。
©Mark Allison. All rights reserved.這篇文章最先出如今 Styling Android。
此頁的某些部分是基於Google的創造和共享內容,並使用知識共享許可協議3.0版(CreativeCommons 3.0 Attribution License)。