自定義android ProgressDialog
來自: http://blog.csdn.net//guijiaoba/article/details/19288461
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();
}
}
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
本文由用戶 oktua689 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!