Walter isler
Walter isler

Reputation: 3

How can i programatically move a view in android?

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 position, 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!

Upvotes: 0

Views: 685

Answers (2)

blessanm86
blessanm86

Reputation: 31779

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.

Upvotes: 2

aromero
aromero

Reputation: 25761

Have you tried using smoothScrollBy?

Upvotes: 0

Related Questions