《Android自定義控件》——打造各種Dialog

? ? ? 這里主要做的就是想讓你可以進行各種Dialog的顯示功能,如果想要各種不同樣式的就可以進行配置不同的文件設置。這里主要做的就是一個集成大部分功能的一個BaseDialog。

這里先放一個效果圖:

顯示效果圖

首先要定義一個主要抽象類的BaseDialog,其他的自定義形式的Dialog都是基于BaseDialog進行實現的。

第一步——實現接口DialogInterface.OnKeyListener來對手機back鍵以及search鍵的屏蔽,防止這些操作對Dialog進行關閉。

public abstract class BaseDialog extends Dialog implements DialogInterface.OnKeyListener{

@Override

public booleanonKey(DialogInterface dialog,intkeyCode, KeyEvent event) {

//event.getRepeatCount()方法是點擊返回鍵多次? 返回back鍵進行屏蔽

if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){

return? true;

}else if(keyCode==KeyEvent.KEYCODE_SEARCH){

return? true;

}

return false;

}

}

定義是否屏蔽手機back鍵等等的操作。

// 即點擊屏蔽其它地方隱藏

protected void setCanClose(){

setCanceledOnTouchOutside(true);

this.setOnKeyListener(null);

}

// 即點擊屏蔽其它地方不隱藏

protected void setNoCanClose(){

setCanceledOnTouchOutside(false);

this.setOnKeyListener(this);

}

第二步 ——對繼承的Dialog方法構造方法的的重寫初步對整個Dialog的設置

先通過xml的Style對Dialog的基本設置進行配置

Style配置位置
Dialog自定義樣式

public BaseDialog(@NonNull Context context) {

super(context,R.style.InputDialogStyle);

onInit();

}

public BaseDialog(@NonNull Context context,@StyleRes int themeResId) {

super(context, R.style.InputDialogStyle);

onInit();

}

protected BaseDialog(@NonNull Context context,boolean cancelable,@Nullable OnCancelListener cancelListener) {

super(context, cancelable, cancelListener);

onInit();

}

第三步對窗口管理器的獲取以及onCreat方法的抽象方法

protected WindowManager windowManager;

protected? void onInit(){

//獲取手機窗口的管理器

windowManager= (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

//取消掉窗口標題

requestWindowFeature(Window.FEATURE_NO_TITLE);

//整個界面的布局

getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);

//默認窗口能關閉

setCanClose();

init();

}

//需要設置window的抽象方法

protected abstract void init();


onCreat的抽象方法,這是讓繼承的自定義Dialog可以進行頁面的布局以及事件的處理

protected abstract void onCreatView();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

onCreatView();

}

第四步顯示時的頁面布局動畫等等效果的處理

基本上dialog出來的動畫可以分成三種:

1.從頂部彈出

2.從中間彈出

3.從底部彈出

那么就開始代碼的實現吧。

首先當然需要進行Dialog的彈出和消失的動畫的實現。

動畫文件配置位置

其次定義相關的需要的動畫,然而這個可以進行自己需求進行設計。這里只是簡單的動畫操作。

這里舉個簡單的例子底部彈出主需要進行位移動畫讓

Dialog的彈出

<!--%p指相對于父容器? duration表示動畫的時間 -->

<translate

android:fromYDelta="100%p" ? ??

android:duration="600"/>

Dialog的消失

<translate

android:toYDelta="100%p"

android:duration="600"/>

其他比如中間彈出和頂部的彈出動畫就不多描述了,很簡單。

接著需要在style中定義完成的動畫

底部Dialog彈出效果

最后就是進行代碼的主要分別top bottom以及middle的操作了。

第一一個枚舉可以對幾個樣式的 type 定義,這就不解釋了,不懂請自行百度枚舉。

//對動畫的枚舉進行設置

protected enum AnimType{

/**

* 頂部彈出

*/

Top(0),

/**

* 底部彈出

*/

Bottom(1),

/**

* 中間彈出

*/

Middle(2);

/**

* 頂部

*/

int type;

AnimType(inti) {

this.type=i;

}

}

重寫show()方法里面每條代碼都進行了注釋,只要慢慢看都會明白。

@Override

public void show() {

super.show();

Window window=getWindow();

//獲取窗體的寬高,這里主要是為了方式對話框超過手機的狀態欄,對齊頂部的位置進行設置

Rect rect=newRect();

window.getDecorView().getWindowVisibleDisplayFrame(rect);

if(rect.top==0){

rect.top= getContext().getResources().getDimensionPixelSize(getContext().getResources().getIdentifier("status_bar_height","dimen","android"));

}

//設置對話框彈出的動畫的選擇

AnimType anim=showAnim();

if(anim==AnimType.Top){

window.setWindowAnimations(R.style.Dialog_Top);

}else if(anim==AnimType.Bottom){

window.setWindowAnimations(R.style.Dialog_Bottom);

}else if(anim==AnimType.Middle){

window.setWindowAnimations(R.style.Dialog_Middle);

}

//設置背景顏色

window.setBackgroundDrawable(new ColorDrawable(0));

//設置全屏

//讓該window后所有的東西都成暗淡(dim)dimAmount設置變暗程度

window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

WindowManager.LayoutParams layoutParams = window.getAttributes();

layoutParams.dimAmount=0.5f;

layoutParams.horizontalMargin=0;

//設置顯示的位置

if(anim==AnimType.Top){

layoutParams.gravity= Gravity.TOP;

}else if(anim==AnimType.Bottom){

layoutParams.gravity= Gravity.BOTTOM;

}else if(anim==AnimType.Middle){

layoutParams.gravity= Gravity.CENTER;

}

//獲取手機屏幕的大小

DisplayMetrics displayMetrics=new DisplayMetrics();

windowManager.getDefaultDisplay().getMetrics(displayMetrics);

layoutParams.width=displayMetrics.widthPixels;

//如果還想修改布局可以在這個方法里進行

onSetLayoutParam(layoutParams,rect);

window.setAttributes(layoutParams);

}

這樣一個基本的BaseDialog就完成了,那么接下來就是進行一個簡單的例子操作了。

最簡單的一個進度條Dialog的實現啦:

如果這里進度條有什么不明白的請看

《Android自定義控件》——帶有百分比數字的漸變顏色進度條

首先進行一個xml文件設置ProgressDialog的布局process_layout_bar.xml

process_layout_bar的布局

public class ProgressDialog extends BaseDialog {

private ProgressWithNum progressBarWithNumber;

private TextView textView;

private String title;

publicProgressDialog(@NonNullContext context) {

super(context);

}

//選擇可以點擊back鍵外界取消Dialog,如果不想可以設置為setNoCanClose();

@Override

protected void init() {

setCanClose();

}

//基本的xml文件布局的設置

@Override

protected void onCreatView() {

setContentView(R.layout.process_layout_bar);

progressBarWithNumber= (ProgressWithNum) findViewById(R.id.id_progressbar01);

textView= (TextView) findViewById(R.id.id_text_view);

textView.setText(this.title);

}

//Dialog動畫以及顯示位置的選擇

@Override

protected AnimType showAnim() {

return AnimType.Middle;

}

//這里是對進度條的最大百分比設置一般為100

public void setMax(int max){

progressBarWithNumber.setMax(max);

}

//這里是對進度條的百分比設置一般為0-100

public void setValue(int value){

progressBarWithNumber.setProgress(value);

}

//進行標題文字的設置

public void setText(String text){

this.title=text;

}

}

最后剩下的就是在Activity中進行顯示了。點擊按鈕進行Dialog的實現,xml文件就不放出來就是一個按鈕Button

Button button= (Button) findViewById(R.id.btn);

final Context context=this;

button.setOnClickListener(newView.OnClickListener() {

@Override

public void onClick(View v) {

if(hasWindowFocus()) {

ProgressDialog processDialog =null;

if(processDialog ==null) {

processDialog=newProgressDialog(context);

processDialog.setText("ssssss");

}

processDialog.show();

processDialog.setMax(100);

processDialog.setValue(50);

}

}

});

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容