Bharata
Bharata

Reputation: 747

Scrolling image in android

i write some code like this to scrolling image automatically:

scroll=(ImageView)findViewById(R.id.pesancredit);
    Thread t = new Thread(){
        public void run(){
            int y = scroll.getScrollY();
            int x = scroll.getScrollX();
            while(y<1600){
                scroll.scrollTo(x, y);
                y++;
                try {
                    sleep(1000/12);
                } catch (InterruptedException e) {
                }
            }
        }
    };
    t.start();

But, it doesn't work. Can anyone help me please?

Upvotes: 1

Views: 2683

Answers (2)

CDub
CDub

Reputation: 61

You could animate the image with something like this:

res/anim/slide_out_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0" 
        android:toXDelta="-300%p"
        android:duration="6000"/>
</set>

then in your activity:

scroll=(ImageView)findViewById(R.id.pesancredit);
scroll.setImageBitmap(yourImage);
Animation slideOutLeft = AnimationUtils.LoadAnimation(this, R.anim.slide_outleft);
scroll.startAnimation(slideOutLeft);

check out this. http://developer.android.com/guide/topics/resources/animation-resource.html

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234797

You need to call the scrollTo method on the UI thread. To do this, you need to use a handler. Something like this should work:

// declare a class field:
final Handler h = new Handler();

// later:
scroll=(ImageView)findViewById(R.id.pesancredit);
Thread t = new Thread(){
    public void run(){
        int y = scroll.getScrollY();
        int x = scroll.getScrollX();
        while(y<1600){
            // need final values to create anonymous inner class
            final int X = x;
            final int Y = y;
            h.post(new Runnable() {
                public void run() {
                    scroll.scrollTo(X, Y);
                }
            });
            y++;
            try {
                sleep(1000/12);
            } catch (InterruptedException e) {
            }
        }
    }
};
t.start();

Upvotes: 1

Related Questions