android 自定义dialog弹出和消失动画

来源:互联网 发布:英文版导航软件 编辑:程序博客网 时间:2024/04/29 18:19

自定义dialog窗口,根据坐标可随意设置dialog显示位置,实现了窗口弹出动画

 

Java代码:

package com.sunxu.org.IndividualityDialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class IndividualityDialogActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.button1);
        
        btn.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {

                //多个Activity嵌套时用this.parent否则异常
                new myDialog(IndividualityDialogActivity.this)
                    .showDialog(R.layout.dialog, 80, 50);
            }
        });
    }
    
    //自定义Dialog
    class myDialog extends Dialog{
        
        private Window window = null;
        
        public myDialog(Context context)
        {
            super(context);
        }
        
        public void showDialog(int layoutResID, int x, int y){
            setContentView(layoutResID);
            
            windowDeploy(x, y);
            
            //设置触摸对话框意外的地方取消对话框
            setCanceledOnTouchOutside(true);
            show();
        }
        
        //设置窗口显示
        public void windowDeploy(int x, int y){
            window = getWindow(); //得到对话框
            window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
            window.setBackgroundDrawableResource(R.color.vifrification); //设置对话框背景为透明
            WindowManager.LayoutParams wl = window.getAttributes();
            //根据x,y坐标设置窗口需要显示的位置
            wl.x = x; //x小于0左移,大于0右移
            wl.y = y; //y小于0上移,大于0下移  
//            wl.alpha = 0.6f; //设置透明度
//            wl.gravity = Gravity.BOTTOM; //设置重力
            window.setAttributes(wl);
        }
    }
}

 

 

设置窗口弹出,退出动画在res/values下创建style

<?xml version="1.0" encoding="utf-8"?>
< !-- 设置dialog弹出,退出动画 -->

< resources>
    <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">
        <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
        <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
    </style>
    
< /resources>

 

在res/anim下创建,设置dialog窗口弹出动画

<?xml version="1.0" encoding="utf-8"?>

< !-- 弹出时动画 -->
< set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale 
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="0%"
        android:pivotY="100%"
        android:fillAfter="false"
        android:duration="400"/>
< /set>

 

在res/anim下创建,设置dialog窗口退出动画

<?xml version="1.0" encoding="utf-8"?>
< !-- 退出时动画效果 -->
< set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale 
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="0%"
        android:pivotY="100%"
        android:fillAfter="false"
        android:duration="400"/>
< /set>

 

在res/values下创建color

<?xml version="1.0" encoding="utf-8"?>
< resources>
    <color name="vifrification">#00000000</color>   <!-- 透明 -->
< /resources>

 

设置dialog窗口layout

<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/dialog_background" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:text="Hello" />

< /LinearLayout>

 

main布局

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击弹出myDialog" />
< /LinearLayout>

 

<!-- 
    android:fromXscale="1.0" 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
    android:toXscale="0.0"表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)
    android:fromYscale="1.0" 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
    android:toYscale="0.0"表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)
    android:pivotX="50%" X轴缩放的位置为中心点
    android:pivotY="50%" Y轴缩放的位置为中心点
    android:duration="2000" 动画播放时间 这里是2000毫秒也就是2秒
 -->

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 速腾油箱盖打不开怎么办 奥迪a6油箱盖打不开怎么办 苹果手提虚拟机黑屏怎么办 mac系统桌面变大怎么办 删除文件要权限怎么办 页面载入错误了怎么办 手机打不开excel表格怎么办 皇室战争闪退怎么办 苹果老是闪退怎么办 黑苹果开机黑屏怎么办 MAC磁盘删了怎么办 mac磁盘被锁定怎么办 bt5读不到网卡怎么办 笔记本电脑cpu温度过高怎么办 笔记本cpu温度过高怎么办 联想系统崩溃了怎么办 办公软件用不了怎么办 win10设置闪退怎么办 手机浏览器版本低怎么办 wps界面动不了怎么办 手机设置删了怎么办 苹果手机设置不见了怎么办 笔记本电脑键盘不好使怎么办 网咖怎么办临时卡 cydia添加雷锋源失败怎么办 电脑mac已锁定怎么办 苹果home键发热怎么办 苹果软件消失了怎么办 苹果键盘消失了怎么办 苹果图标消失了怎么办 超账户授权有误怎么办 华为账号码被盗怎么办 华为无法截屏怎么办 华为账号密码忘怎么办 华为忘记激活码怎么办 云充吧登录不上怎么办 充电器插头太松怎么办 充电器接口掉了怎么办 华为p9手机发烫怎么办 华为mate8不快充怎么办 小米6x充不进电怎么办