第一种方式:使用动画让一个图片旋转
然后自定义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); }}