Android dialog上动画的问题

问题描述

Android dialog上动画的问题

Activity上有ListView,点击ListView的Item会弹出dialog,点击dialog上的+号演示一个小球从+号飞到Activity底部的购物车的动画,想了好几天无法实现这个动画,要么这个动画是在dialog的后面,要么这个动画只能在dialog的范围内,求大神赐教!谢谢!!
下面贴我的代码

 public class MainActivity extends Activity implements OnClickListener {

    private Button bt;
    private TextView cart;
    private Button add;
    private ImageView photoImageView;
    private Dialog selectDialog;

    private int[] foodAddLocation = new int[2];
    private int[] cartLocation = new int[2];

    private ViewGroup animLayout;//动画层

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
        cart = (TextView) findViewById(R.id.cart);

    }

    public void showSelectDialog() {

        View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);

        ImageView close = (ImageView)view.findViewById(R.id.select_close);
        close.setOnClickListener(this);
        add = (Button)view.findViewById(R.id.add);//TODO
        add.setOnClickListener(this);

        TextView nameView = (TextView)view.findViewById(R.id.food_name);
        nameView.setText("哈哈哈");

        photoImageView = (ImageView)view.findViewById(R.id.photo);
        photoImageView.setImageResource(R.drawable.ic_food_none);

        selectDialog = new Dialog(this, R.style.transparentFrameWindowStyle);
        selectDialog.setContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        Window window = selectDialog.getWindow();
        WindowManager windowManager = this.getWindowManager();
        // 设置显示动画
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
        // 设置显示位置
        selectDialog.onWindowAttributesChanged(wl);

        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int)(display.getWidth()*0.8); //设置宽度
        selectDialog.getWindow().setAttributes(lp);

        // 设置点击外围关闭
        selectDialog.setCanceledOnTouchOutside(true);
        selectDialog.show();

    }

    private void addToCartAnimation(final ImageView ball, boolean isDialog) {
        animLayout = null;
        animLayout = createAnimLayout(isDialog);

        final View view = addViewToAnimLayout(animLayout, ball, foodAddLocation);

        cart.getLocationInWindow(cartLocation);
        // 计算位移
        int endX = 0 - foodAddLocation[0] + cart.getWidth();
        int endY = cartLocation[1] - foodAddLocation[1] - cart.getHeight()*3;

        TranslateAnimation translateAnimationX = new TranslateAnimation(-cart.getHeight(),
                endX+cart.getHeight(), 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
                -cart.getHeight()*3, endY-cart.getHeight());
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);
        translateAnimationY.setFillAfter(true);

        AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(300);
        view.startAnimation(set);

        set.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                ball.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ball.setVisibility(View.GONE);
            }
        });

    }

    private ViewGroup createAnimLayout(boolean isDialog) {
        Window window;
        if(isDialog){
            window = selectDialog.getWindow();
        }else{
            window = this.getWindow();
        }
        ViewGroup rootView = (ViewGroup) window.getDecorView();
        LinearLayout animLayout = new LinearLayout(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }

    private View addViewToAnimLayout(final ViewGroup animLayout, final View ball,
            int[] foodAddLocation) {
        int x = foodAddLocation[0];
        int y = foodAddLocation[1];
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = x;
        lp.topMargin = y;
        ball.setLayoutParams(lp);
        animLayout.addView(ball);
        return ball;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.bt:
            showSelectDialog();
            break;
        case R.id.add:
            add.getLocationInWindow(foodAddLocation);
            ImageView ball = new ImageView(this);
            ball.setImageResource(R.drawable.badge_ifaux);
            addToCartAnimation(ball, true);
            break;
        default:
            break;
        }
    }

}

解决方案

createAnimLayout(boolean isDialog)方法中用window = selectDialog.getWindow();时,动画只在Dialog的范围内,用window = this.getWindow();时,动画在Dialog的后面

时间: 2024-08-07 23:42:16

Android dialog上动画的问题的相关文章

tablet-在 android 平板上的动画显示错误

问题描述 在 android 平板上的动画显示错误 我开发一个android程序,开始于启动画面.程序在模拟器和真机上可以运行,但是平板上不行. package com.example.project; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Window; import an

android 自定义dialog,窗口动画

http://www.apkbus.com/android-17050-1-1.html 自定义dialog窗口,根据坐标可随意设置dialog显示位置,实现了窗口弹出动画 Java代码: package com.sunxu.org.IndividualityDialog; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.os.Bundle

点击android模拟器上的menu键如何直接出现一个dialog

问题描述 点击android模拟器上的menu键如何直接出现一个dialog 点击android模拟器上的menu键如何直接出现一个dialog,是要给menu按钮设置监听吗,如果是,android系统中的id哪个代表模拟器上的menu 解决方案 http://zhidao.baidu.com/link?url=O5-EAphVev_lnmEOIU99pyfO54Fr61GKZaiEdBfpmy6y4Qe_XYmYnIgZScWGNMi40WgAfCp-s3lL-LR3BfS_jq 解决方案二:

Android Dialog 动画实例详解

Android Dialog 动画实例详解 动画描述: 动画与底部菜单一样出现和消失 制作过程: 1. 创建两个动画文件 window_in.xml: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration=&

Android Activity进出动画三种方法

Android Activity进出动画三种方法 实现activity的进出场动画总共有3种方式,下面会一一列出,首先给出示例的动画xml文件. 动画的xml文件 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="

java-下面是两张图片,就是点击编辑改变css样式,怎样实现,还要再dialog上点击,实现删除

问题描述 下面是两张图片,就是点击编辑改变css样式,怎样实现,还要再dialog上点击,实现删除 # 解决方案 你下面的标签用div容器float布局,div relative定位,那个xx按钮absolute定位,默认隐藏.点击编辑的时候新增一个样式给div容器,这个样式控制xx的显示. 大概demo如下,居于jquery的,那个xx的样式你自己调整了 <script type="text/javascript" src="http://ajax.aspnetcdn

[译] 如何创建 BubblePicker – Android 多彩菜单动画

本文讲的是[译] 如何创建 BubblePicker – Android 多彩菜单动画, 原文地址:How We Created BubblePicker – a Colorful Menu Animation for Android 原文作者:Irina Galata, Yuliya Serbenenko 译文出自:掘金翻译计划 译者:hackerkevin 校对者:luoqiuyu phxnirvana 如何创建 BubblePicker – Android 多彩菜单动画 我们已经习惯了移动

android 巧用动画使您app风骚起来

巧用Android的自定义动画,使你更加的有动感,是大多数Android开发人员的目标,那怎么做到这点.请听下文分解: 3.0以前,android支持两种动画模式,tween animation(幅间动画),frame animation(帧动画),在android3.0中又引入了一个新的动画系统:property animation(值动画),这三种动画模式在SDK中被称为property animation,view animation,drawable animation. 可通过Nine

Android Dialog对话框用法实例详解_Android

本文实例讲述了Android Dialog对话框用法.分享给大家供大家参考,具体如下: Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int),dismissDialog(int)等方法,如果使用这些方法的话,Activity将通过getOwnerActivity()方法返回该Activity管理的对话框(dialog). 1. onCreat