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

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

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

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

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

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

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

[下面大量 代碼 ]

第一個(gè)對(duì)話框的實(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的時(shí)候?qū)⒉季痔砑拥紻ecorView上面去,dismiss的時(shí)候?qū)⒉季謴腄ecorView上面移除

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

  }
}

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

相關(guān)文章

最新評(píng)論

京山县| 吉安市| 波密县| 涿鹿县| 杭州市| 汉源县| 陈巴尔虎旗| 贵南县| 报价| 嘉定区| 永清县| 文昌市| 夏邑县| 鄢陵县| 定边县| 临沭县| 巍山| 大田县| 海淀区| 沧州市| 大余县| 大荔县| 宜君县| 习水县| 元谋县| 永靖县| 阿巴嘎旗| 吉安市| 陇川县| 鹤壁市| 开阳县| 嘉兴市| 凤城市| 乌海市| 东丰县| 科尔| 浦县| 彰武县| 万宁市| 宣汉县| 揭阳市|