Android實(shí)現(xiàn)加載對(duì)話框
本文實(shí)例為大家分享了Android實(shí)現(xiàn)加載對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下
這里簡(jiǎn)單說一下兩種實(shí)現(xiàn)加載對(duì)話框的方式:1.使用動(dòng)畫讓一個(gè)圖片旋轉(zhuǎn) 2.使用progressbar。
感覺簡(jiǎn)單來說,dialog就是一個(gè)彈出的window,把自己定義的布局放置到window里面就可以了,加載對(duì)話框就是有個(gè)加載的動(dòng)畫,核心的地方就是實(shí)現(xiàn)這個(gè)動(dòng)畫,所所以方法 可以有,對(duì)圖片添加動(dòng)畫,或者使用progressbar。
第一種方式:使用動(dòng)畫讓一個(gè)圖片旋轉(zhuǎn)
先看一下布局:
<?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="@drawable/dialog_bg_while" android:orientation="vertical" > <ImageView android:layout_width="54dp" android:id="@+id/loading_dialog_pic" android:layout_height="54dp" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:background="@drawable/loading" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="正在加載..." /> </LinearLayout>
然后自定義Alertdialog,并對(duì)圖片添加旋轉(zhuǎn)動(dòng)畫:
public class LoadingDialog extends AlertDialog {
private final String DEFAULT_TEXT="正在加載";
private ImageView mImageview;
private TextView mTextView;
private LinearLayout mLayout;
private String mText;
protected LoadingDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null);
mImageview=(ImageView) mLayout.findViewById(R.id.loading_dialog_pic);
mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text);
loadanimation();
getWindow().setContentView(mLayout);
}
private void loadanimation() {//對(duì)圖片添加旋轉(zhuǎn)動(dòng)畫
// TODO Auto-generated method stub
Animation anim=AnimationUtils.loadAnimation(getContext(), R.anim.loading_dialog_anim);
LinearInterpolator lin = new LinearInterpolator();
anim.setInterpolator(lin);
mImageview.setAnimation(anim);
}
}
看一下xml的動(dòng)畫:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="1500" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0.0" android:repeatCount="infinite" android:toDegrees="-358" /> </set>
第二種方式:使用progressbar
首先是一個(gè)animation-list:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <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/loading4" android:duration="100"/> <item android:drawable="@drawable/loading5" android:duration="100"/> <item android:drawable="@drawable/loading6" android:duration="100"/> <item android:drawable="@drawable/loading7" android:duration="100"/> <item android:drawable="@drawable/loading8" android:duration="100"/> </animation-list>
看一下布局的實(shí)現(xiàn):
<?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="wrap_content" android:background="@drawable/dialog_bg_while" android:orientation="vertical" > <ProgressBar style="@android:style/Widget.ProgressBar.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:indeterminateDrawable="@drawable/loading_animation_list" android:indeterminateDuration="1500" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#00BCD4" /> <TextView android:id="@+id/loading_dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="正在加載..." /> </LinearLayout>
然后自定義一個(gè)alertdialog:
public class LoadingDialog extends AlertDialog {
private final String DEFAULT_TEXT="正在加載";
private TextView mTextView;
private LinearLayout mLayout;
private String mText;
protected LoadingDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null);
mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text);
WindowManager m=(WindowManager) getContext().getSystemService(getContext().WINDOW_SERVICE);
int windowwith=m.getDefaultDisplay().getWidth();
int w=windowwith*3/5;
int h=300;
getWindow().setLayout(w, h);//設(shè)置對(duì)話框窗體大小
getWindow().setContentView(mLayout);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter自動(dòng)路由插件auto_route使用詳解
這篇文章主要為大家介紹了Flutter自動(dòng)路由插件auto_route的基本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
android 如何獲取MCC/MNC控制小區(qū)廣播的開啟
獲取MCC/MNC以便控制小區(qū)廣播的開啟下面針對(duì)于單卡、雙卡,為大家詳細(xì)介紹下具體的實(shí)現(xiàn),感興趣的朋友可以參考下哈2013-06-06
Android編程之在SD卡上進(jìn)行文件讀寫操作實(shí)例詳解
這篇文章主要介紹了Android編程之在SD卡上進(jìn)行文件讀寫操作的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android的文件操作及針對(duì)SD卡的存取操作相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android編程使用Intent傳遞對(duì)象的方法分析
這篇文章主要介紹了Android編程使用Intent傳遞對(duì)象的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android使用Intent實(shí)現(xiàn)傳遞對(duì)象的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-01-01
Android開發(fā)中多進(jìn)程共享數(shù)據(jù)簡(jiǎn)析
這篇文章主要為大家簡(jiǎn)單分析Android開發(fā)中多進(jìn)程共享數(shù)據(jù),怎么做才能讓這兩邊共享數(shù)據(jù),感興趣的小伙伴們可以參考一下2016-04-04
Android系統(tǒng)view與SurfaceView的基本使用及區(qū)別分析
這篇文章主要為大家介紹了Android系統(tǒng)view與SurfaceView基本使用的案例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

