使用相对布局交换两个图像无法正常工作

时间:2013-12-05 10:04:02

标签: android layout

这是基于手指移动的交换代码。我可以交换,但图像不替换位置。交换后的交换图像必须采用新的位置,动画必须再次设置。对于此代码,它将重定向到旧位置并第二次拍摄动画效果。

if (imageendX > imagestartX && imageendY < imagestartY) {

        if (imageendX - imagestartX < 60) {

        } else {


            TranslateAnimation tr = new TranslateAnimation(0,

            90, 0, 0);
            tr.setDuration(1000);
            // tr.setFillAfter(true);
            // tr.setFillEnabled(true);

            TranslateAnimation tr2 = new TranslateAnimation(0, -90, 0, 0);
            tr2.setDuration(1000);
            // tr2.setFillAfter(true);
            // tr2.setFillEnabled(true);


            currentView.startAnimation(tr);
            RightView.startAnimation(tr2);

            tr.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            tr2.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {


                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

        }


    } else if (imageendY < -10) {


        TranslateAnimation tr = new TranslateAnimation(0, 0, 0, -90);
        tr.setDuration(1000);
        // tr.setFillAfter(true);

        TranslateAnimation tr2 = new TranslateAnimation(0, 0, 0,90);
        tr2.setDuration(1000);
        // tr2.setFillAfter(true);

        currentView.startAnimation(tr);
        TopView.startAnimation(tr2);

        tr2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                android.widget.RelativeLayout.LayoutParams pr = (android.widget.RelativeLayout.LayoutParams) TopView
                        .getLayoutParams();
                // pr.topMargin += 90;
                pr.topMargin += 90;
                TopView.setLayoutParams(pr);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        tr.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                android.widget.RelativeLayout.LayoutParams pr = (android.widget.RelativeLayout.LayoutParams) currentView
                        .getLayoutParams();
                // pr.topMargin += 90;
                pr.bottomMargin += 90;
                currentView.setLayoutParams(pr);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    } else if (imageendY > imagestartY && imageendX < imagestartX) {

        if (imageendY - imagestartY < 60) {

        } else {



            TranslateAnimation tr = new TranslateAnimation(0, 0, 0, RM);
            tr.setDuration(1000);
            // tr.setFillAfter(true);

            TranslateAnimation tr2 = new TranslateAnimation(0, 0, 0, -RM);
            tr2.setDuration(1000);
            // tr2.setFillAfter(true);

            currentView.startAnimation(tr);
            BottomView.startAnimation(tr2);

            tr2.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {


                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            tr.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {


                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }

    } else if (imagestartX >= imageendX) {
        if (imagestartX - imageendX < 90) {
        } else {
            System.out.println("right to left");
            TranslateAnimation tr = new TranslateAnimation(0, RM, 0, 0);
            tr.setDuration(1000);
            tr.setFillAfter(true);

            TranslateAnimation tr2 = new TranslateAnimation(0, -RM, 0, 0);
            tr2.setDuration(1000);
            tr2.setFillAfter(true);

            LeftView.startAnimation(tr);
            currentView.startAnimation(tr2);

            tr2.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            tr.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });


        }
        // }
    }

1 个答案:

答案 0 :(得分:1)

使用View Animation视图将在动画后的原始位置,因此根据您的要求使用Property Animation视图将在动画后的动画位置,但唯一的缺点是属性动画支持API 11

没问题......使用NineOldAndroids来克服这个问题..

包含两个图片视图的示例代码

imageOne和imageTwo之间的动画

ObjectAnimator imageOneAnimator = ObjectAnimator.ofFloat(
                    imageOne, "X", imageOne.getX(), imageTwo.getX());
ObjectAnimator imageTwoAnimator = ObjectAnimator.ofFloat(
                    imageTwo, "X", imageTwo.getX(), imageOne.getX());
imageOneAnimator.setDuration(1000);
imageTwoAnimator.setDuration(1000);
AnimatorSet set = new AnimatorSet();
set.playTogether(imageOneAnimator, imageTwoAnimator);
set.start();

imageThree和imageOne之间的动画

ObjectAnimator imageOneYAnimator = ObjectAnimator.ofFloat(
                    imageOne, "Y", imageOne.getY(), imageThree.getY());
ObjectAnimator imageThreeYAnimator =      ObjectAnimator.ofFloat(
                    imageThree, "Y", imageThree.getY(), imageOne.getY());
imageOneYAnimator.setDuration(1000);
imageThreeYAnimator.setDuration(1000);
AnimatorSet setY = new AnimatorSet();
setY.setStartDelay(1000);
setY.playTogether(imageOneYAnimator, imageThreeYAnimator);
setY.start();

您可以根据计算在触控侦听器中启动动画