欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android巧用DecorView实现对话框功能

时间:2020-11-25 12:31:21|栏目:Android代码|点击:

如果还不知道DecorView,那也没有什么关系 ^_^

先来看看实现的效果

实现的大致思路

  1. 首先需要明白什么是DecorView,他是android中界面的根布局。其实android的activity界面整个就是一个控件树,DecorView是根节点,DecorView的孩子节点就是一个LinearLayout,这个LinearLayout的孩子系节点就包括状态栏 + 和我们自己写的布局
  2. DecorView是FramLayout的子类(可以从源码中看到)
  3. 既然DecorView是根节点,而且还是FrameLayout,所以我们可以把我们自己的布局 添加到DecorView 或者 从DecorView移除,这样就模拟出了一个Dialog的效果~~ ,当然这个Dialog的样式,动画就可以自己想怎么写就怎么写了撒
  4. 通过activity.getWindow().getDecorView()可以获得DecorView

[下面大量 代码 ]

第一个对话框的实现

public class TipsDialog {
  private Activity activity;
  private View rootView;
  private TextView confirmTextView;
  private TextView cancelTextView;
  private TextView contentTextView;

  private boolean isShowing;

  public TipsDialog(Activity activity) {
    this.activity = activity;
    isShowing = false;
    rootView = LayoutInflater.from(activity).inflate(R.layout.view_tips_dialog,null);
    confirmTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_confirm);
    cancelTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_cancel);
    contentTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_content);

  }

  public void show(){
    if(activity == null){
      return;
    }
    if(isShowing){
      return;
    }
    ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.gravity = Gravity.CENTER;
    rootView.setLayoutParams(params);
    decorView.addView(rootView);
    rootView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dismiss();
      }
    });

    RotateAnimation rotateAnimation = new RotateAnimation(0,720f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
    rotateAnimation.setDuration(2000);
    contentTextView.startAnimation(rotateAnimation);

    isShowing = true;
  }

  public void dismiss(){
    if(!isShowing){
      return;
    }
    isShowing = false;
    if(rootView.getParent() == null){
      return;
    }
    ViewGroup parent = (ViewGroup) rootView.getParent();
    parent.removeView(rootView);

  }

  public int getRandomColor(){
    Random random = new Random();
    return Color.argb(random.nextInt(200),random.nextInt(240),random.nextInt(240),random.nextInt(240));
  }

  public boolean isShowing() {
    return isShowing;
  }
}

其实就是show的时候将布局添加到DecorView上面去,dismiss的时候将布局从DecorView上面移除

提示的实现(没有处理完善~~ 仅仅就是说明哈DecorView)

public class TopTipDialog {
  private Activity activity;
  private View rootView;
  private boolean isShowing;
  private static final int VIEW_HEIGHT = 64;//px

  public TopTipDialog(Activity activity) {
    this.activity = activity;
    rootView = LayoutInflater.from(activity).inflate(R.layout.view_top_tip_dialog,null);
  }


  public void show(){

    if(isShowing){
      return;
    }
    ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, VIEW_HEIGHT);
    params.gravity = Gravity.TOP;
    params.setMargins(0,0,0,-VIEW_HEIGHT);
    rootView.setLayoutParams(params);

    TranslateAnimation translateAnimation = new TranslateAnimation(0,0,-VIEW_HEIGHT,0);
    translateAnimation.setDuration(1500);
    translateAnimation.setFillAfter(true);
    decorView.addView(rootView);
    rootView.startAnimation(translateAnimation);

    rootView.postDelayed(new Runnable() {
      @Override
      public void run() {
        TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,-VIEW_HEIGHT);
        translateAnimation1.setDuration(1500);
        translateAnimation1.setFillAfter(true);
        rootView.startAnimation(translateAnimation1);
      }
    },3000);

  }
}

上一篇:Android实现屏幕手写签名

栏    目:Android代码

下一篇:Android关于FTP文件上传和下载功能详解

本文标题:Android巧用DecorView实现对话框功能

本文地址:http://www.codeinn.net/misctech/26217.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有