激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - Android Animation實戰(zhàn)之屏幕底部彈出PopupWindow

Android Animation實戰(zhàn)之屏幕底部彈出PopupWindow

2021-05-07 16:16無緣公子 Android

這篇文章主要為大家介紹了Android Animation動畫實戰(zhàn)項目,屏幕底部彈出PopupWindow,如何實現(xiàn)?文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

android動畫的一個實戰(zhàn)內(nèi)容,從屏幕底部滑動彈出popupwindow。 相信這種效果大家在很多app上都遇到過,比如需要拍照或者從sd卡選擇圖片,再比如需要分享某些東西時,大多會采用這么一種效果:

Android Animation實戰(zhàn)之屏幕底部彈出PopupWindow

那這種效果如何實現(xiàn)呢?
我們仿寫一個這種效果的實例吧:

Android Animation實戰(zhàn)之屏幕底部彈出PopupWindow

1)我們首先定義一下,彈出窗口的頁面布局組件:take_photo_pop.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:orientation="vertical">
 
  <linearlayout
    android:id="@+id/pop_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:layout_alignparentbottom="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">
 
    <textview
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:clickable="false"
      android:gravity="center"
      android:text="修改頭像"
      android:textcolor="#8a8a8a"
      android:textsize="15sp" />
 
    <view
      android:layout_width="fill_parent"
      android:layout_height="0.1dp"
      android:layout_marginleft="10dp"
      android:layout_marginright="10dp"
      android:background="#00c7c0" />
 
    <button
      android:id="@+id/btn_take_photo"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="拍照"
      android:textcolor="#0e61aa"
      android:textsize="18sp" />
 
    <button
      android:id="@+id/btn_pick_photo"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="從相冊選擇"
      android:textcolor="#0e61aa"
      android:textsize="18sp" />
 
    <button
      android:id="@+id/btn_cancel"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginbottom="15dip"
      android:layout_margintop="15dip"
      android:text="取消"
      android:textcolor="#0e61aa"
      android:textsize="18sp"
      android:textstyle="bold" />
  </linearlayout>
 
</relativelayout>

2)現(xiàn)在定義動畫,要知道該popupwindow出現(xiàn)時是從頁面底部向上滑動,消失時是從上向下滑動消失,,所以我們需要定義兩個動畫文件:
退出動畫pop_exit_anim.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="200"
    android:fromydelta="0"
    android:toydelta="50%p" />
  <alpha
    android:duration="200"
    android:fromalpha="1.0"
    android:toalpha="0.0" />
</set>
顯示動畫pop_enter_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 
  <translate
    android:duration="200"
    android:fromydelta="100%p"
    android:toydelta="0" />
  <alpha
    android:duration="200"
    android:fromalpha="0.0"
    android:toalpha="1.0" />
</set>

關(guān)于這兩個動畫,此處不再多做解析,讀過我之前博文的都應(yīng)該知道啦,很簡單的,若是看不懂?請點擊此文上方的鏈接學(xué)習(xí)之。
3)自定義彈出框popupwindow:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import android.content.context;
import android.graphics.drawable.colordrawable;
import android.view.layoutinflater;
import android.view.motionevent;
import android.view.view;
import android.widget.button;
import android.widget.popupwindow;
import android.widget.relativelayout;
 
public class takephotopopwin extends popupwindow {
 
  private context mcontext;
 
  private view view;
 
  private button btn_take_photo, btn_pick_photo, btn_cancel;
 
 
  public takephotopopwin(context mcontext, view.onclicklistener itemsonclick) {
 
    this.view = layoutinflater.from(mcontext).inflate(r.layout.take_photo_pop, null);
 
    btn_take_photo = (button) view.findviewbyid(r.id.btn_take_photo);
    btn_pick_photo = (button) view.findviewbyid(r.id.btn_pick_photo);
    btn_cancel = (button) view.findviewbyid(r.id.btn_cancel);
    // 取消按鈕
    btn_cancel.setonclicklistener(new view.onclicklistener() {
 
      public void onclick(view v) {
        // 銷毀彈出框
        dismiss();
      }
    });
    // 設(shè)置按鈕監(jiān)聽
    btn_pick_photo.setonclicklistener(itemsonclick);
    btn_take_photo.setonclicklistener(itemsonclick);
 
    // 設(shè)置外部可點擊
    this.setoutsidetouchable(true);
    // mmenuview添加ontouchlistener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
    this.view.setontouchlistener(new view.ontouchlistener() {
 
      public boolean ontouch(view v, motionevent event) {
 
        int height = view.findviewbyid(r.id.pop_layout).gettop();
 
        int y = (int) event.gety();
        if (event.getaction() == motionevent.action_up) {
          if (y < height) {
            dismiss();
          }
        }
        return true;
      }
    });
 
 
    /* 設(shè)置彈出窗口特征 */
    // 設(shè)置視圖
    this.setcontentview(this.view);
    // 設(shè)置彈出窗體的寬和高
    this.setheight(relativelayout.layoutparams.match_parent);
    this.setwidth(relativelayout.layoutparams.match_parent);
 
    // 設(shè)置彈出窗體可點擊
    this.setfocusable(true);
 
    // 實例化一個colordrawable顏色為半透明
    colordrawable dw = new colordrawable(0xb0000000);
    // 設(shè)置彈出窗體的背景
    this.setbackgrounddrawable(dw);
 
    // 設(shè)置彈出窗體顯示時的動畫,從底部向上彈出
    this.setanimationstyle(r.style.take_photo_anim);
 
  }
}

    定義要彈出的組件takephotopopwin,它繼承自popupwindow,具體如何實現(xiàn)的,我備注信息很詳細(xì)了。 有一個地方要提醒的是,就是最后要設(shè)置彈出窗體的顯示動畫,this.setanimationstyle(r.style.take_photo_anim); 這是必不可少的,只有加上了它,才能應(yīng)用動畫效果!
看下take_photo_anim style的定義:

?
1
2
3
4
<style name="take_photo_anim" parent="android:animation">
    <item name="android:windowenteranimation">@anim/pop_enter_anim</item>
    <item name="android:windowexitanimation">@anim/pop_exit_anim</item>
</style>

就這么幾步,一個可以從屏幕底部滑動彈出的組件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void showpopformbottom(view view) {
  takephotopopwin takephotopopwin = new takephotopopwin(this, onclicklistener);
  //showatlocation(view parent, int gravity, int x, int y)
  takephotopopwin.showatlocation(findviewbyid(r.id.main_view), gravity.center, 0, 0);
}
 
private view.onclicklistener onclicklistener = new view.onclicklistener() {
  @override
  public void onclick(view v) {
 
    switch (v.getid()) {
      case r.id.btn_take_photo:
        system.out.println("btn_take_photo");
        break;
      case r.id.btn_pick_photo:
        system.out.println("btn_pick_photo");
        break;
    }
  }
};

這下子,效果就和我一開始傳的圖一致啦!有木有學(xué)會了呢!?

拓展:
     玩過app的大家都知道,在你進(jìn)入新頁面或者注冊登錄啥的時候,都會彈出一個等待的框框,表示網(wǎng)絡(luò)請求中,你需要耐心等待下,比如微信的等待請求框效果如下:

Android Animation實戰(zhàn)之屏幕底部彈出PopupWindow

    這里面其中也有個地方用到了動畫,那就是不停旋轉(zhuǎn)的那個小圖標(biāo),它其實用的就是旋轉(zhuǎn)動畫!
    關(guān)于如何實現(xiàn)這么樣一個旋轉(zhuǎn)等待框,我以前寫過一篇介紹的文章,可查看: 《android自定義progressdialog進(jìn)度等待框》

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久草在线资源观看 | 午夜精品视频免费观看 | 亚洲男人一区 | lutube成人福利在线观看污 | 免费国产视频在线观看 | 狠狠干夜夜草 | 性欧美一区二区 | 国产羞羞网站 | 91精品国产乱码久久桃 | 国产精品免费视频观看 | 国产成人精品区 | 亚洲第一页中文字幕 | 在线天堂中文在线资源网 | 色域tv| 日韩电影av在线 | 国产伦精品一区二区三区在线 | 色吧久久 | 成人一区二区三区在线 | 色婷婷久久一区二区 | 日本成人在线免费 | 一级一片免费看 | 国产中出视频 | 最新中文字幕在线视频 | 国产精品剧情一区二区在线观看 | 色的综合 | 日韩欧美高清片 | 久久久久se | 久久精品超碰 | 久久精品免费国产 | 午夜小视频免费观看 | 久久国产成人精品国产成人亚洲 | 欧美特级黄色 | 中国女警察一级毛片视频 | 日产精品一区二区三区在线观看 | 日韩黄色片网站 | av成人免费| 免费播放欧美毛片 | 91精品国产99久久久久久 | 欧美精品电影一区 | 久久影院午夜 | a网在线 |