Android實(shí)現(xiàn)波浪線效果(xml bitmap)
我們要實(shí)現(xiàn)的效果如下:
在這之前先帶大家了解一下xml bitmap,何為XML Bitmap?
XML Bitmap 是一個(gè)用XML定義的文件放在資源目錄,定義的對(duì)象是圖片,為bitmap定義別名,這個(gè)文件可以給bitmap定義一些額外的屬性。例如:抖動(dòng)。
1、文件存放位置
res/drawable/filename.xml
2、語法
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@[package:]drawable/drawable_resource" android:antialias=["true" | "false"] android:dither=["true" | "false"] android:filter=["true" | "false"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
例子:
<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" tools:context=".MainActivity" > <!-- 與默認(rèn)情況(@drawable/btn_default_pressed_holo_light)有差別 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/bm" android:text="sssssssssssssssssss" /> </RelativeLayout> <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" tools:context=".MainActivity" > <!-- 與默認(rèn)情況(@drawable/btn_default_pressed_holo_light)有差別 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/bm" android:text="sssssssssssssssssss" /> </RelativeLayout>
效果圖:
默認(rèn)(@drawable/btn_default_pressed_holo_light):
引用(\@drawable/bm):
從截圖上就可以看出來差別。
titileMode="repeat":
之后找了一些關(guān)于實(shí)現(xiàn)波浪線的方法,總感覺不大滿意,常見的方法分享給大家:
1、直接搞一個(gè)這樣的波浪線的切圖
這種方式最簡單,但是劣勢也非常明顯,如果view的寬度過大,則會(huì)出現(xiàn)圖片變形,如下圖所示:
如果過小則如下:
要求不高的話,這樣勉強(qiáng)可以蒙混過關(guān),但是追求完美的話,這樣的效果顯然很不給力
2、自定義控件繪制
這個(gè)方法的確可以達(dá)到不錯(cuò)的效果,但是實(shí)現(xiàn)比較麻煩。我也看到有朋友搞過,
3、用xml的bitmap標(biāo)簽實(shí)現(xiàn)波浪線效果
首頁要搞一個(gè)周期的波浪線,即包括波峰和波谷,如下圖所示:
wave_item:
然后水平方向上平鋪,從而實(shí)現(xiàn)波浪線效果,代碼如下:
wave.xml:
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:antialias="true" android:src="@drawable/wave_item" android:tileMode="repeat" />
activity_main.xml
<LinearLayout 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:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/wave" /> </LinearLayout>
注意:這個(gè)ImageView通過background顯示圖片,src是顯示不了這樣的效果的。
希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
Android學(xué)習(xí)筆記——Menu介紹(一)
Android3.0(API level 11)開始,Android設(shè)備不再需要專門的菜單鍵。隨著這種變化,Android app應(yīng)該取消對(duì)傳統(tǒng)6項(xiàng)菜單的依賴。取而代之的是提供anction bar來提供基本的用戶功能2014-10-10Android中獲得正在運(yùn)行的程序和系統(tǒng)服務(wù)的方法
這篇文章主要介紹了Android中獲得正在運(yùn)行的程序和系統(tǒng)服務(wù)的方法,分別是對(duì)ActivityManager.RunningAppProcessInfo類和ActivityManager.RunningServiceInfo類的使用,需要的朋友可以參考下2016-02-02Android ScrollView顯示到底部或任意位置實(shí)現(xiàn)代碼
這篇文章主要介紹了 Android ScrollView顯示到底部或任意位置實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android Flutter實(shí)現(xiàn)視頻上滑翻頁效果的示例代碼
我們?cè)诙桃曨l應(yīng)用中經(jīng)常會(huì)看到不停上滑瀏覽下一條視頻的沉浸式交互效果,這種交互能夠讓用戶不停地翻頁,直到找到喜歡的視頻內(nèi)容。本文將通過Flutter中的PageView組件實(shí)現(xiàn),感興趣的可以了解一下2022-10-10Android 系統(tǒng)實(shí)現(xiàn)多種開機(jī)動(dòng)畫和logo切換功能
這篇文章主要介紹了android 系統(tǒng)實(shí)現(xiàn)多種開機(jī)動(dòng)畫和logo切換功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12Android程序報(bào)錯(cuò)程序包org.apache.http不存在問題的解決方法
這篇文章主要介紹了Android程序報(bào)錯(cuò)"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片
這篇文章主要為大家詳細(xì)介紹了android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android 新手引導(dǎo)蒙層效果實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android 新手引導(dǎo)蒙層效果實(shí)現(xiàn)代碼示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01