Android仿ios加載loading菊花圖效果
項(xiàng)目中經(jīng)常會(huì)用到加載數(shù)據(jù)的loading顯示圖,除了設(shè)計(jì)根據(jù)app自身設(shè)計(jì)的動(dòng)畫loading,一般用的比較多的是仿照ios 的菊花加載loading 圖,當(dāng)然一些條件下還會(huì)涉及到加載成功/ 失敗情況的顯示,還有顯示文字。
使用ProgressBar 來加載動(dòng)畫轉(zhuǎn)圈,這里使用drawable文件 定義轉(zhuǎn)圈動(dòng)畫, indeterminateDrawable 屬性進(jìn)行加載。
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@mipmap/load" android:pivotX="50%" android:pivotY="50%" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="50dp" android:layout_height="50dp" android:indeterminateDrawable="@drawable/anim" />
部分情況下,在加載成功/ 失敗之后會(huì)顯示對(duì)應(yīng)的靜態(tài)圖片,所以一開始想直接通過 setIndeterminateDrawable(Drawable d)
來加載靜態(tài)圖片,但是直接寫是顯示不出圖片的,還要設(shè)置Drawable 的位置 d.setBounds(Rect bounds)
,即使這樣加載出了靜態(tài)圖片,但是設(shè)置 R.drawable.anim
的轉(zhuǎn)圈動(dòng)畫時(shí) 卻沒有了轉(zhuǎn)圈的效果,好氣喲 ~~
所以在自定義view 的布局里 成功/失敗的狀態(tài)單獨(dú)用 imageView 顯示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="110dp" android:layout_height="110dp" android:background="@drawable/shape_dialog_bg" android:gravity="center" android:orientation="vertical"> <ProgressBar android:id="@+id/progressBar" android:layout_width="50dp" android:layout_height="50dp" android:indeterminateDrawable="@drawable/anim" /> <ImageView android:id="@+id/iv" android:visibility="gone" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="正在加載" android:textColor="#fff" /> </LinearLayout>
自定義view,提供三種狀態(tài)的方法。
public class LoadingView extends LinearLayout { ...構(gòu)造函數(shù)... /** * loading */ public void showLoading() { iv.setVisibility(GONE); progressBar.setVisibility(VISIBLE); } /** * 成功 */ public void showSuccess() { iv.setImageResource(R.mipmap.load_success); iv.setVisibility(View.VISIBLE); progressBar.setVisibility(GONE); } /** *失敗 */ public void showFail() { iv.setImageResource(R.mipmap.load_fail); iv.setVisibility(View.VISIBLE); progressBar.setVisibility(GONE); } /** * 提示文字 * * @param txt string */ public void setText(String txt) { tv.setText(txt); } /** * 提示文字 */ public void setText(@StringRes int txtId) { tv.setText(txtId); } }
效果圖:
github地址: https://github.com/taixiang/loading
總結(jié)
以上所述是小編給大家介紹的Android仿ios加載loading菊花圖效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java操作Ant壓縮和解壓文件及批量打包Anroid應(yīng)用
這篇文章主要介紹了使用Java操作Ant壓縮和解壓文件以及批量打包Anroid應(yīng)用的教程,Ant是一個(gè)自動(dòng)化部署工具,用來處理zip和tar文件非常方便,需要的朋友可以參考下2016-02-02Android中利用Xposed框架實(shí)現(xiàn)攔截系統(tǒng)方法
這篇文章主要介紹了Android中利用Xposed框架實(shí)現(xiàn)攔截系統(tǒng)方法的相關(guān)資料,需要的朋友可以參考下2016-11-11Android實(shí)現(xiàn)動(dòng)態(tài)改變app圖標(biāo)的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)改變app圖標(biāo)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09Android檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
這篇文章主要為大家詳細(xì)介紹了Android檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android自定義View實(shí)現(xiàn)繪制虛線的方法詳解
這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)繪制虛線的方法,在繪制過程中走了一些彎路才實(shí)現(xiàn)了虛線的效果,所以想著總結(jié)分享出來,方便有需要的朋友和自己在需要的時(shí)候參考學(xué)習(xí),下面來一起看看吧。2017-04-04Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫效果,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android xml布局及RotateAnimation動(dòng)畫類相關(guān)操作技巧,需要的朋友可以參考下2018-02-02