Android Autoscroll imageview

前端 未结 3 1128
囚心锁ツ
囚心锁ツ 2021-02-06 15:30

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 re

相关标签:
3条回答
  • 2021-02-06 15:48

    What you could do is add the ImageView inside a ScrollView. Like this

    <ScrollView
        android:id="@+id/img_scroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/IMAGENAME" />
    </ScrollView>
    

    As you want to scroll the Image automatically, you could create a AsyncTask and inside doInBackground() method, create a new Runnable or runOnUiThread() and send commands to the ScrollView using it's ID to scroll as you need it to.

    0 讨论(0)
  • 2021-02-06 15:51

    So, the solution to my problem is here (for all of the new android developers, like me, who might need this for their apps):

    public class MainActivity extends Activity {
    
        Animation           _translateAnimation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            _translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, -300f, TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f);
            _translateAnimation.setDuration(8000);
            _translateAnimation.setRepeatCount(-1);
            _translateAnimation.setRepeatMode(Animation.REVERSE); // REVERSE
            _translateAnimation.setInterpolator(new LinearInterpolator());
            ImageView img = (ImageView) findViewById(R.id.img);
            img.startAnimation(_translateAnimation);
        }
    }
    

    and the layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/global_relative"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical"
        >
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="600dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@drawable/rainforest"
            android:scaleType="center"
            android:src="@drawable/rainforest613x490" >
    
        </ImageView>
    </ScrollView>  
    </RelativeLayout>
    

    just make sure your image is big/high enough to fit the screen, and is (+300dp) wider than the screen width (or adjust the code above).

    Happy coding ;)

    0 讨论(0)
  • 2021-02-06 16:03

    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

    0 讨论(0)
提交回复
热议问题