如何设置 imageView 的背面?

发布于 2024-12-17 20:26:19 字数 3912 浏览 4 评论 0原文

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

我有一个要设置动画的 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的建议,它似乎成功了。

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

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

I have a problem which bothers me for some time.

I have an imageView that I animate (move, scale and rotate on the X and Y axises) and it is inside a RelativeLayout. My animation is supposed to flip the imageView and scale it so that it will fill the screen with the imageView's back-side solid color. Imagine something like having a sticker that has a solid grey color on its back side and that I want to be able to see this solid color when I flip the sticker.

The problem is that whatever I do, I can't see that solid color.

I tried setting the background color of the imageView but it didn't work.
I tried creating another Layout inside the parent Relativelayout and then setting the Layout's background color, but it didn't work.
I tried setting the background color of the parent RelativeLayout, but it didn't work.

I am kinda stuck so I would really aprreciate any help or ideas that you can provide.

Thanks.

Update

Here is what I'm doing:

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();

After aNi's suggestion, it seems to do the trick.

There is another problem now though. Almost at the middle of the rotation, the view gets clipped for a split second and then comes back to normal.

Do you have any idea what it might be causing it ?
I am thinking that it might be the visible plane of the window or something similar but I don't know what I should look for. Maybe something with the properties of the parent layout of the animated view ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2024-12-24 20:26:19

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

Here is an example of flip3d, provides capability of 3D flip- animated swap between two different views - back and front view. http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文