开发者

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) {
                }
            }
  开发者_StackOverflow      }
    };
    t.start();

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


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();


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜