最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android巧用DecorView實(shí)現(xiàn)對話框功能

 更新時間:2017年04月08日 10:03:39   作者:大批  
本篇文章主要介紹了Android巧用DecorView實(shí)現(xiàn)對話框功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

如果還不知道DecorView,那也沒有什么關(guān)系 ^_^

先來看看實(shí)現(xiàn)的效果

實(shí)現(xiàn)的大致思路

  1. 首先需要明白什么是DecorView,他是android中界面的根布局。其實(shí)android的activity界面整個就是一個控件樹,DecorView是根節(jié)點(diǎn),DecorView的孩子節(jié)點(diǎn)就是一個LinearLayout,這個LinearLayout的孩子系節(jié)點(diǎn)就包括狀態(tài)欄 + 和我們自己寫的布局
  2. DecorView是FramLayout的子類(可以從源碼中看到)
  3. 既然DecorView是根節(jié)點(diǎn),而且還是FrameLayout,所以我們可以把我們自己的布局 添加到DecorView 或者 從DecorView移除,這樣就模擬出了一個Dialog的效果~~ ,當(dāng)然這個Dialog的樣式,動畫就可以自己想怎么寫就怎么寫了撒
  4. 通過activity.getWindow().getDecorView()可以獲得DecorView

[下面大量 代碼 ]

第一個對話框的實(shí)現(xiàn)

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;
  }
}

其實(shí)就是show的時候?qū)⒉季痔砑拥紻ecorView上面去,dismiss的時候?qū)⒉季謴腄ecorView上面移除

提示的實(shí)現(xiàn)(沒有處理完善~~ 僅僅就是說明哈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);

  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

蛟河市| 沧州市| 河南省| 唐山市| 虹口区| 平湖市| 德兴市| 栾川县| 郧西县| 临湘市| 奉化市| 噶尔县| 中江县| 广东省| 电白县| 乌苏市| 玉林市| 斗六市| 安丘市| 揭阳市| 北辰区| 泰来县| 福建省| 绥阳县| 石家庄市| 来凤县| 大埔区| 肇东市| 东平县| 田林县| 阿拉善右旗| 上饶县| 平顺县| 灵石县| 新建县| 孟津县| 鄂伦春自治旗| 平乡县| 元朗区| 奉贤区| 裕民县|