Android自定義View實(shí)現(xiàn)loading動(dòng)畫加載效果
項(xiàng)目開發(fā)中對(duì)Loading的處理是比較常見的,安卓系統(tǒng)提供的不太美觀,引入第三發(fā)又太麻煩,這時(shí)候自己定義View來實(shí)現(xiàn)這個(gè)效果,并且進(jìn)行封裝抽取給項(xiàng)目提供統(tǒng)一的loading樣式是最好的解決方式了。
先自定義一個(gè)View,繼承自LinearLayout,在Layout中,添加布局控件
/**
* Created by xiedong on 2017/3/7.
*/
public class Loading_view extends LinearLayout {
private Context mContext;
private RelativeLayout loading_content;
private ImageView img;
private TextView loadingText;
private AnimationDrawable animationDrawable;
public Loading_view(Context context) {
super(context);
mContext = context;
setupView();
}
public Loading_view(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setupView();
}
public Loading_view(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
setupView();
}
private void setupView() {
// View view= LayoutInflater.from(mContext).inflate(R.layout.loading_view_layout,this); //一定要把布局添加進(jìn)容器,不能為null
View.inflate(mContext, R.layout.loading_view_layout, this);
loading_content = (RelativeLayout) findViewById(R.id.loading_content);
img = (ImageView) findViewById(R.id.img);
loadingText = (TextView) findViewById(R.id.text);
img.setImageResource(R.drawable.anim_loading);
animationDrawable = ((AnimationDrawable) img.getDrawable());
animationDrawable.start();
}
public void setMessage(String msg) {
loadingText.setText(msg);
}
}
自定義View的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90000000"
android:gravity="center">
<RelativeLayout
android:id="@+id/loading_content"
android:layout_width="150dp"
android:layout_height="120dp"
android:background="#40ffffff">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:layout_centerHorizontal="true"
android:text="加載中..."
/>
</RelativeLayout>
</LinearLayout>
這里使用AnimationDrawable的方式來實(shí)現(xiàn)動(dòng)畫效果,AnimationDrawable的list文件如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/loading1"
android:duration="100" />
<item
android:drawable="@drawable/loading2"
android:duration="100" />
<item
android:drawable="@drawable/loading3"
android:duration="100" />
<item
android:drawable="@drawable/loading10"
android:duration="100" />
</animation-list>
自定義View部分的工作完成之后,接下來就是如何在項(xiàng)目中具體運(yùn)用。在相應(yīng)的布局中使用的時(shí)候,一定要記得把此布局文件add進(jìn)ViewGroup中,因?yàn)樵撟远x的View跟調(diào)用他的View是獨(dú)立的兩個(gè)View,沒有完成add的話,loading布局可能不會(huì)顯示出來。
private Loading_view loading_view;
loading_view = new Loading_view(this); //實(shí)例化自定義VIew
loading_view.setMessage("loading文字提示內(nèi)容....");
//添加當(dāng)前自定義View進(jìn)主布局文件
addContentView(loading_view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
以上所述是小編給大家介紹的Android自定義View實(shí)現(xiàn)loading動(dòng)畫加載效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android 自定義View的使用介紹
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
- Android自定義View設(shè)定到FrameLayout布局中實(shí)現(xiàn)多組件顯示的方法 分享
- Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果
- Android自定義View實(shí)現(xiàn)繪制虛線的方法詳解
- Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android 使用Kotlin自定義View的方法教程
- Android?自定義view中根據(jù)狀態(tài)修改drawable圖片
相關(guān)文章
Android之線程池ThreadPoolExecutor的簡介
今天小編就為大家分享一篇關(guān)于Android之線程池ThreadPoolExecutor的簡介,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
AndroidStudio3 支持 Java8 了請(qǐng)問你敢用嗎
Google 發(fā)布了 AS 3.0,以及一系列的 Support 包,有意思的新東西挺多,AS3里面有一個(gè)亮眼的特性就是支持J8。接下來通過本文給大家分享AndroidStudio3 支持 Java8 的相關(guān)內(nèi)容,感興趣的朋友一起看看吧2017-11-11
Android音頻開發(fā)之SurfaceView的使用詳解
這篇文章主要為大家介紹了Android中SurfaceView的使用方法,本文通過簡要的案例,為大家進(jìn)行了詳細(xì)的講解,需要的朋友可以參考一下2022-04-04
Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼
這篇文章主要介紹了Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作
這篇文章主要介紹了Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android中oncreate中獲得控件高度或?qū)挾鹊膶?shí)現(xiàn)方法
這篇文章主要介紹了Android中oncreate中獲得控件高度或?qū)挾鹊膶?shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能,涉及Android基于ImageView的攝像頭拍攝圖片加載、保存及權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android Animation實(shí)戰(zhàn)之一個(gè)APP的ListView的動(dòng)畫效果
這篇文章主要介紹了Android Animation實(shí)戰(zhàn)項(xiàng)目,為大家分享了一個(gè)APP的ListView的動(dòng)畫效果,感興趣的小伙伴們可以參考一下2016-01-01

