今天,弄了一个dialog加载动画,加载动画应该都是大家安卓项目中经常用到的,那今天我就把我今天搞得一个dialog和大家分享下,最近才开始有写博客的习惯,写的不好,多多包涵,效果如下图所示:
-----------------------------------------------------------------------------
首先,我这个dialog是一个由多张图片循环播发的效果,类是gif图的效果。
1,第一步,项目准备,先制作轮播图的资源文件,一会需要用
我这里有8张图片,一共8贞
新建一个xml文件,ProgressDialog.xml
<pre style="font-family: 'DejaVu Sans Mono'; font-size: 9.8pt; background-color: rgb(255, 255, 255);"><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="83" android:drawable="@drawable/dialog0" />
<item android:duration="83" android:drawable="@drawable/dialog1" />
<item android:duration="83" android:drawable="@drawable/dialog2" />
<item android:duration="83" android:drawable="@drawable/dialog3" />
<item android:duration="83" android:drawable="@drawable/dialog4" />
<item android:duration="83" android:drawable="@drawable/dialog5" />
<item android:duration="83" android:drawable="@drawable/dialog6" />
<item android:duration="83" android:drawable="@drawable/dialog7" />
<item android:duration="83" android:drawable="@drawable/dialog8" />
</animation-list>
duration 属性是指播发的速度
drawable 是对应的资源文件
2,第二布,新建一个类MyDialog.java 继承ProgressDialog
然后我们现在开始绘制这个dialo的样式了
重写oncreate方法在创建视图的生命周期方法里,我们先创建一个LinearLayout。
</pre><pre name="code" class="java">//创建LinearLayout容器
LinearLayout linearLayout=new LinearLayout(context);
//设置垂直布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
//适配屏幕
int p = DisplayUtil.dip2px(context,5);
//设置相对居中
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
//设置边距居中
linearLayout.setPadding(p, p, p, p);
//设置视图宽高属性
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
试图有了,我们该往里面丢控件了创建一个ImageView
</pre><pre name="code" class="java"><pre name="code" class="java">image=new ImageView(context);
final LinearLayout.LayoutParams imageParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageParams.leftMargin=4* p;
imageParams.rightMargin=4* p;
//设置大小
imageParams.height=320;
imageParams.width=320;
//为ImageView设置属性
image.setLayoutParams(imageParams);
最后,我们把imageView添加到试图中
</pre><pre name="code" class="java">linearLayout.addView(image);
setContentView(linearLayout);
//添加高亮显示
setScreeBrightness();
</pre><span style="font-size:24px; color:#3366ff">为了让dialog更加的明亮,做一个特殊处理</span>
/**
* 此处设置亮度值。dimAmount代表黑暗数量,也就是昏暗的多少,设置为0则代表完全明亮。
* 范围是0.0到1.0
*/
private void setScreeBrightness(){
Window window=getWindow();
WindowManager.LayoutParams lp=window.getAttributes();
lp.dimAmount=0.3f;
window.setAttributes(lp);
}
//工具方法
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
----现在一个Dialog就创建穿出来,只不过还是一个什么都没有的空壳子,Dialog有了,那么接下来,就是创建动画了 我们为dialog添加一个setOnShowListener监听,在监听里面,我们去启动动画的播放
</pre><pre name="code" class="java"> <pre name="code" class="java">this.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
//添加图片资源
image.setBackgroundResource(+R.anim.dialog_anim);
//创建一个逐帧动画
AnimationDrawable animation= (AnimationDrawable) image.getBackground();
//循环播发
animation.setOneShot(false);
if (animation.isRunning()) {
animation.stop();
}
//启动
animation.start();
}
});
</pre>
我们除了监听show的时候启动播发动画之外,我们应该还有做的是在dismm的时候停止动画
public static void showDialog(Context context){
if (dialog==null){
dialog=new MyDialog(context);
dialog.setCancelable(true);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (myDismm != null) {
myDismm.onDismiss(dialog);
}
MyDialog.dialog.cancel();
MyDialog.dialog = null;
}
});
dialog.show();
}
}
然后,对外开放两个方法。启动(上面的就是启动的方法)和关闭
public static void dismissDialog(){
if (dialog != null&& dialog.isShowing()) {
dialog.dismiss();
}
}
然后在使用的时候,只需要下面这样就可以了
//启动
MyDialog.showDialog(context);
//关闭
MyDialog.dismissDialog();
//最后附上完整的类
package com.android.zsjj.webapp.widget;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.android.zsjj.webapp.R;
import com.android.zsjj.webapp.util.DisplayUtil;
/**
* Created by zhengkaiyuan on 16/5/30.
*/
public class MyDialog extends ProgressDialog {
private Context context;
private ImageView image;
private static MyDialog dialog;
private static OnDismissListener myDismm;
public MyDialog(Context context) {
super(context);
this.context=context;
}
public MyDialog(Context context, int theme) {
super(context, theme);
}
public static void showDialog(Context context){
if (dialog==null){
dialog=new MyDialog(context);
dialog.setCancelable(true);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (myDismm != null) {
myDismm.onDismiss(dialog);
}
MyDialog.dialog.cancel();
MyDialog.dialog = null;
}
});
dialog.show();
}
}
public static void dismissDialog(){
if (dialog != null&& dialog.isShowing()) {
dialog.dismiss();
}
}
public static boolean isShow(){
if (dialog != null) {
return dialog.isShow();
}
return false;
}
public static void setDialogCancelable(Boolean yesAndNo){
if (dialog != null) {
dialog.setCancelable(yesAndNo);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout=new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
int p = DisplayUtil.dip2px(context,5);
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
linearLayout.setPadding(p, p, p, p);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
image=new ImageView(context);
final LinearLayout.LayoutParams imageParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageParams.leftMargin=4* p;
imageParams.rightMargin=4* p;
imageParams.height=320;
imageParams.width=320;
image.setLayoutParams(imageParams);
linearLayout.addView(image);
setContentView(linearLayout);
setScreeBrightness();
this.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
image.setBackgroundResource(+R.anim.dialog_anim);
AnimationDrawable animation= (AnimationDrawable) image.getBackground();
animation.setOneShot(false);
if (animation.isRunning()) {
animation.stop();
}
animation.start();
}
});
}
/**
* 此处设置亮度值。dimAmount代表黑暗数量,也就是昏暗的多少,设置为0则代表完全明亮。
* 范围是0.0到1.0
*/
private void setScreeBrightness(){
Window window=getWindow();
WindowManager.LayoutParams lp=window.getAttributes();
lp.dimAmount=0.3f;
window.setAttributes(lp);
}
}