国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android自定義View實(shí)現(xiàn)loading動(dòng)畫加載效果

2019-12-12 03:27:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 項(xiàng)目開(kāi)發(fā)中對(duì)Loading的處理是比較常見(jiàn)的,安卓系統(tǒng)提供的不太美觀,引入第三發(fā)又太麻煩,這時(shí)候自己定義View來(lái)實(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的方式來(lái)實(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部分的工作完成之后,接下來(lái)就是如何在項(xiàng)目中具體運(yùn)用。在相應(yīng)的布局中使用的時(shí)候,一定要記得把此布局文件add進(jìn)ViewGroup中,因?yàn)樵撟远x的View跟調(diào)用他的View是獨(dú)立的兩個(gè)View,沒(méi)有完成add的話,loading布局可能不會(huì)顯示出來(lái)。

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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五大连池市| 山西省| 济南市| 沂源县| 岳西县| 渝北区| 安新县| 绥阳县| 彰化县| 龙口市| 北票市| 南阳市| 甘孜县| 定结县| 玛沁县| 五峰| 万宁市| 景洪市| 镇康县| 内乡县| 桦川县| 玉山县| 凌海市| 武川县| 安顺市| 城市| 南乐县| 镇赉县| 清镇市| 曲周县| 南昌市| 华阴市| 思茅市| 凌云县| 玛多县| 新田县| 南京市| 平湖市| 苗栗市| 镇江市| 会泽县|