自定义Dialog动画

来源:互联网 发布:mysql for windows 10 编辑:程序博客网 时间:2024/05/16 19:37

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


Java代码:

  1. package com.sunxu.org.IndividualityDialog;

  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.view.Gravity;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.view.Window;
  10. import android.view.WindowManager;
  11. import android.widget.Button;

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

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

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

  1. <resources>
  2.     <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">
  3.         <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
  4.         <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
  5.     </style>
  6.     
  7. </resources>
复制代码

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

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

<!-- 弹出时动画 -->

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">
  2.     <scale 
  3.         android:interpolator="@android:anim/accelerate_interpolator"
  4.         android:fromXScale="1.0"
  5.         android:toXScale="1.0"
  6.         android:fromYScale="0.0"
  7.         android:toYScale="1.0"
  8.         android:pivotX="0%"
  9.         android:pivotY="100%"
  10.         android:fillAfter="false"
  11.         android:duration="400"/>
  12. </set>
复制代码

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

<?xml version="1.0" encoding="utf-8"?>
<!-- 退出时动画效果 -->

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">
  2.     <scale 
  3.         android:interpolator="@android:anim/accelerate_interpolator"
  4.         android:fromXScale="1.0"
  5.         android:toXScale="1.0"
  6.         android:fromYScale="1.0"
  7.         android:toYScale="0.0"
  8.         android:pivotX="0%"
  9.         android:pivotY="100%"
  10.         android:fillAfter="false"
  11.         android:duration="400"/>
  12. </set>
复制代码
在res/values下创建color
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <color name="vifrification">#00000000</color>   <!-- 透明 -->
  4. </resources>
复制代码
设置dialog窗口layout
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:orientation="vertical"
  6.     android:gravity="center"
  7.     android:background="@drawable/dialog_background" >

  8.     <TextView
  9.         android:id="@+id/textView1"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:textSize="50sp"
  13.         android:text="Hello" />

  14. </LinearLayout>
复制代码
main布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <Button
  7.         android:id="@+id/button1"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="点击弹出myDialog" />
  11. </LinearLayout>

  12. <!-- 
  13.     android:fromXscale="1.0" 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
  14.     android:toXscale="0.0"表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)
  15.     android:fromYscale="1.0" 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
  16.     android:toYscale="0.0"表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)
  17.     android:pivotX="50%" X轴缩放的位置为中心点
  18.     android:pivotY="50%" Y轴缩放的位置为中心点
  19.     android:duration="2000" 动画播放时间 这里是2000毫秒也就是2秒
  20. -->
复制代码
原创粉丝点击