Search code examples
androidandroid-imageviewandroid-scrollview

Android Autoscroll imageview


In my activity I have just an ImageView. In it, the src is a picture that is a lot bigger than the screen. I want the picture to scroll slooooowly from left to right until it reaches the right margin of the photo, then start scrolling back towards left until left margin reached. Then start all over again. I need it to happen in a separate thread so the phone would not freeze while this happens. This Is What I need

How can I achieve this? Is there a widget that does this by default?

UPDATED CODE // layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mylinear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/rainforest" />

</RelativeLayout>

// and Activity

public class MainActivity extends Activity {

    Animation           _translateAnimation;
    RelativeLayout        _relativeLoading = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, -100f, TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f);
        _translateAnimation.setDuration(5000);
        _translateAnimation.setRepeatCount(-1);
        _translateAnimation.setRepeatMode(Animation.REVERSE); // REVERSE
        _translateAnimation.setInterpolator(new LinearInterpolator());
        _relativeLoading = (RelativeLayout) findViewById(R.id.mylinear);
        _relativeLoading.startAnimation(_translateAnimation);
    }

But it goes off the picture. I mean, the scroll goes from left to right, "pushing" the picture to the left and showing the white background bellow the ImageView.

  • Also, should this be inside a thread or something? I need to be able to exit this "scrolling" activity somehow, without using the Back button. I want a button on top of the ImageView (the button should stay still) and onClick it should start another Intent

  • It seems like the picture inside the ImageView is cropped to fit inside the screen. How can I overcome this?


Solution

  • Exemple :

    private Animation           _translateAnimation;
    private ImageView       _image;
    
    _image = (ImageView)findViewById(R.id.yourimage);
    
    _translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 100f, TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f);
    _translateAnimation.setDuration(10000);
    _translateAnimation.setRepeatCount(-1);
    _translateAnimation.setRepeatMode(Animation.REVERSE);
    _translateAnimation.setInterpolator(new LinearInterpolator());
    _image.setAnimation(_translateAnimation);
    

    This will make _linearLoading translate slowly right and left and right and left...

    More duration = more slowly

    TranslateAnimation.ABSOLUTE = Work with the position of the image TranslateAnimation.RELATIVE_TO_PARENT = Work with the parent layout ...

    The method TranslateAnimation(start x, end x, start y, end y)

    play with that