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

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

问题描述

有人可以解释一个菜鸟的正确方法,以动画视图所以它的触摸区域和图像真正动起来呢?!

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这显然仅移动图像,而不是视图位置,以及各种不同的布局选项,并填写,没有填充和无效而不得,但解决不了它。我显然不undersatnd需要位置的底层机制和渲染视图! :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

感谢。

编辑:解

EDIT : Solution

对于有类似问题的人,这是怎么了,我发现,相对布局工作。创建具有指定大小的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(),这是不是特别有用

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

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

相反, onAnimationEnd(),你需要修改的的LayoutParams 查看来反映你想要的部件是在新的位置。一个小部件的大小和位置由它与它的容器协商布局规则所决定的。最初,这些是通过你的布局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.LayoutParams RelativeLayout.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天全站免登陆