自定义dialog窗口,根据坐标可随意设置dialog显示位置,实现了窗口弹出动画
Java代码:
package .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
@anim/dialog_enter_anim
@anim/dialog_exit_anim
在res/anim下创建,设置dialog窗口弹出动画
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"/>
在res/anim下创建,设置dialog窗口退出动画
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"/>
在res/values下创建color
#00000000
设置dialog窗口layout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/dialog_background" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="Hello" />
main布局
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击弹出myDialog" />