如何正确移动视图? [英] How to properly move a view?

查看:16
本文介绍了如何正确移动视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向菜鸟解释动画视图的正确方法,使其触摸区域和图像实际一起移动吗?!

Can someone please explain to a noob the correct way to animate a View so its touch area and image actually move together?!

我已经阅读了很多帖子、问题和教程,但没有一个解释是什么移动了布局以及是什么移动了图像,以便我可以为视图设置动画,然后将其留在新位置.

I have read lots of posts and questions and tutorials, but none explains what moves the layout and what moves the image such that I can animate a view and then leave it at its new position.

这是我正在使用的一个示例方法,尝试了许多不同的组合但没有成功.该视图位于父RelativeLayout 中.它是一个可触摸的图标菜单,并带有一个 xml 资源动画,单击鼠标滑出屏幕,只留下一个小选项卡显示,它需要停留在那里直到再次单击.

This is an example method I'm working with, trying lots of different combinations to no success. The view is in the parent RelativeLayout. It's a touchable menu of icons, and is animated with an xml resource on a click to slide off screen leaving just a little tab showing, where it needs to stay until clicked again.

public void RetractTools (View v){      
    final ImageView finalImage1 = (ImageView) findViewById(R.id.paintsView);
    Animation slideout = AnimationUtils.loadAnimation(this, R.anim.slideout_tools);
    slideout.setFillAfter(true);
    slideout.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
            finalImage1.setEnabled(true);
            optionMenu.showing = false;
            optionMenu.inMotion = false;
            finalImage1.layout(1258, 668, 1697, 752);
            finalImage1.setRight(1280);
            finalImage1.invalidate();
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
        }

        @Override
        public void onAnimationStart(Animation arg0) {
            finalImage1.setEnabled(false);
        }
    });

    optionMenu.inMotion = true;
    v.startAnimation(slideout);
 }// End RetractMenu

无论我尝试什么,都会遇到问题.setFillAfter 在 xml 文件中设置时不执行任何操作.以编程方式设置,它将图像保留在正确的位置,但触摸控件保留在菜单所在的位置.我试过 setLeft 和 setRight 显然只移动图像,而不是视图位置,以及各种不同的布局选项,填充和不填充和无效,但不能解决它.我显然不了解定位和渲染视图所需的底层机制!:D

No matter what I try, I encounter problems. setFillAfter does nothing when set in the xml file. Set programmatically, it leaves the image in the right place but the touch controls remain where the menu was. I have tried setLeft and setRight which apparently only move the image, not the view position, and all sorts of different layout options, and fill and no fill and invalidating and not, but can't solve it. I clearly don't undersatnd the underlying mechanics needed to position and render a view! :D

谢谢.

解决方案

对于任何有类似问题的人,这就是我发现使用相对布局的方式.您创建一个具有指定大小的 LayoutParams 对象,然后您可以为其分配位置.例如.

For anyone having similar issues, this is how I have found to work with relative layouts. You create a LayoutParams object with the specified size, and then you can assign it positions. eg.

    final RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);           
    position.leftMargin = 440;

然后将其分配给您的视图

Then assign that to your view

    myView.setLayoutParams(position);

总而言之,您使用 LayoutParams 对象作为视图位置的接口,而不是像我假设的那样直接访问视图的坐标.

So in summary, you use a LayoutParams object as an interface to your view's position, rather than accessing the view's coordinates directly as I assumed.

推荐答案

你的基本没问题,有两个缺陷:

What you have is basically fine, with two flaws:

  1. 您正在使用 setFillAfter(),这不是特别有用

你正在调用 layout()setRight() 之类的东西,这不是特别有效

You are calling layout() and setRight() and stuff, which is not especially effective

相反,在 onAnimationEnd() 中,您需要修改 ViewLayoutParams 以反映您希望小部件的新位置小部件的大小和位置由它与其容器协商的布局规则决定.最初,这些是通过您的布局 XML 资源设置的.通过在运行时修改 LayoutParams,您正在更改这些规则.

Instead, in onAnimationEnd(), you need to modify the LayoutParams of the View to reflect the new position you want the widget to be in. The size and position of a widget is dictated by the layout rules it negotiates with its container. Initially, those are set via your layout XML resource. By modifying the LayoutParams at runtime, you are changing what those rules are.

那些 LayoutParams 是什么(LinearLayout.LayoutParamsRelativeLayout.LayoutParams 等)以及你应该在它们中指定什么值,我们不能告诉你,因为我们不知道你在做什么.

What those LayoutParams are (LinearLayout.LayoutParams, RelativeLayout.LayoutParams, etc.) and what values you should specify in them, we cannot tell you, because we don't know what you are doing.

这篇关于如何正确移动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆