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

從圖中我們可以看到要這個(gè)Dialog是圖片還有文字組成的,(不過(guò)我這里使用代碼實(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);//無(wú)限循環(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);
// 頁(yè)面中的LoadingView
mLoadingView = (LVCircularRing) view.findViewById(R.id.lv_circularring);
// 頁(yè)面中顯示文本
TextView loadingText = (TextView) view.findViewById(R.id.loading_text);
// 顯示文本
loadingText.setText(msg);
// 創(chuàng)建自定義樣式的Dialog
mLoadingDialog = new Dialog(context, R.style.loading_dialog);
// 設(shè)置返回鍵無(wú)效
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();
以上是對(duì)Android Dialog 重寫的小示例,有需要的朋友可以參考下。
- Android編程自定義Dialog的方法分析
- Android自定義dialog可選擇展示年月日時(shí)間選擇欄
- Android中用Builder模式自定義Dialog的方法
- Android自定義Dialog實(shí)現(xiàn)文字動(dòng)態(tài)加載效果
- Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)
- Android中制作自定義dialog對(duì)話框的實(shí)例分享
- Android自定義dialog簡(jiǎn)單實(shí)現(xiàn)方法
- Android編程經(jīng)典代碼集錦(復(fù)制,粘貼,瀏覽器調(diào)用,Toast顯示,自定義Dialog等)
- Android編程中自定義dialog用法實(shí)例
- Android 去掉自定義dialog的白色邊框的簡(jiǎn)單方法
- Android 自定義dialog的實(shí)現(xiàn)代碼
相關(guān)文章
Android ToolBar整合實(shí)例使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android ToolBar整合實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
android:descendantFocusability方法介紹
開發(fā)中很常見的一個(gè)問題,項(xiàng)目中的listview不僅僅是簡(jiǎn)單的文字,常常需要自己定義listview,問題就出現(xiàn)了,可能會(huì)發(fā)生點(diǎn)擊每一個(gè)item的時(shí)候沒有反應(yīng),無(wú)法獲取的焦點(diǎn)2012-11-11
Android端權(quán)限隱私的合規(guī)化處理實(shí)戰(zhàn)記錄
大家應(yīng)該都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用市場(chǎng)都要求應(yīng)用上架需要用戶協(xié)議,這篇文章主要給大家介紹了關(guān)于Android端權(quán)限隱私合規(guī)化處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
Android便攜式熱點(diǎn)的開啟狀態(tài)檢測(cè)和SSID的獲取方法
WIFI熱點(diǎn)的開啟狀態(tài)和開啟后的SSID如何獲取呢?接下來(lái)通過(guò)本文給大家分享Android便攜式熱點(diǎn)的開啟狀態(tài)檢測(cè)和SSID的獲取方法,需要的朋友參考下吧2017-01-01
Flutter實(shí)現(xiàn)抖音點(diǎn)贊效果
抖音的點(diǎn)贊效果在第一次看到的時(shí)候,總有一種眼前一亮的感覺。一邊看視頻,還能在視頻上點(diǎn)贊,而且整個(gè)屏幕都能夠點(diǎn)贊,并伴隨動(dòng)畫,還是很炫酷的。今天我們用Flutter來(lái)實(shí)現(xiàn)一下這個(gè)效果2021-05-05
Android實(shí)現(xiàn)支付寶支付密碼輸入界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)支付寶支付密碼輸入界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
React?Native之在Android上添加陰影的實(shí)現(xiàn)
這篇文章主要介紹了React?Native之在Android上添加陰影的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

