Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Dialog在android中的應用(二)

Dialog在android中的應用(二)

編輯:關於Android編程

如果實現一個帶動畫的下拉的Dialog

 

沿用上篇的基類,實現實際上的子類:

 

public  class DropDownFilterDialogForPhone extends DropDownFilterDialog {
    private boolean mShowAnimation = true;
    private boolean mIsInAnimation;
    private boolean mFinishShowDialog = false;

    public DropDownFilterDialogForPhone(Context context) {
        super(context);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drop_down_filter_dialog);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mShowAnimation) {
            Animation downAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.drop_down_menu_down_anim);
            downAnimation.setAnimationListener(new Animation.AnimationListener() {

                @Override
                public void onAnimationEnd(Animation animation) {
                    mFinishShowDialog = true;
                    mIsInAnimation = false;
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }

                @Override
                public void onAnimationStart(Animation animation) {
                }

            });
            mDropDownMenuLayout.startAnimation(downAnimation);
            mIsInAnimation = true;
        } else {
            mFinishShowDialog = true;
        }
    }

    public void hideDropDownFilter(boolean showAnimation) {
        Animation upAnimation = AnimationUtils.loadAnimation(mContext, R.anim.drop_down_menu_up_anim);
        upAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
                mIsInAnimation = false;

            }
        });
        startGridViewUpAnimation(upAnimation);
        mDropdownClickListener.onDropdownHide();
        mIsInAnimation = true;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (mFinishShowDialog) {
                if (!mIsInAnimation) {
                    hideDropDownFilter(true);
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mFinishShowDialog) {
                if (!mIsInAnimation) {
                    hideDropDownFilter(true);
                }
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }


    @Override
    protected void onStop() {
        super.onStop();
    }


    public void showDialog(boolean showAnimation, View banner) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        int positionY = mContext.getResources().getDimensionPixelSize(R.dimen.header_hight);
        if (banner == null) {
            lp.y = positionY;
        } else {
            lp.y = positionY + banner.getMeasuredHeight();
        }
        mShowAnimation = showAnimation;
        show();
    }

    public void startGridViewUpAnimation(Animation animation) {
        mDropDownMenuLayout.startAnimation(animation);
    }
}
   @Override
    public void onClick(View v) {
        if (null == mHeaderClickListener) {
            return;
        }
        super.onClick(v);
        switch (v.getId()) {
            case R.id.title_layout:
                if(mBtnDropDownImageview.getVisibility() == View.GONE) {
                    return;
                }
                if (!mIsShowDialog) {
                    // will show dialog
                    showDropDownFilter(true);
                    mIsShowDialog = true;
                } else {
                    mDropDownFilterDialog.hideDropDownFilter(true);
                    mIsShowDialog = false;
                }
                break;
        }

    }

    public void setOnDropDownMenuClick(DropDownMenuClicked mDropDownMenuClicked) {
        this.mDropDownMenuClicked = mDropDownMenuClicked;
    }


    public void setDropDownMenuVisible(int visible) {
        if(visible == View.VISIBLE) {
            mBtnDropDownImageview.setVisibility(View.VISIBLE);
        }else{
            mBtnDropDownImageview.setVisibility(View.GONE);
        }
    }


    public void rotatePlusButton(Context context, boolean isUp) {
        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(
                context, isUp ? R.anim.flip_up : R.anim.flip_down);
        animatorSet.setTarget(mBtnDropDownImageview);
        animatorSet.start();
    }


    private void showDropDownFilter(boolean showAnimation) {
        if (mDropDownFilterDialog == null) {
            if(false) {
                mDropDownFilterDialog = new DropDownFilterDialogForTablet(getContext());
            }else{
                mDropDownFilterDialog = new DropDownFilterDialogForPhone(getContext());
            }
        }

        rotatePlusButton(mContext, false);
        mDropDownFilterDialog.setCurrentIndex(mCurrentIndex);
        mDropDownFilterDialog.setTopMenuItemList(mDropDownItemList);
        mDropDownFilterDialog.showDialog(showAnimation, mBanner);
        mDropDownFilterDialog.setDropDownClickListener(new DropDownFilterDialog.OnDropdownClickListener() {
            @Override
            public void onDropdownHide() {
                rotatePlusButton(mContext, true);
                mIsShowDialog = false;
            }

            @Override
            public void onClickItem(int index) {
                if (index == mCurrentIndex) {
                    return;
                }
                mCurrentIndex = index;
                mDropDownMenuClicked.onDropDownMenuClicked(index);
            }
        });
    }



    /*****************************************init filter*************************************************/
    public int getCurrentIndex() {
        return mCurrentIndex;
    }



    public void initMessageFilterWithState(int state) {
        mCurrentIndex = state;
        switch (state) {
            case STATE_ALL:
                setText(R.string.messages_bar_item_all);
                break;
            case STATE_VOICE:
                setText(R.string.messages_bar_item_voice);
                break;
            case STATE_FAX:
                setText(R.string.messages_bar_item_fax);
                break;
            case STATE_TEXT:
                setText(R.string.messages_bar_item_text);
                break;
        }
    }


動畫的定義:

 

 



    

在Titlebar裡面點擊的代碼可以這樣寫:

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved