Android Autoscroll imageview

In my activity I have only ImageView. In it, src is an image that is much larger than the screen. I want the image to scroll slooooowly from left to right until it reaches the right edge of the photo, then start scrolling back to the left until it reaches the left edge. Then start all over again. I need this to happen in a separate thread so that the phone does not freeze while this is happening. 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 that does not work. I mean, the scroll goes from left to right, β€œpushing” the image to the left and displaying a white background in the ImageView.

  • Also, should it be inside the thread or something else? I need to somehow get out of this "scroll" without using the "Back" button. I want a button on the top of the ImageView (the button should remain stationary), and onClick to start another Intent

  • It seems that the image inside the ImageView has been cropped to make it fit inside the screen. How can I overcome this?

+4
source share
3 answers

Example:

 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 cause _linearLoading to move slowly to the right, left and right and left ...

More Duration = Slower

TranslateAnimation.ABSOLUTE = Working with the image position TranslateAnimation.RELATIVE_TO_PARENT = Working with the parent layout ...

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

play with it

+2
source

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

 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 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 large / large enough to fit the screen, and (+ 300dp) wider than the screen width (or edit the code above).

Happy coding;)

+4
source

What you can do is add an 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 automatically scroll the image, you can create an AsyncTask method inside doInBackground (), create new Runnable or runOnUiThread() commands and send ScrollView commands using the ID to scroll as needed.

0
source

Source: https://habr.com/ru/post/1498147/


All Articles