使用ObjectAnimator翻译视图,但不起作用

时间:2018-10-07 14:51:25

标签: android objectanimator

我正在使用以下代码将imageview转换为屏幕上特定的x和y点。

private void makeCircle() {
        ObjectAnimator cornerAnimation =
                ObjectAnimator.ofFloat(gradientDrawable, "cornerRadius", 500.0f);

        Animator shiftAnimation = AnimatorInflater.loadAnimator(this, R.animator.slide_up_left);
        shiftAnimation.setTarget(mParent);
        Log.d("TESTING","oy : "+oy);
        ObjectAnimator oax = ObjectAnimator.ofFloat(mParent, "x", ox);
        ObjectAnimator oay = ObjectAnimator.ofFloat(mParent, "y", oy);
        ValueAnimator anim = ValueAnimator.ofInt(ch, oh);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                layoutParams.height = val;
                imageView.setLayoutParams(layoutParams);
                layoutParams = mParent.getLayoutParams();
                layoutParams.height = val;
                mParent.setLayoutParams(layoutParams);
            }
        });
        ValueAnimator anim2 = ValueAnimator.ofInt(cw, ow);
        anim2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = mParent.getLayoutParams();
                layoutParams.width = val;
                mParent.setLayoutParams(layoutParams);
                layoutParams = imageView.getLayoutParams();
                layoutParams.width = val;
                imageView.setLayoutParams(layoutParams);
            }
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(cornerAnimation, shiftAnimation,oax,oay,anim,anim2);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                    public void onGlobalLayout() {
                        int ch = imageView.getHeight();
                        int cw = imageView.getWidth();
//                x = imageView.getLeft();
//                y = imageView.getTop();
                        imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        int[] locations = new int[2];
                        imageView.getLocationOnScreen(locations);
                        int cx = locations[0];
                        int cy = locations[1];
                        // don't forget to remove the listener to prevent being called again
                        // by future layout events:
                        Log.d("TEST2", "cx:"+cx+",cy:"+cy+",cw:"+cw+",ch:"+ch);
                    }
                });
//                dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
        Log.d("VIEWPROFILEPICTUREACT", "ox:"+ox+",oy:"+oy+",ow:"+ow+",oh:"+oh);
        Log.d("VIEWPROFILEPICTUREACT", "cx:"+cx+",cy:"+cy+",cw:"+cw+",ch:"+ch);
    }

我在“ TEST2”日志和其他日志中获得的值如下所示:

测试:oy:276 VIEWPROFILEPICTUREACTACT:ox:62,oy:276,ow:197,oh:199 VIEWPROFILEPICTUREACT:cx:277,cy:666,cw:525,ch:525 TEST2:cx:63,cy:340,cw:197,ch:199

为什么我要在代码中要求将其转换为276 y,但仅增加到340?

0 个答案:

没有答案