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

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

Java代码:

01.package com.sunxu.org.IndividualityDialog;  
02.  
03.import android.app.Activity;  
04.import android.app.Dialog;  
05.import android.content.Context;  
06.import android.os.Bundle;  
07.import android.view.Gravity;  
08.import android.view.View;  
09.import android.view.View.OnClickListener;  
10.import android.view.Window;  
11.import android.view.WindowManager;  
12.import android.widget.Button;  
13.  
14.public class IndividualityDialogActivity extends Activity {  
15.    /** Called when the activity is first created. */  
16.    @Override  
17.    public void onCreate(Bundle savedInstanceState) {  
18.        super.onCreate(savedInstanceState);  
19.        setContentView(R.layout.main);  
20.          
21.        Button btn = (Button)findViewById(R.id.button1);  
22.          
23.        btn.setOnClickListener(new OnClickListener()  
24.        {  
25.            public void onClick(View v)  
26.            {  
27.                //多个Activity嵌套时用this.parent否则异常   
28.                new myDialog(IndividualityDialogActivity.this)  
29.                    .showDialog(R.layout.dialog, 80, 50);  
30.            }  
31.        });  
32.    }  
33.      
34.    //自定义Dialog   
35.    class myDialog extends Dialog{  
36.          
37.        private Window window = null;  
38.          
39.        public myDialog(Context context)  
40.        {  
41.            super(context);  
42.        }  
43.          
44.        public void showDialog(int layoutResID, int x, int y){  
45.            setContentView(layoutResID);  
46.              
47.            windowDeploy(x, y);  
48.              
49.            //设置触摸对话框意外的地方取消对话框   
50.            setCanceledOnTouchOutside(true);  
51.            show();  
52.        }  
53.          
54.        //设置窗口显示   
55.        public void windowDeploy(int x, int y){  
56.            window = getWindow(); //得到对话框   
57.            window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画   
58.            window.setBackgroundDrawableResource(R.color.vifrification); //设置对话框背景为透明   
59.            WindowManager.LayoutParams wl = window.getAttributes();  
60.            //根据x,y坐标设置窗口需要显示的位置   
61.            wl.x = x; //x小于0左移,大于0右移   
62.            wl.y = y; //y小于0上移,大于0下移     
63.//            wl.alpha = 0.6f; //设置透明度   
64.//            wl.gravity = Gravity.BOTTOM; //设置重力   
65.            window.setAttributes(wl);  
66.        }  
67.    }  
68.}  
69.  


 
设置窗口弹出,退出动画在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>

<!-- 
    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秒
 -->

 

在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>


 


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZeroFlutter

感谢支持,请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值