自定义dialog,加载数据或网络请求时 dialog+动画(两种样式分享)
2015-03-25 14:40
519 查看
方式一:
效果图
package com.example.dialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
public class ProgressBarUtils {
private static Dialog mDialog;
private static void initProgressBar(Context context) {
mDialog = new AlertDialog.Builder(context).create();
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();
Window window = mDialog.getWindow();
window.setContentView(R.layout.progress_bar);
}
public static void showProgressBar(Context context) {
hideProgressBar();
initProgressBar(context);
}
public static void showProgressBar(Context context, String message) {
hideProgressBar();
showProgressBar(context);
}
public static void hideProgressBar() {
if (mDialog != null) {
mDialog.cancel();
mDialog = null;
}
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateBehavior="repeat"
android:indeterminateDrawable="@drawable/progressbar_bg"
android:indeterminateOnly="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
drawable下资源文件:
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progress_bar"
android:pivotX="50%"
android:pivotY="50%"
/>
方式二:
效果图:
代码 :
package com.example.dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
public class ShowProgressDialog extends Dialog {
private Context ctt=null;
private static ShowProgressDialog dialog;
public static ShowProgressDialog createDialog(Context context){
if (dialog == null) {
//dialog = new ShowProgressDialog(context);
dialog = new ShowProgressDialog(context,R.style.CustomProgressDialog);//这里 你可以自己定义一个
dialog.setContentView(R.layout.showprogressdialog);
dialog.setCancelable(false);//设置按back键不会取消
dialog.setCanceledOnTouchOutside(false);//设置窗体外部 不可触摸
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;//显示在中间
dialog.onWindowFocusChanged(false);//关键
dialog.show();
return dialog;
}else {
return dialog;
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (dialog==null) {
return;
}else {
ImageView imageView = (ImageView) dialog.findViewById(R.id.loadingImageView);
AnimationDrawable ad= (AnimationDrawable) imageView.getDrawable(); //关键
ad.start();
}
}
//系统提供的 一个参数的
public ShowProgressDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.ctt = context;
}
//系统提供的 二个参数的
public ShowProgressDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.ctt = context;
}
//系统提供 带三个参数的
protected ShowProgressDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
this.ctt = context;
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:src="@drawable/progress_round" />
</RelativeLayout>
drawable下 资源文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loading_01" android:duration="100"></item>
<item android:drawable="@drawable/loading_02" android:duration="100"></item>
<item android:drawable="@drawable/loading_03" android:duration="100"></item>
<item android:drawable="@drawable/loading_04" android:duration="100"></item>
<item android:drawable="@drawable/loading_05" android:duration="100"></item>
<item android:drawable="@drawable/loading_06" android:duration="100"></item>
<item android:drawable="@drawable/loading_07" android:duration="100"></item>
<item android:drawable="@drawable/loading_08" android:duration="100"></item>
</animation-list>
调用示例:
package com.example.dialog;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends Activity implements GetContent{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hell=(TextView) findViewById(R.id.hell);
show_qishu=(TextView) findViewById(R.id.show_qishu);
show_beishu=(TextView) findViewById(R.id.show_beishu);
hell.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Mydialog.createDialog(MainActivity.this).show();
//自定义dialog示例1
//ShowProgressDialog dialog=ShowProgressDialog.createDialog(MainActivity.this);
//dialog.show(); //注意里面我是 封装了show()方法的
//自定义dialog示例 2
ProgressBarUtils.showProgressBar(MainActivity.this);
}
});
}
}
效果图
package com.example.dialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
public class ProgressBarUtils {
private static Dialog mDialog;
private static void initProgressBar(Context context) {
mDialog = new AlertDialog.Builder(context).create();
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();
Window window = mDialog.getWindow();
window.setContentView(R.layout.progress_bar);
}
public static void showProgressBar(Context context) {
hideProgressBar();
initProgressBar(context);
}
public static void showProgressBar(Context context, String message) {
hideProgressBar();
showProgressBar(context);
}
public static void hideProgressBar() {
if (mDialog != null) {
mDialog.cancel();
mDialog = null;
}
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateBehavior="repeat"
android:indeterminateDrawable="@drawable/progressbar_bg"
android:indeterminateOnly="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
drawable下资源文件:
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progress_bar"
android:pivotX="50%"
android:pivotY="50%"
/>
方式二:
效果图:
代码 :
package com.example.dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
public class ShowProgressDialog extends Dialog {
private Context ctt=null;
private static ShowProgressDialog dialog;
public static ShowProgressDialog createDialog(Context context){
if (dialog == null) {
//dialog = new ShowProgressDialog(context);
dialog = new ShowProgressDialog(context,R.style.CustomProgressDialog);//这里 你可以自己定义一个
dialog.setContentView(R.layout.showprogressdialog);
dialog.setCancelable(false);//设置按back键不会取消
dialog.setCanceledOnTouchOutside(false);//设置窗体外部 不可触摸
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;//显示在中间
dialog.onWindowFocusChanged(false);//关键
dialog.show();
return dialog;
}else {
return dialog;
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (dialog==null) {
return;
}else {
ImageView imageView = (ImageView) dialog.findViewById(R.id.loadingImageView);
AnimationDrawable ad= (AnimationDrawable) imageView.getDrawable(); //关键
ad.start();
}
}
//系统提供的 一个参数的
public ShowProgressDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.ctt = context;
}
//系统提供的 二个参数的
public ShowProgressDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.ctt = context;
}
//系统提供 带三个参数的
protected ShowProgressDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
this.ctt = context;
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:src="@drawable/progress_round" />
</RelativeLayout>
drawable下 资源文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loading_01" android:duration="100"></item>
<item android:drawable="@drawable/loading_02" android:duration="100"></item>
<item android:drawable="@drawable/loading_03" android:duration="100"></item>
<item android:drawable="@drawable/loading_04" android:duration="100"></item>
<item android:drawable="@drawable/loading_05" android:duration="100"></item>
<item android:drawable="@drawable/loading_06" android:duration="100"></item>
<item android:drawable="@drawable/loading_07" android:duration="100"></item>
<item android:drawable="@drawable/loading_08" android:duration="100"></item>
</animation-list>
调用示例:
package com.example.dialog;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends Activity implements GetContent{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hell=(TextView) findViewById(R.id.hell);
show_qishu=(TextView) findViewById(R.id.show_qishu);
show_beishu=(TextView) findViewById(R.id.show_beishu);
hell.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Mydialog.createDialog(MainActivity.this).show();
//自定义dialog示例1
//ShowProgressDialog dialog=ShowProgressDialog.createDialog(MainActivity.this);
//dialog.show(); //注意里面我是 封装了show()方法的
//自定义dialog示例 2
ProgressBarUtils.showProgressBar(MainActivity.this);
}
});
}
}
相关文章推荐
- 自定义网络请求时加载动画dialog
- android自定义加载数据动画dialog和简单的timer计时器
- 使用Animation list实现网络请求过程中的加载动画dialog
- Android为网络请求自定义加载动画
- 组合属性动画:缩放、渐变 平移和旋转+ImageLoader加载图片+MVP+OKhttp+拦截器+请求网络数据二级列表购物车
- AlertDialog对话框自定义大小,加载网络请求,点击不消失
- 自定义带进度的ProgressRelativeLayout,实现加载网络数据时有进度条样式
- Vue2.0 使用vue-resource 模拟网络请求加载本地数据
- RxJava + Retrofit+okhttp 封装,包含对相同格式请求数据、相同格式返回数据处理,显示 Material Design 加载 dialog,文件上传下载进度展示、全局异常捕捉。
- MVP框架+RXJava+reftorfit请求网络数据+XRecycleView多条目展示(banner轮播+条目展示、刷新加载)、点击事件+fresco图片加载
- iOS网络编程(二) 自定义请求网络类----推荐用于需要请求过程片段数据
- iOS中 UIWebView加载网络数据 技术分享
- pull解析请求网络的数据(带分页加载,刷新)
- 异步加载网络数据,自定义进度条显示
- 网络请求数据加滑动加载
- 两种数据加载动画
- 异步加载网络数据,自定义进度条显示
- 自定义view-58同城数据加载动画
- XlistView 加载头部ViewPager 网络请求数据 存入数据库
- handle发送get和post请求网络数据+多条目加载+banner+xlistview+TabLayout