189 8069 5689

Android实现加载对话框的方法

这篇文章将为大家详细讲解有关Android实现加载对话框的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了秀屿免费建站欢迎大家使用!

具体内容如下

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

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

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

先看一下布局:



 
 
 
 
 

然后自定义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
 }
 
 @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() {//对图片添加旋转动画
 // 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的动画:



 
 
 

第二种方式:使用progressbar

首先是一个animation-list:



 
 
 
 
 
 
 
 
 
 
 

看一下布局的实现:



 
 
 
 
 
 
 

然后自定义一个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);//设置对话框窗体大小
 getWindow().setContentView(mLayout);
 
 }
 
 
}

关于“Android实现加载对话框的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网页标题:Android实现加载对话框的方法
转载源于:http://cdxtjz.cn/article/gophcp.html

其他资讯