如何设置imageView的背面?

时间:2011-11-25 09:10:39

标签: android android-layout imageview android-animation

我有一个困扰我一段时间的问题。

我有一个我动画的imageView(在X轴和Y轴上移动,缩放和旋转),它位于RelativeLayout内。我的动画应该翻转imageView并缩放它,以便用imageView的背面纯色填充屏幕。想象一下,如果贴纸的背面有一个坚实的灰色,我希望能够在翻转贴纸时看到这种纯色。

问题在于,无论我做什么,我都看不到那种纯色。

我尝试设置imageView的背景颜色,但它不起作用。 我尝试在父Relativelayout中创建另一个布局,然后设置布局的背景颜色,但它不起作用。 我尝试设置父RelativeLayout的背景颜色,但它不起作用。

我有点陷入困境,所以我真的很想知道你能提供的任何帮助或想法。

感谢。

更新

以下是我正在做的事情:

int[] location = {0, 0};
vg1.getLocationOnScreen(location);

newViewWidth = vg1.getRight() - vg1.getLeft();
newViewHeight = vg1.getBottom() - vg1.getTop();

newViewCenterX = newViewWidth/2;
newViewCenterY = newViewHeight/2;

screenWidth = root.getRight() - root.getLeft();
screenHeight = root.getBottom() - root.getTop();

ratioX = (float) screenWidth/newViewWidth;
ratioY = (float) screenHeight/newViewHeight;

rootCenterX = screenWidth/2;
rootCenterY = screenHeight/2;

newView = mInflater.inflate(R.layout.item, null);
LayoutParams lp = new LayoutParams(newViewWidth, newViewHeight);
lp.setMargins(10, 10, 10, 10);
newView.setLayoutParams(lp);

vg1.setVisibility(View.INVISIBLE);

root.addView(newView);

newView.bringToFront();

Log.d(tag, "View vg1 is at " + location[0] + ", " + location[1]);
Log.d(tag, "View vg1 is at top:" + vg1.getTop() + ", left:" + vg1.getLeft() + ", bottom:" + vg1.getBottom() + ", right:" + vg1.getRight());
Log.d(tag, "View vg1 has width " + newViewWidth + " and height " + newViewHeight + " and is centered at X " + newViewWidth/2 + " and Y " + newViewHeight/2);

root.getLocationOnScreen(location);
Log.d(tag, "Root is at " + location[0] + ", " + location[1]);
Log.d(tag, "Root is at top:" + root.getTop() + ", left:" + root.getLeft() + ", bottom:" + root.getBottom() + ", right:" + root.getRight());
Log.d(tag, "Root has width " + screenWidth + " and height " + screenHeight + " and is centered at X " + rootCenterX + " and Y " + rootCenterY);

Log.d(tag, "Root holder center X is at " + (root.getRight() - root.getLeft())/2 + ".");
Log.d(tag, "Root holder center Y is at " + (root.getBottom() - root.getTop())/2 + ".");


Log.d(tag, "RatioX: " + ratioX + ", ratioY: " + ratioY);

ObjectAnimator moveX = ObjectAnimator.ofFloat(newView, "translationX", 0f, rootCenterX - newViewCenterX).setDuration(2000);
ObjectAnimator moveY = ObjectAnimator.ofFloat(newView, "translationY", 0f, rootCenterY - newViewCenterY - 10).setDuration(2000);

ObjectAnimator scaleX = ObjectAnimator.ofFloat(newView, "scaleX", 1f, ratioX).setDuration(2000);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(newView, "scaleY", 1f, ratioY).setDuration(2000);

ObjectAnimator rotateX = ObjectAnimator.ofFloat(newView, "rotationX", 0f, -90f).setDuration(2000);
ObjectAnimator rotateY = ObjectAnimator.ofFloat(newView, "rotationY", 0f, -90f).setDuration(2000);
rotateX.addListener(new AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {
        newView.findViewById(R.id.itemImage).setVisibility(View.GONE);
        newView.findViewById(R.id.colorfulBackground).setVisibility(View.VISIBLE);

        ObjectAnimator rotateX = ObjectAnimator.ofFloat(newView, "rotationX", -90f, -180f).setDuration(2000);
        ObjectAnimator rotateY = ObjectAnimator.ofFloat(newView, "rotationY", -90f, -180f).setDuration(2000);

        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(rotateX, rotateY);

        animSet.start();
    }

    @Override
    public void onAnimationCancel(Animator animation) {}
});

AnimatorSet animSet = new AnimatorSet();
animSet.playTogether(moveX, moveY, scaleX, scaleY, rotateX, rotateY);

animSet.start();

在aNi的建议之后,似乎可以解决这个问题。

现在还有另一个问题。几乎在旋转的中间,视图被剪切一瞬间然后恢复正常。

你知道它可能导致什么吗? 我想它可能是窗户的可见平面或类似的东西,但我不知道我应该寻找什么。也许具有动画视图父级布局属性的东西?

1 个答案:

答案 0 :(得分:1)

以下是flip3d的示例,提供了两个不同视图之间的3D翻转动画交换功能 - 后视图和前视图。 http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html