public class MyDialog extends Dialog implements android.view.View.OnClickListener{ private Button cancel; Context context; View localView; private RelativeLayout clearallpan; protected MyDialog(Context context) { super(context); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // 这句代码换掉dialog默认背景,否则dialog的边缘发虚透明而且很宽 // 总之达不到想要的效果 getWindow().setBackgroundDrawableResource(android.R.color.transparent); LayoutInflater inflater = ((AnimationActivity) context).getLayoutInflater(); localView = inflater.inflate(R.layout.animclearpan, null); localView.setAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_bottom_to_top)); setContentView(localView); // 这句话起全屏的作用 getWindow().setLayout(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); initView(); initListener(); } @Override public boolean onTouchEvent(MotionEvent event) { this.dismiss(); return super.onTouchEvent(event); } private void initListener() { cancel.setOnClickListener(this); clearallpan.setOnClickListener(this); } private void initView() { cancel = (Button) findViewById(R.id.cancel); clearallpan = (RelativeLayout) findViewById(R.id.clearallpan); } public void onClick(View v) { switch (v.getId()) { case R.id.cancel: this.dismiss(); break; case R.id.clearallpan: Toast.makeText(context, "请在该区域之外点击", 0).show(); break; } } }
animclearpan.xml布局文件如下:
<?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="fill_parent" android:orientation="vertical" > <!-- android:background="@drawable/clearpanbackground" --> <RelativeLayout android:id="@+id/clearallpan" android:layout_width="fill_parent" android:layout_height="300dp" android:layout_alignParentBottom="true" android:background="@android:color/white" > <Button android:id="@+id/clearall" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="20dip" android:layout_marginBottom="10dip" android:text="删除所有" android:textColor="#FFFFFFFF"/> <Button android:id="@+id/cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dip" android:layout_marginBottom="20dip" android:text="取消" android:layout_below="@id/clearall" android:textColor="#FFFFFFFF"/> </RelativeLayout> </RelativeLayout>
动画效果slide_bottom_to_top.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" > <translate android:duration="300" android:fromYDelta="100.0%" android:toYDelta="10.000002%" /> <alpha android:duration="50" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
自定义Dialog,然后设置为透明背景,然后在View上面设置动画
2: 我们也可以把Acitivity变成Dialog样式,但是这样有个缺点:就是不同的手机room对activity中的跳转样式有所变化,当我设置Activity退出的动画时,没有效果。所以如果需求要求全屏的类似Dialog的样式,就自定义Dialog显示,不要把Activity转成Dialog样式.
一下是把Activity变成Dialog样式
1)Activity如下:
package com.example.picpopupwindow; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; public class SelectPicPopupWindow extends Activity implements OnClickListener{ private Button btn_take_photo, btn_pick_photo, btn_cancel; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alert_dialog); btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo); btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo); btn_cancel = (Button) this.findViewById(R.id.btn_cancel); layout=(LinearLayout)findViewById(R.id.pop_layout); //添加选择窗口范围监听可以优先获取触点,即不再执行onTouchEvent()函数,点击其他地方时执行onTouchEvent()函数销毁Activity layout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!", Toast.LENGTH_SHORT).show(); } }); //添加按钮监听 btn_cancel.setOnClickListener(this); btn_pick_photo.setOnClickListener(this); btn_take_photo.setOnClickListener(this); } //实现onTouchEvent触屏函数但点击屏幕时销毁本Activity @Override public boolean onTouchEvent(MotionEvent event){ finish(); return true; } public void onClick(View v) { switch (v.getId()) { case R.id.btn_take_photo: break; case R.id.btn_pick_photo: break; case R.id.btn_cancel: break; default: break; } finish(); } }
样式alert_dialog.xml如下
<?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:gravity="center_horizontal" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="@drawable/btn_style_alert_dialog_background" > <Button android:id="@+id/btn_take_photo" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> <Button android:id="@+id/btn_pick_photo" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="从相册选择" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> <Button android:id="@+id/btn_cancel" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="15dip" android:layout_marginBottom="15dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消" android:background="@drawable/btn_style_alert_dialog_cancel" android:textColor="#ffffff" android:textStyle="bold" /> </LinearLayout> </RelativeLayout>
在清单文件中配置该Activity
<activity android:name=".SelectPicPopupWindow" android:theme="@style/MyDialogStyleBottom" /> <activity
MyDialogStyleBottom样式如下:
<style name="AnimBottom" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/push_bottom_in</item> <!-- <item name="android:windowExitAnimation">@anim/push_bottom_out</item> --> </style> <style name="MyDialogStyleBottom" parent="android:Theme.Dialog"> <item name="android:windowAnimationStyle">@style/AnimBottom</item> <item name="android:windowFrame">@null</item> <!-- 边框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 无标题 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景透明 --> <item name="android:backgroundDimEnabled">true</item> <!-- 模糊 --> </style>
动画push_bottom_in.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" /> </set>