Android 自定義Dialog 實(shí)例
開發(fā)中經(jīng)常需要請求網(wǎng)絡(luò)獲取數(shù)據(jù),我們在請求網(wǎng)絡(luò)到得到數(shù)據(jù)時(shí)當(dāng)中需要等待一些時(shí)間,為了增加用戶體驗(yàn),我們一般會用一個(gè)Dialog來提示用戶我們在加載網(wǎng)絡(luò)數(shù)據(jù)。
今天我們來實(shí)現(xiàn)如下效果的加載中Dialog。

從圖中我們可以看到要這個(gè)Dialog是圖片還有文字組成的,(不過我這里使用代碼實(shí)現(xiàn)的,沒有用圖片),以下是這個(gè)加載圖形的代碼:
public class LVCircularRing extends View {
private float mWidth = 0f;
private float mPadding = 0f;
private float startAngle = 0f;
private Paint mPaint;
public LVCircularRing(Context context) {
this(context, null);
}
public LVCircularRing(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LVCircularRing(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (getMeasuredWidth() > getHeight())
mWidth = getMeasuredHeight();
else
mWidth = getMeasuredWidth();
mPadding = 5;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.argb(100, 255, 255, 255));
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaint);
mPaint.setColor(Color.WHITE);
RectF rectF = new RectF(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
canvas.drawArc(rectF, startAngle, 100
, false, mPaint);//第四個(gè)參數(shù)是否顯示半徑
}
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(8);
}
public void startAnim() {
stopAnim();
startViewAnim(0f, 1f, 1000);
}
public void stopAnim() {
if (valueAnimator != null) {
clearAnimation();
valueAnimator.setRepeatCount(1);
valueAnimator.cancel();
valueAnimator.end();
}
}
ValueAnimator valueAnimator;
private ValueAnimator startViewAnim(float startF, final float endF, long time) {
valueAnimator = ValueAnimator.ofFloat(startF, endF);
valueAnimator.setDuration(time);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//無限循環(huán)
valueAnimator.setRepeatMode(ValueAnimator.RESTART);//
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
startAngle = 360 * value;
invalidate();
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
if (!valueAnimator.isRunning()) {
valueAnimator.start();
}
return valueAnimator;
}
}
Dialog 代碼:
public class LoadingDialog {
LVCircularRing mLoadingView;
Dialog mLoadingDialog;
public LoadingDialog(Context context,String msg) {
// 首先得到整個(gè)View
View view = LayoutInflater.from(context).inflate(
R.layout.loading_dialog_view, null);
// 獲取整個(gè)布局
LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
// 頁面中的LoadingView
mLoadingView = (LVCircularRing) view.findViewById(R.id.lv_circularring);
// 頁面中顯示文本
TextView loadingText = (TextView) view.findViewById(R.id.loading_text);
// 顯示文本
loadingText.setText(msg);
// 創(chuàng)建自定義樣式的Dialog
mLoadingDialog = new Dialog(context, R.style.loading_dialog);
// 設(shè)置返回鍵無效
mLoadingDialog.setCancelable(false);
mLoadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
}
public void show(){
mLoadingDialog.show();
mLoadingView.startAnim();
}
public void close(){
if (mLoadingDialog!=null) {
mLoadingView.stopAnim();
mLoadingDialog.dismiss();
mLoadingDialog=null;
}
}
}
布局文件loading_dialog_view 代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/dialog_bg" android:padding="20dp" android:orientation="vertical"> <com.ye.daqiapp.ui.widget.loading.LVCircularRing android:id="@+id/lv_circularring" android:layout_width="50dp" android:layout_height="50dp"/> <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginTop="5dp" android:textSize="15sp"/> </LinearLayout>
Dialog中Style代碼:
<style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowContentOverlay">@null</item> </style>
背景dialog_bg 代碼:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 內(nèi)部顏色 --> <solid android:color="#444444" /> <!-- 圓角的幅度 --> <corners android:bottomLeftRadius="3dp" android:bottomRightRadius="3dp" android:topLeftRadius="3dp" android:topRightRadius="3dp" /> </shape>
如何使用:在需要使用的地方初始化Dialog:
LoadingDialog dialog=new LoadingDialog(context,"玩命加載中..."); //顯示Dialog dialog.show(); //關(guān)閉Dialog dialog.close();
以上是對Android Dialog 重寫的小示例,有需要的朋友可以參考下。
- Android編程自定義Dialog的方法分析
- Android自定義dialog可選擇展示年月日時(shí)間選擇欄
- Android中用Builder模式自定義Dialog的方法
- Android自定義Dialog實(shí)現(xiàn)文字動態(tài)加載效果
- Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對話框效果(7)
- Android中制作自定義dialog對話框的實(shí)例分享
- Android自定義dialog簡單實(shí)現(xiàn)方法
- Android編程經(jīng)典代碼集錦(復(fù)制,粘貼,瀏覽器調(diào)用,Toast顯示,自定義Dialog等)
- Android編程中自定義dialog用法實(shí)例
- Android 去掉自定義dialog的白色邊框的簡單方法
- Android 自定義dialog的實(shí)現(xiàn)代碼
相關(guān)文章
Android ListView 實(shí)現(xiàn)上拉加載的示例代碼
這篇文章主要介紹了Android ListView 實(shí)現(xiàn)上拉加載的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Android實(shí)現(xiàn)騰訊新聞的新聞?lì)悇e導(dǎo)航效果
這篇文章主要介紹了Android實(shí)現(xiàn)騰訊新聞的新聞?lì)悇e導(dǎo)航效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android Studio Kotlin代碼和java代碼相互轉(zhuǎn)化實(shí)例
這篇文章主要介紹了Android Studio Kotlin代碼和java代碼相互轉(zhuǎn)化實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android中Service和Activity相互通信示例代碼
在android中Activity負(fù)責(zé)前臺界面展示,service負(fù)責(zé)后臺的需要長期運(yùn)行的任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Android中Service和Activity相互通信的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android ViewPagerIndicator詳解及實(shí)例代碼
這篇文章主要介紹了Android ViewPagerIndicator詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

