自定义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" ...
随机推荐
- 关于多系统跨浏览器 BrowserStack 的使用
偶然在Scott Hanselman Blogs看到一篇关于 BrowserStack 博文,对于前端多浏览器测试. 现在拥有各自内核的浏览器越来越多,各自的特性也千差万别.如果作为一个前端攻城师想要 ...
- document.body和document.documentElement区别
1.document.documentElement表示文档节点树的根节点,即<html> document.body是body节点 2. 页面具有 DTD,或者说指定了 DOCTYPE ...
- 2018.09.19 atcoder AtCoDeer and Election Report(贪心)
传送门 很有意思的一道贪心. 就是每次翻最小的倍数来满足条件. 代码: #include<bits/stdc++.h> #define ll long long using namespa ...
- jdk10运行springboot项目出现:Type javax.xml.bind.JAXBContext not present
项目由openjdk8.0迁移到jdk10导致的 原因:java9模块化的概念使得JAXB默认没有加载: jaxb-api是存在jdk中的,只是默认没有加载而已,手动引入即可. 推荐方式: <! ...
- vs2010 EF4.0 访问mysql
需要安装mysql-connector-net-6.3.5 6.8.9的安装完后在dbfirst里找不到对应的提供程序 链接字符串里需要 指定下编码(如果不是gbk的话) <add name=& ...
- 关于调用Feign client超时得不到结果的问题
需要在调用方的配置文件加入以下配置 hystrix.command.default.execution.timeout.enabled: false ribbon: ConnectTimeout: R ...
- PS各个工具的字母快捷键和英文全名
选框-Marquee(M) 移动-move(V) 套索-Lasso(L) 魔棒-Wand(W) 喷枪-injection lance (J) 画笔-Brush (B) 铅笔-pencil(N) 橡皮图 ...
- GoogleStyle格式化代码
<div class="iteye-blog-content-contain" style="font-size: 14px"></div&g ...
- python-optparse模块给脚本增加参数选项
import optparse parser = optparse.OptionParser('[-] Usage %prog '+ '-H <RHOST[s]> -l [-p -F ]' ...
- Android属性动画之ValueAnimator的介绍
之前两篇博客,介绍的是ObjectAnimator作用与某一个控件的某一个属性.但我们的ValueAnimator它本身并不会作用与任何一个属性,它本身也不会提供任何一种动画.它简单的来说,就是一个数 ...