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

最新文章

  1. gravatar配置和使用【让你的网站使用全球通用头像】
  2. (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题
  3. angular animate
  4. debian开机启动管理
  5. B类地址
  6. PHP Framework安装
  7. linux 下安装 nginx
  8. Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解
  9. Maven Build Life Cycle--reference
  10. 利用ajax从txt读取数据
  11. poj 3335 /poj 3130/ poj 1474 半平面交 判断核是否存在 / poj1279 半平面交 求核的面积
  12. POJ 2609 Ferry Loading
  13. 四、正则表达式re模块
  14. 找到一个牛的一逼的,超简易ssm和ssh的学习网址
  15. kotlin学习笔记-异常好玩的list集合总结
  16. maven整合ssh框架笔记
  17. MVC的SignalR例子
  18. Java 8新特性之 并行和并行数组(八恶人-8)
  19. 移动端html的overflow:hidden属性失效问题
  20. Linux基础命令---uniq

热门文章

  1. PS各个工具的字母快捷键和英文全名
  2. changetoutf-8
  3. mysql中要根据某个逗号分割的字符串关联查询另一张表的数据
  4. 自定义方法实现strcpy,strlen, strcat, strcmp函数,了解及实现原理
  5. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
  6. node 命令行
  7. 解决Redis/Codis Connection with master lost(复制超时)问题
  8. [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
  9. ES6 学习笔记之三 函数参数默认值
  10. C# 创建、部署和调用WebService简单示例