Android 加载对话框的实现

来源:互联网 发布:淘宝老a工具箱 编辑:程序博客网 时间:2024/05/16 02:19


这里简单说一下两种实现加载对话框的方式:1.使用动画让一个图片旋转 2.使用progressbar。

 感觉简单来说,dialog就是一个弹出的window,把自己定义的布局放置到window里面就可以了,加载对话框就是有个加载的动画,核心的地方就是实现这个动画,所所以方法  可以有,对图片添加动画,或者使用progressbar。

第一种方式:使用动画让一个图片旋转

   先看一下布局:
<?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,并对图片添加旋转动画:
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}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {//对图片添加旋转动画// TODO Auto-generated method stubAnimation anim=AnimationUtils.loadAnimation(getContext(), R.anim.loading_dialog_anim);LinearInterpolator lin = new LinearInterpolator(); anim.setInterpolator(lin);mImageview.setAnimation(anim);}}

看一下xml的动画:
<?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

首先是一个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>

看一下布局的实现:
<?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>



然后自定义一个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}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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);//设置对话框窗体大小 getWindow().setContentView(mLayout);}}


0 0
原创粉丝点击