自定义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

posted @ 2014-02-16 16:39  鬼脚八  阅读(306)  评论(0编辑  收藏  举报