开发者

How can i programatically move a view in android?

开发者 https://www.devze.com 2023-03-31 10:40 出处:网络
Here\'s the layout (XML) structure. <RelativeLayout>       <Linearlayout>

Here's the layout (XML) structure.

<RelativeLayout>

      <Linearlayout>

           <ScrollView>

            ...<Linearlayout>...</Linearlayout>

           </ScrollView>

      </Linearlayout>

     ...(Buttons)...

</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. If i call scrollBy(100,0) (e.g.) it just switches to the specified positi开发者_StackOverflowon, but theres no visible movement in between.

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. 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.

https://github.com/blessenm/SlideshowDemo

The above is a continuous slider example which shows the use of the above methods.


Have you tried using smoothScrollBy?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号