EDITED
Taking into account that the images are stored locally, in the Drawable folder, I would do the following:
You create a layout with a view of type ViewPager.
You create another layout with a view of type ImageView, to contain the images of the ViewPager.
You create an array-type xml with the references to the drawables.
You create a class that extends from PagerAdapter and passes the array created in the previous point.
Set the viewPager with the adapter.
Here is an example of all the steps.
file name: activity_main.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent>
<android.support.v4.view.ViewPager
android:id="@+id/image_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
file name: element_image.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
filename of arrays.xml
<array name="images">
<item>@drawable/image_1</item>
<item>@drawable/image_2</item>
<item>@drawable/image_3</item>
<item>@drawable/image_4</item>
</array>
file name: ImageAdapter.java
public class ImageAdapter extends PagerAdapter {
private TypedArray images;
private LayoutInflater layoutInflater;
public ImageAdapter(Context context) {
images = context.getResources().obtainTypedArray(R.array.images);
layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = layoutInflater.inflate(R.layout.element_image, container, false);
ImageView image = (ImageView) itemView.findViewById(R.id.item_image);
image.setImageDrawable(images.getDrawable(position));
((ViewGroup) image.getParent()).removeView(image);
container.addView(image);
return image;
}
@Override
public int getCount() {
return images.length();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
}
file name: MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageAdapter imageAdapter = new ImageAdapter(this);
ViewPager imageGallery = findViewById(R.id.image_gallery);
imageGallery.setAdapter(imageAdapter);
}
}
I think the code fits better now than you wanted
a greeting.