简体   繁体   English

向左动画视图

[英]Animate a view towards left

I'm using the following code to expand a view with an animation: 我正在使用以下代码来扩展带有动画的视图:

public class HorizontallyAnimate extends Animation {

    private int  toWidth;
    private int  startWidth;
    private View view;
    private String TAG = HorizontallyAnimate.class.getSimpleName();
    private int newWidth;
    public HorizontallyAnimate(View view) {
        this.view = view;
        this.startWidth = this.view.getWidth();
        this.toWidth = (this.startWidth == view.getHeight() ? this.startWidth * 4 : view.getHeight());

        Log.d(TAG,"Start width" + this.startWidth);
        Log.d(TAG,"view hieght " + view.getHeight());

    }

    protected void applyTransformation(float interpolatedTime, Transformation t) {
        newWidth = this.startWidth + (int) ((this.toWidth - this.startWidth) * interpolatedTime);
        this.view.getLayoutParams().width = newWidth;
        this.view.requestLayout();
    }

}

The above code animates the view from left to right when the width changes. 上面的代码在宽度改变时从左到右动画化视图。

But, I'm trying to animate it from the right to left. 但是,我正在尝试从右到左对其进行动画处理。 In other words, the width should grow in opposite direction. 换句话说,宽度应沿相反的方向增长。 How can I be able to do so? 我该怎么做?

The problem you're dealing with here is an anchoring issue. 您在这里处理的问题是一个锚定问题。 A view's anchor (or pivot point) determines which point on the view stays still when other parts change. 视图的锚点(或枢轴点)确定其他部分发生更改时视图上的哪个点保持静止。

A view's anchor when adjusting its dimensions highly depends on how the view is laid out. 调整视图尺寸时,其锚点高度取决于视图的布局方式。 Since you didn't supply any information on how your view is laid out in the code you posted, I'll assume from the problem you're experiencing that the view's horizontal anchor is its left side. 由于您没有在发布的代码中提供有关视图布局的任何信息,因此从您遇到的问题来看,我认为该视图的水平锚点位于其左侧。

This anchoring issue would produce a growth which would cause the left-most side to stay still, while the right side expands right-wards. 此锚定问题将产生增长,这将导致最左侧保持静止,而右侧向右扩展。

Making the view's left side to expand leftwards while the right side stays still can be achieved in several ways. 可以通过几种方法使视图的左侧向左扩展而右侧保持不动。 One way would be to alter the way the view is laid out in its parent (ie, if the parent is a RelativeLayout , set the view to alignParentRight=true , or playing with gravity in other containers). 一种方法是更改​​视图在其父级中的布局方式(即,如果父级是RelativeLayout ,则将视图设置为alignParentRight=true ,或者在其他容器中使用gravity进行播放)。

However, since you didn't specify how the view is laid out, I will give you a solution which does not make any assumptions on its container. 但是,由于您没有指定视图的布局方式,因此我将为您提供一个不对其容器进行任何假设的解决方案。 This solution isn't perfect as it may cause some stuttering, but it should still achieve what you're trying to do. 该解决方案并不完美,因为它可能会引起一些卡顿现象,但是它仍然可以实现您要执行的操作。

In your applyTransformation method, you will need to compensate for the right growth by translating leftwards. 在您的applyTransformation方法中,您需要通过向左平移来补偿正确的增长。 You can compensate for this by using translationX : 您可以使用translationX对此进行补偿:

protected void applyTransformation(float interpolatedTime, Transformation t) {

        // hold the change in a separate variable for reuse.
        int delta = (int) ((this.toWidth - this.startWidth) * interpolatedTime);

        newWidth = this.startWidth + delta;
        this.view.getLayoutParams().width = newWidth;

        // shift the view leftwards so that the right side appears not to move.
        // shift amount should be equal to the amount the view expanded, but in the
        // opposite direction.
        this.view.setTranslationX(-delta); 

        this.view.requestLayout();
    }

As you can see, this is a bit of "trick". 如您所见,这有点“技巧”。 While the view is expanding to the right, we are moving it to the left at the exact same ratio which causes the illusion of the view expanding to the left. 当视图向右扩展时,我们以完全相同的比例将其向左移动,这导致视图向左扩展的错觉。

Test this code and see if it works for you. 测试此代码,看看它是否对您有用。 I would also recommend seeing if you can play around with the view's alignment or gravity from within its container. 我还建议您查看是否可以从其容器中浏览视图的对齐方式或重力。 Doing this would solve your issue in a more standard manner, ie, without any "tricks". 这样做将以更标准的方式解决您的问题,即没有任何“技巧”。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM