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

时间:2011-08-27 23:21:38

标签: java android multithreading scrollview slide

这是布局(XML)结构。

< RelativeLayout的>
<&的LinearLayout GT;
<的滚动型>
...< Linearlayout> ...< / Linearlayout>
< / ScrollView >

< /&的LinearLayout GT;
...(按钮)...
< / RelativeLayout的>

我试图让ScrollView滚动速度变慢(就像某种滑块一样),以便它滚动例如一个px而不是等待10毫秒并滚动另一个px ......直到它滚动100px。 如果我调用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){}
            }

        }
    });
}

我一直在ScrollView(我扩展它)和Activity中尝试Threads(像这样)。如果有任何方法让它慢慢移动我会很高兴知道。谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用Timer和TimerTask类并将scrollTo方法更新为1,直到达到100.

https://github.com/blessenm/SlideshowDemo

以上是连续滑块示例,其中显示了上述方法的使用。

答案 1 :(得分:0)

您是否尝试过使用smoothScrollBy