欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中从屏幕左下角弹出Dialog动画效果的实现代码

程序员文章站 2024-02-22 21:04:40
mainactivity代码: import android.app.dialog; import android.os.bundle; import and...

mainactivity代码:

import android.app.dialog;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.window;
import android.view.windowmanager;
public class mainactivity extends appcompatactivity {
private view inflate;
private dialog dialog;
@override
protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
}
public void click(view view) {
 dialog = new dialog(this,r.style.actiondialogstyle);
 //填充对话框的布局
 inflate = layoutinflater.from(this).inflate(r.layout.item_dialog, null);
 //初始化控件
 //将布局设置给dialog
 dialog.setcontentview(inflate);
 //获取当前activity所在的窗体
 window dialogwindow = dialog.getwindow();
 //设置dialog从窗体底部弹出
 dialogwindow.setgravity( gravity.bottom);
 //获得窗体的属性
 windowmanager.layoutparams lp = dialogwindow.getattributes();
 lp.y = 20;//设置dialog距离底部的距离
 lp.width = windowmanager.layoutparams.match_parent;
//  将属性设置给窗体
 dialogwindow.setattributes(lp);
 dialog.show();//显示对话框
 }
}

主界面的布局:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context="ucai.cn.dialoganimator.mainactivity">
<textview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="hello world!"
 android:onclick="click"/>
</linearlayout>

dialog布局:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<imageview
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:src="@drawable/hani_gift_1"
 android:layout_gravity="center"/>
</linearlayout>

styles代码:

 <style name="actiondialogstyle" parent="@android:style/theme.dialog">
   
<!-- 背景透明 -->
 <item name="android:windowbackground">@android:color/transparent</item>
 <item name="android:windowcontentoverlay">@null</item>
 <!-- 浮于activity之上 -->
 <item name="android:windowisfloating">true</item>
 <!-- 边框 -->
 <item name="android:windowframe">@null</item>
 <!-- dialog以外的区域模糊效果 -->
 <item name="android:backgrounddimenabled">true</item>
 <!-- 无标题 -->
 <item name="android:windownotitle">true</item>
 <!-- 半透明 -->
 <item name="android:windowistranslucent">true</item>
 <!-- dialog进入及退出动画 -->
 <item name="android:windowanimationstyle">@style/actionsheetdialoganimation</item>
</style>
<!-- actionsheet进出动画 -->
<style name="actionsheetdialoganimation" parent="@android:style/animation.dialog">
 <item name="android:windowenteranimation">@anim/actionsheet_dialog_in</item>
 <item name="android:windowexitanimation">@anim/actionsheet_dialog_out</item>
</style>

进入动画(左下角开始放大):

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromxscale="0"
android:toxscale="100%"
android:fromyscale="0"
android:toyscale="100%"
android:pivotx="0%"
android:pivoty="100%"
/>

退出动画(向下移动):

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromydelta="0"
android:toydelta="100%" />

以上所述是小编给大家介绍的android 中从屏幕左下角弹出dialog动画效果的实现代码,希望对大家有所帮助