第5例:仿退出58同城動(dòng)畫對(duì)話框

核心思想知識(shí)點(diǎn):
1)、 AlertDialog的基本使用
2)、添加動(dòng)畫

效果圖如下

GIF.gif

功能實(shí)現(xiàn)過程

1拷窜、strings.xml

<resources>
    <string name="app_name">動(dòng)畫對(duì)話框</string>
    <string name="text_title1">提示</string>
    <string name="text_title2">您確定要退出58同城嗎涧黄?</string>
    <string name="text_btn_determine">確定</string>
    <string name="text_btn_cancel">取消</string>
</resources>

2、dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        <!--標(biāo)題-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="20dp"
            android:text="@string/text_title1"
            android:textColor="@color/black"
            android:textSize="16sp" />
        <!--提示文字-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="50dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="20dp"
            android:text="@string/text_title2"
            android:textColor="@color/blue"
            android:textSize="20sp" />
        <!--線-->
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/yellow" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!--確定按鈕-->
        <Button
            android:id="@+id/btn_determine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="@string/text_btn_determine"
            android:textColor="@color/orange"
            android:textSize="20sp" />
        <!--線-->
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/yellow" />
        <!--取消按鈕-->
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="@string/text_btn_cancel"
            android:textColor="@color/orange"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

3、styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="mystyle" parent="android:Animation">
        <!--進(jìn)入時(shí)的動(dòng)畫-->
       <item name="android:windowEnterAnimation">@anim/dialog_enter</item>
        <!--退出時(shí)的動(dòng)畫-->
        <item name="android:windowExitAnimation">@anim/dialog_exit</item>
    </style>


</resources>

4、dialog_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--從上進(jìn)入到中間-->
    <translate
        android:duration="200"
        android:fromYDelta="-100%">
    </translate>

</set>

5月帝、dialog_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--從中間到底部退出-->
    <translate
        android:fromYDelta="0"
        android:toYDelta="100%"
        android:duration="200"/>
</set>

6嚷辅、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    >

</RelativeLayout>

7、MainActivity.java

public class MainActivity extends Activity {

    private Button button_determine, button_cancel;      //定義對(duì)話框按鈕
    private AlertDialog dlg;                               //定義對(duì)話框

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

    /**
     * 判斷當(dāng)單擊手機(jī)返回按鈕時(shí)扁位,從手機(jī)頂部向下移動(dòng)對(duì)話
     * 再次單擊返回按鈕,對(duì)話框?qū)闹虚g向底部移動(dòng)消失對(duì)話框
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判斷如果單擊了返回按鈕
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //創(chuàng)建對(duì)話框?qū)嵗?            dlg = new AlertDialog.Builder(this).create();
            dlg.show();                             //顯示對(duì)話框
            Window window = dlg.getWindow();        //獲取對(duì)話框窗口
            window.setGravity(Gravity.CENTER);     //此處設(shè)置dialog顯示在中心位置
            window.setWindowAnimations(R.style.mystyle);      //添加動(dòng)畫
            window.setContentView(R.layout.dialog_layout);   //設(shè)置對(duì)話框布局文件
            //獲取對(duì)話框確定按鈕
            button_determine = (Button) window.findViewById(R.id.btn_determine);
            //獲取對(duì)話框取消按鈕
            button_cancel = (Button) window.findViewById(R.id.btn_cancel);
            initEvent();                                       //調(diào)用初始化事件方法
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 該方法出事對(duì)話框中按鈕的事件域仇,單擊確定按鈕退出該應(yīng)用
     * 單擊取消按鈕则酝,對(duì)話框?qū)⒁苿?dòng)至底部消失
     */
    private void initEvent() {
        button_determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();          //對(duì)話框移動(dòng)到底部消失
                finish();               //關(guān)閉當(dāng)前應(yīng)用
            }
        });
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();          //對(duì)話框移動(dòng)到底部消失
            }
        });
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市般卑,隨后出現(xiàn)的幾起案子蝠检,更是在濱河造成了極大的恐慌,老刑警劉巖叹谁,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焰檩,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡析苫,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茫死,“玉大人,你說我怎么就攤上這事屡久」窃樱” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵蛤售,是天一觀的道長(zhǎng)妒潭。 經(jīng)常有香客問我,道長(zhǎng)漠酿,這世上最難降的妖魔是什么谎亩? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任宇姚,我火速辦了婚禮夫凸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘魔熏。我一直安慰自己鸽扁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布躲雅。 她就那樣靜靜地躺著巩那,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上裆赵,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天战授,我揣著相機(jī)與錄音,去河邊找鬼植兰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛废境,可吹牛的內(nèi)容都是我干的筒繁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼驮宴,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼呕缭!你這毒婦竟也來了修己?” 一聲冷哼從身側(cè)響起迎罗,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎膘掰,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體咪笑,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡窗怒,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了努隙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辜昵。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖躬存,靈堂內(nèi)的尸體忽然破棺而出舀锨,到底是詐尸還是另有隱情,我是刑警寧澤坎匿,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布碑诉,位于F島的核電站,受9級(jí)特大地震影響进栽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜格嗅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望玄柏。 院中可真熱鬧贴铜,春花似錦、人聲如沸绍坝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)勤讽。三九已至拗踢,卻和暖如春脚牍,著一層夾襖步出監(jiān)牢的瞬間巢墅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留庵芭,地道東北人雀监。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓会前,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親瓦宜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程昵慌,因...
    小菜c閱讀 6,424評(píng)論 0 17
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,152評(píng)論 25 707
  • 馬起同志生于1992年3月19日斋攀,標(biāo)準(zhǔn)的90后雙魚座梧田。 馬起同志就是我,我就是馬起同志裁眯。 從初中起我就有寫小說的夢(mèng)...
    雷芒閱讀 284評(píng)論 0 0
  • 1.高質(zhì)量的名單 2.談合作渠道 3.大眾點(diǎn)評(píng)未状、百度糯米俯画、官網(wǎng)司草、微信 4.設(shè)計(jì) 5.提高效率 6.健身 7.學(xué)英語(yǔ)...
    臉大大閱讀 111評(píng)論 0 0