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的ListView多選刪除操作實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android的ListView多選刪除操作實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉頂部圖片變大,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件
這篇文章主要為大家詳細(xì)介紹了可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件,結(jié)合WheelView實(shí)現(xiàn)滾輪選擇日期操作,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)絡(luò)直播彈幕功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
淺談Android開發(fā)中ListView控件性能的一些優(yōu)化方法
這篇文章主要介紹了Android開發(fā)中ListView控件性能的一些優(yōu)化方法,需要的朋友可以參考下2016-01-01
21天學(xué)習(xí)android開發(fā)教程之SurfaceView
21天學(xué)習(xí)android開發(fā)教程之SurfaceView,SurfaceView由于可以直接從內(nèi)存或者DMA等硬件接口取得圖像數(shù)據(jù),因此是個(gè)非常重要的繪圖容器,操作相對(duì)簡單,感興趣的小伙伴們可以參考一下2016-02-02
Android Activity啟動(dòng)模式之standard實(shí)例詳解
這篇文章主要介紹了Android Activity啟動(dòng)模式之standard,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中活動(dòng)(Activity)四種啟動(dòng)模式中的standard相關(guān)注意事項(xiàng)與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)標(biāo)題顯示隱藏功能
這篇文章主要介紹了Android實(shí)現(xiàn)標(biāo)題顯示隱藏功能2016-02-02

