android 自定义dialog 带动画的

自定义dialog 带动画, 网上有些动画是用到ObjectAnimator 但是这是api11 以上才有的 2.3 系统用不了 要用还要用兼容包. 麻烦 我就写了xml文件 做了一个动画.

l

总是习惯性的 ctrl+ s 忘记这是网页了…

//这个是对话框工具类

package com.example.mydialog;



import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


public class FlowerDialogUtil {



    /**a * Create Dialog with Layout and theme * * @param context * Context * @param layoutId * Layout ID * @return Dialog */
    public Dialog createDialog(Activity context, int layoutId, int themeId) {
        Dialog ret;
        ret = new Dialog(context, themeId);
        ret.setContentView(layoutId);
        ret.setCancelable(true);
        return ret;
    }

    /** * 登录送花对话框 * * @param context * @return */
    public void showFlowerLoginDialog(Activity activity,String content) {
        Dialog ret;
        ret = new Dialog(activity,R.style.blend_theme_dialog);
        ret.setContentView(R.layout.dialog_item);
        TextView dialog_content = (TextView) ret.findViewById(R.id.dialog_tv);
        dialog_content.setText(content);
        ret.setCancelable(true);
        ret.show();
    }




}
//这个是调用
package com.example.mydialog;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private FlowerDialogUtil dialog;
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dialog= new FlowerDialogUtil();
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FlowerDialogUtil dialog = new FlowerDialogUtil();
                dialog.showFlowerLoginDialog(MainActivity.this,"啊发的发打发的司法的司法");

            }
        });
    }


}


<?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="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="400"/>  
</set>  




<?xml version="1.0" encoding="utf-8"?>
<!-- 退出时动画效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale  android:duration="400" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.0" android:toYScale="0.0" />

</set>

这是主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/out" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#999999" android:orientation="vertical" >

    <TextView  android:id="@+id/dialog_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:gravity="center" android:text="我来了" android:textColor="#888888" android:textSize="20sp" />

</LinearLayout>

加一个样式

    <style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
        <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
    </style>

    <style name="blend_theme_dialog" parent="@android:style/Theme.Dialog">
     <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowAnimationStyle">@style/dialogWindowAnim</item>
    </style>

http://download.csdn.net/detail/liudao7994/9108923 代码下载地址

你可能感兴趣的:(android)