Android 自定義progressDialog實現
我們在專案中經常會遇到這樣一個應用場景:執行某個耗時操作時,為了安撫使用者等待的煩躁心情我們一般會使用進度條之類的空間,在android中讓大家最 容易想到的就是progressbar或者progressDialog,區別在於前者是一個控制元件,後者是對話方塊。由於一些需求在彈出進度條時不希望使用者 能夠操作其他控制元件,所以只能使用progressDialog,這個時候有遇到了一個問題,我不想要progressDialog的黑色框框,感覺這樣跟 應用的整體風格不協調,這個時候就考慮了寫一個自定義的progressDialog。
在網上搜過很多自定義progressDialog的例子,對著寫了下,但是沒有任何效果,不知道是自己使用的方法不對還是什麼地方出錯了。通過不斷的查詢資料,寫了一個簡單的自定義progressDialog。先上圖看下效果:
1.String.xml 檔案,progressDialog是繼承與Dialog,先設定一下progressDialog的風格,設定背景透明色。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/progress_round"/> <TextView android:id="@+id/id_tv_loadingmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20dp"/> </LinearLayout>
2.customprogressdialog.xml檔案,定義自己的佈局,由於我的需求只需要一個進度條以及一串顯示的內容,所以佈局比較接單
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/progress_round"/> <TextView android:id="@+id/id_tv_loadingmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20dp"/> </LinearLayout>
3.progress_round.xml檔案.這個檔案為了實現轉動的效果,迴圈顯示這些圖片。
<?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="200"/>
</animation-list>
4.CustomProgressDialog.java檔案,這個是就是我們最終需要使用的progressDialog了。
/**************************************************************************************
* [Project]
* MyProgressDialog
* [Package]
* com.lxd.widgets
* [FileName]
* CustomProgressDialog.java
* [Copyright]
* Copyright 2012 LXD All Rights Reserved.
* [History]
* Version Date Author Record
*--------------------------------------------------------------------------------------
* 1.0.0 2012-4-27 lxd ([email protected]) Create
**************************************************************************************/
package com.lxd.widgets;
import com.lxd.activity.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;
/********************************************************************
* [Summary]
* TODO 請在此處簡要描述此類所實現的功能。因為這項註釋主要是為了在IDE環境中生成tip幫助,務必簡明扼要
* [Remarks]
* TODO 請在此處詳細描述類的功能、呼叫方法、注意事項、以及與其它類的關係.
*******************************************************************/
public class CustomProgressDialog extends Dialog {
private Context context = null;
private static CustomProgressDialog customProgressDialog = null;
public CustomProgressDialog(Context context){
super(context);
this.context = context;
}
public CustomProgressDialog(Context context, int theme) {
super(context, theme);
}
public static CustomProgressDialog createDialog(Context context){
customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.customprogressdialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
return customProgressDialog;
}
public void onWindowFocusChanged(boolean hasFocus){
if (customProgressDialog == null){
return;
}
ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
}
/**
*
* [Summary]
* setTitile 標題
* @param strTitle
* @return
*
*/
public CustomProgressDialog setTitile(String strTitle){
return customProgressDialog;
}
/**
*
* [Summary]
* setMessage 提示內容
* @param strMessage
* @return
*
*/
public CustomProgressDialog setMessage(String strMessage){
TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
if (tvMsg != null){
tvMsg.setText(strMessage);
}
return customProgressDialog;
}
}
5.接下來就是寫一個測試activity呼叫我們的progressDialog了。
package
com.lxd.activity;
import
com.lxd.widgets.CustomProgressDialog;
import
android.app.Activity;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.view.Window;
import
android.view.WindowManager;
public
class
MainFrame
extends
Activity {
private
MainFrameTask mMainFrameTask =
null
;
private
CustomProgressDialog progressDialog =
null
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
this
.requestWindowFeature(Window.FEATURE_NO_TITLE);
this
.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mMainFrameTask =
new
MainFrameTask(
this
);
mMainFrameTask.execute();
}
@Override
protected
void
onDestroy() {
stopProgressDialog();
if
(mMainFrameTask !=
null
&& !mMainFrameTask.isCancelled()){
mMainFrameTask.cancel(
true
);
}
super
.onDestroy();
}
private
void
startProgressDialog(){
if
(progressDialog ==
null
){
progressDialog = CustomProgressDialog.createDialog(
this
);
progressDialog.setMessage(
"正在載入中..."
);
}
progressDialog.show();
}
private
void
stopProgressDialog(){
if
(progressDialog !=
null
){
progressDialog.dismiss();
progressDialog =
null
;
}
}
public
class
MainFrameTask
extends
AsyncTask<Integer, String, Integer>{
private
MainFrame mainFrame =
null
;
public
MainFrameTask(MainFrame mainFrame){
this
.mainFrame = mainFrame;
}
@Override
protected
void
onCancelled() {
stopProgressDialog();
super
.onCancelled();
}
@Override
protected
Integer doInBackground(Integer... params) {
try
{
Thread.sleep(
10
*
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
return
null
;
}
@Override
protected
void
onPreExecute() {
startProgressDialog();
}
@Override
protected
void
onPostExecute(Integer result) {
stopProgressDialog();
}
}
}
這樣我們需要的progressDialog效果就出來了
相關推薦
Android 自定義progressDialog實現
我們在專案中經常會遇到這樣一個應用場景:執行某個耗時操作時,為了安撫使用者等待的煩躁心情我們一般會使用進度條之類的空間,在android中讓大家最 容易想到的就是progressbar或者progressDialog,區別在於前者是一個控制元件,後者是對話方塊。由於一些需
【android】自定義ProgressDialog實現暫時隱藏進度值並顯示等待狀態(附原始碼下載)
有時,我們需要訪問網路才能獲取到需要操作的任務數(例如下載的檔案數),而在伺服器返回任務數之前要想隱藏進度百分比和進度數值,就需要我們自己重寫ProgressDialog。等到獲取到任務數後再把進度值和百分比顯示出來。先上效果圖: 關鍵程式碼: public clas
Android -- 自定義view實現keep歡迎頁倒計時效果
super onfinish -m use new getc awt ttr alt 1,最近打開keep的app的時候,發現它的歡迎頁面的倒計時效果還不錯,所以打算自己來寫寫,然後就有了這篇文章。 2,還是老規矩,先看一下我們今天實現的效果 相較於我們常見的倒計時
Android自定義View——實現水波紋效果類似剩余流量球
string 三個點 pre ber block span 初始化 move 理解 最近突然手癢就想搞個貝塞爾曲線做個水波紋效果玩玩,終於功夫不負有心人最後實現了想要的效果,一起來看下吧: 效果圖鎮樓 一:先一步一步來分解一下實現的過程 需要繪制一個正弦曲線(sin
Android自定義processor實現bindView功能
lis dds 定義 java代碼 cli 註冊 文章 type() mage 一、簡介 在現階段的Android開發中,註解越來越流行起來,比如ButterKnife,Retrofit,Dragger,EventBus等等都選擇使用註解來配置。按照處理時期,註解又分為兩
Android自定義view實現圖片選色器
https://www.jb51.net/article/141336.htm 這篇文章主要為大家詳細介紹了Android自定義view實現圖片選色器,具有一定的參考價值,感興趣的小夥伴們可以參考一下 簡介 本文介紹該自定義view的使用及實現的方法,主要實現以下幾個功能: - 選取
Android :自定義view實現簡易的轉盤
直接先上效果圖 xml裡面的程式碼 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
Android 自定義View實現拖拽效果
騰訊QQ有那種紅點拖動效果,今天就來實現一個簡單的自定義View拖動效果,再回到原處,並非完全仿QQ紅點拖動 先來看一下效果圖 簡單說一下實現步驟 1.建立一個類繼承View 2.繪製出一個
Android自定義View實現類似車來了軌跡圖
總體分析下:水平方向recyclewview,item包含定位點,站臺位置和站臺名稱。 下面看實現: 1.繼承framelayout,實現構造方法: public class BusStopPlateView extends FrameLayout { ... public
Android 自定義View實現圓形環繞效果
之前專案中需要實現一個四周環繞中心圓形頭像的效果,感覺還是自定義比較方便,於是就自己封裝了一個控制元件去實現。先貼張圖顯示最終效果。 首先自定義一個View繼承自LinearLayout,通過動態新增childView的方式將子控制元件新增到View中。思路是先新增中間圓形頭像
Android 自定義PopupWindow實現懸浮窗效果
有時候我們需要在介面上彈出一個視窗,而Android中彈出窗體有兩種方式:一種是AlertDialog,另一種就是PopupWindow,AlertDialog的位置是固定的,而PopupWindow的位置可以任意指定。下面我們使用自定義的PopupWindow來完成以下的效果圖:
android 自定義view實現圓盤抽獎的效果
廢話不多說直接上程式碼。 import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import andro
android自定義View實現流式佈局
//先來一張效果圖 //自定義的控制元件 import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.
Android自定義view實現載入中、載入失敗、無資料
一、概述 Android中經常在有的app中可以見到“載入中”並不是以彈出對話方塊的形式顯示的,而是佔用整個螢幕,如果載入失敗就會出現載入失敗頁面,點選載入失敗頁面中任意區域,都可以重新載入。今天就和大家一起學習如何通過自定義view的方式實現載入中、載入失敗
Android 自定義View實現城市選擇列表
使用自定義View的方法,實現一個城市選擇列表 手指滑動時 自定義View實現側邊欄,並提供回撥介面 /** * Created by shixi_tianrui1 on 16-9-18. * 城市選擇列表側邊欄 */ public
android 自定義ViewGroup實現仿淘寶的商品詳情頁
最近公司在新版本上有一個需要, 要在首頁新增一個滑動效果, 具體就是仿照X寶的商品詳情頁, 拉到頁面底部時有一個粘滯效果, 如下圖 X東的商品詳情頁,如果使用者繼續向上拉的話就進入商品圖文描述介面: 剛開始是想拿來主義,直接從網上找個現成的demo來用, 但是網上無一
android 自定義ImageView實現圖片手勢滑動,多點觸控放大縮小效果
首先呢,還是一貫作風,我們先來看看眾多應用中的示例:(這種效果是很常見的,可以說應用的必須品.) 搜狐客戶端 百度新聞客戶端
Android自定義View實現類似水波擴散效果
自定義View一共分為6步第一步public SpreadView(Context context) { this(context,null,0); } public SpreadView(Context context, @Nullable AttributeSe
Android 自定義view實現水波紋效果
今天主要分享水波紋效果:1.標準正餘弦水波紋;2.非標準圓形液柱水波紋;雖說都是水波紋,但兩者在實現上差異是比較大的,一個通過正餘弦函式模擬水波紋效果,另外一個會運用到影象的混合模式(PorterDuffXfermode);先看效果:
Android自定義View實現水波紋效果
本篇博文介紹一個Android自定義View的案例,後續博文會接下自定義View的相關流程和繪製原理。通過自定義控制元件實現。觸控式螢幕幕實現水波紋效果。實現步驟第1步.自定義MyWave繼承Viewpublic class MyWave extends View {}第2步