Open In App

Scroll ImageView in Android

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to implement a very important feature related to the ImageView. The image will keep on scrolling horizontally by itself. When we click on the image then it will stop scrolling and when we again click it will again keep scrolling. We can use this feature to show animation in an app. This feature can be used in Online Shopping App. When the item is out for delivery then they can use this feature to show destination. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Scroll ImageView in Android Sample GIF

Step By Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency and JitPack Repository

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation ‘com.github.Q42:AndroidScrollingImageView:1.3.4’

Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.

allprojects {

 repositories {

   …

   maven { url “https://jitpack.io” }

     }

}

After adding this dependency sync your project and now we will move towards its implementation.  

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. There are three attributes for the Scrolling ImageView:

  • speed is the number of dp to move the drawable per second
  • source is drawable to paint. May refer to an array of drawable
  • contiguous When the source is an array of drawable, contiguous determine their ordering.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:scrolling_image_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="0dp"
    tools:context=".MainActivity">
  
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
  
        <com.q42.android.scrollingimageview.ScrollingImageView
            android:id="@+id/scrolling_background"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            scrolling_image_view:source="@drawable/scrolling_background"
            scrolling_image_view:speed="60dp" />
  
        <com.q42.android.scrollingimageview.ScrollingImageView
            android:id="@+id/scrolling_foreground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            scrolling_image_view:source="@drawable/scrolling_foreground"
            scrolling_image_view:speed="150dp" />
          
    </FrameLayout>
  
    <ImageView
        android:id="@+id/scr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="centerInside"
        android:src="@drawable/van" />
  
</RelativeLayout>


Note: Drawable Files can be found here.

Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.q42.android.scrollingimageview.ScrollingImageView;
  
public class MainActivity extends AppCompatActivity {
  
    boolean moving1 = true, moving2 = true;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ScrollingImageView scrollingBackground = (ScrollingImageView) findViewById(R.id.scrolling_background);
  
        scrollingBackground.start();
    }
}


Output:



Last Updated : 11 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads