简体   繁体   English

我如何以编程方式在android中移动视图?

[英]How can i programatically move a view in android?

Here's the layout (XML) structure. 这是布局(XML)结构。

<RelativeLayout> <RelativeLayout的>
<Linearlayout> <的LinearLayout>
< ScrollView > < ScrollView >
...<Linearlayout>...</Linearlayout> ... <的LinearLayout> ... </的LinearLayout>
</ ScrollView > </ ScrollView >
</Linearlayout> </的LinearLayout>
...(Buttons)... ...(纽扣)...
</RelativeLayout> </ RelativeLayout的>

I am trying to make the ScrollView scroll slow ( like some kind of slider ) so that it scrolls for example one px than waits for 10 milliseconds and scrolls another px ... until it has scrolled by 100px. 我试图使ScrollView滚动缓慢(像某种滑块),以便它滚动例如一个px而不是等待10毫秒并滚动另一个px ...直到它滚动100px。 If i call scrollBy(100,0) (eg) it just switches to the specified position, but theres no visible movement in between. 如果我调用scrollBy(100,0)(例如)它只是切换到指定位置,但两者之间没有可见的移动。

public void move(int x, int y)
{
    Activity context = (Activity)getContext();

    context.runOnUiThread(new Runnable() 
    {   
        @Override
        public void run() 
        {
            for(int i=0;i<100;i++)
            {
                scrollBy(-1,0); try{ Thread.sleep(20); } catch(Exception e){}
            }

        }
    });
}

I've been trying Threads (like this) in the ScrollView ( i extended it) and the Activity. 我一直在ScrollView(我扩展它)和Activity中尝试Threads(像这样)。 If there's any way to make it move slowly i'd be glad to know. 如果有任何方法让它慢慢移动我会很高兴知道。 Thanks! 谢谢!

You could use the Timer and TimerTask class and update scrollTo method by 1 until you reach 100. 您可以使用Timer和TimerTask类并将scrollTo方法更新为1,直到达到100。

https://github.com/blessenm/SlideshowDemo https://github.com/blessenm/SlideshowDemo

The above is a continuous slider example which shows the use of the above methods. 以上是连续滑块示例,其示出了上述方法的使用。

你尝试过使用smoothScrollBy吗?

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

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