自定义android ProgressDialog
Android系统自己提供的UI的都是比较难看的,开发中经常用到自定义对话框,下面分享个最近项目中使用的加载框。
下面是源代码,主要的原理就是准备几个图片,然后循环播放。
MainActivity.java
package com.example.testandroidprogressdialog; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button button1;
Button button2; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this);
button2.setOnClickListener(this);
} public void onClick(View v) {
if (v == button1) {
showDialog("正在加载请稍后");
} else if (v == button2) {
showDialog("");
}
} void showDialog(String msg) {
MyProgressDialog myDialog = new MyProgressDialog(this);
myDialog.setMsg(msg);
myDialog.show();
}
}
MyProgressDialog.java
package com.example.testandroidprogressdialog; import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnShowListener;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; public class MyProgressDialog extends Dialog implements OnShowListener, OnDismissListener {
Context context; ImageView imageview;
TextView textView; String msg; public MyProgressDialog(Context context) {
this(context, R.style.AppTheme_Dialog_NoTitleBar);
} public MyProgressDialog(Context context, int theme) {
super(context, theme);
this.context = context;
} protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = View.inflate(context, R.layout.dialog_progress, null); setContentView(view); imageview = (ImageView) view.findViewById(R.id.iv_loading);
textView = (TextView) view.findViewById(R.id.tv_msg);
setOnShowListener(this); textView.setText(msg);
textView.setVisibility(TextUtils.isEmpty(msg) ? View.GONE : View.VISIBLE);
} public void onShow(DialogInterface dialog) {
AnimationDrawable animationDrawable = (AnimationDrawable) imageview.getBackground();
animationDrawable.start();
} public void onDismiss(DialogInterface dialog) {
AnimationDrawable animationDrawable = (AnimationDrawable) imageview.getBackground();
animationDrawable.stop();
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
对话框的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_progress_bg"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" > <ImageView
android:id="@+id/iv_loading"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@anim/dialog_progress_anim_bg"
android:contentDescription="@string/app_name" /> <TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="16sp"
android:visibility="gone" /> </LinearLayout>
还有比较重要的就是,对话框需要的样式。
<style name="AppTheme.Dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
</style> <style name="AppTheme.Dialog.NoTitleBar">
<item name="android:windowNoTitle">true</item>
<item name="android:background">@drawable/dialog_frame_bg</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
动画文件
<?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/progress_1" android:duration="200"/>
<item android:drawable="@drawable/progress_2" android:duration="200"/>
<item android:drawable="@drawable/progress_3" android:duration="200"/>
<item android:drawable="@drawable/progress_4" android:duration="200"/>
<item android:drawable="@drawable/progress_5" android:duration="200"/>
<item android:drawable="@drawable/progress_6" android:duration="200"/>
<item android:drawable="@drawable/progress_7" android:duration="200"/>
<item android:drawable="@drawable/progress_8" android:duration="60"/>
</animation-list>
原理和实现都是比较简单。主要是自定义一个view,可以播放对动画的图片,也就是一个帧动画。然后重装定义对话框的样式,不让系统的样式和样式出现,最后在显示的时候,播放动画。
下面是下载文件地址http://download.csdn.net/detail/xia215266092/6926849
自定义android ProgressDialog的更多相关文章
- Android自定义类似ProgressDialog效果的Dialog
Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loadin ...
- 【Gradle】自定义Android Gradle工程
自定义Android Gradle工程 defaultConfig默认配置 defaultConfig是Android对象中的一个配置项,负责定义所有的默认配置.一个基本的defaultConfig配 ...
- Android开发之自定义的ProgressDialog
package com.example.dialog; import android.app.ProgressDialog; import android.content.Context; /** * ...
- 自定义android RadioButton View,添加较为灵活的布局处理方式
android的RadioButton的使用历来都让人比较头疼,如在布局方面,图案.文字无法分别设置padding等,另外,低版本的android RadioGroup不支持换行排列的RadioBut ...
- Android 自定义Android带图片和文字的ImageButton
经过分析,上述按钮效果实际上就是一个布局,一个最简单不过的垂直线性布局,上部分是一个ImageView,下部分是一个TextView,这个布局可点击.可设置监听. 我们首先要编写自己的ImageBut ...
- 自定义android Dialog
1.自定义Dialog: import android.app.AlertDialog; import android.app.Dialog; import android.content.Conte ...
- 【Android学习】自定义Android样式checkbox
下面简单介绍下在Androdi中如何更改Checkbox的背景图片,可以自定义样式 1.首先res/drawable中定义编写如下样式的XML,命名为:checkbox_style: <?xml ...
- Android 自定义android控件EditText边框背景
在我们进行Android应用界面设计和时候,为了界面风格的统一,我们需要对一些控件进行自定义.比如我们的应用采用的蓝色风格,但是 android的EditText控制获得焦点后显示的却是黄色的边框背景 ...
- 【转】Android ProgressDialog的使用2
原文网址:http://www.cnblogs.com/hnrainll/archive/2012/03/28/2420908.html <?xml version="1.0" ...
随机推荐
- 在EF中使用MySQL的方法及常见问题
有时需要在网上租用空间或数据库,Mysql成本低一些,所以想将sql server转成mysql…… 注意:在安装Mysql时要选择文字集为utf8,否则将不能使用中文(当前也可以在创建数据库时使用u ...
- 2018.10.19 NOIP训练 游戏问题(分组背包)
传送门 分组背包经典问题. 令f[i][j]f[i][j]f[i][j]表示前iii组花费为jjj的最优值. g[i][j]g[i][j]g[i][j]表示前iii组,第iii组已经支付了平台费用的最 ...
- C++之new/delete/malloc/free详解
主要内容: 1. C语言中的函数malloc和free 2. C++中的运算符new和delete 3. new/delete与malloc/free之间的联系和区别 4. C/C++程序的内 ...
- php文件上传代码解析
php文件上传代码解析 is_uploaded_file() //函数判断指定的文件是否是通过 HTTP POST 上传的,返回一个布尔值. $_FILES['upfile']['tmp_name' ...
- dlib安装教程(for linux)
https://blog.csdn.net/LoHiauFung/article/details/78454905 https://www.linuxidc.com/Linux/2017-11/148 ...
- python socket.error: [Errno 10061]
用Python写server和client时候如果server中sock.bind(('localhost', 8001)) 则client中sock.connect(('localhost', 8 ...
- struts2从浅至深(六)contextMap(存取数据)
A:存数据 1.利用ActionContext存数据 这种方式最简便 这是一个购物车案例 把查询来的数据放入到Session中存储起来 2.利用valuestack值栈存数据 把查询出来的数据放入到值 ...
- opencpu
前端通过它调用后端的R语言,对R函数进行一个封装. 网址:https://github.com/jeroenooms/opencpu.js 使用的是opencpu-0.5.js,对它进行了修改. 1. ...
- hibernate 一对多,由谁维护性能最优
举例如下 Customer类: public class Customer { private int id; private String name; private Set orders = ne ...
- 【WinRT】使用 T4 模板简化字符串的本地化
在 WinRT 中,对控件.甚至图片资源的本地化都是极其方便的,之前我在博客中也介绍过如何本地化应用名称:http://www.cnblogs.com/h82258652/p/4292157.html ...