12

Is there any library available for Carousel animation of images?

I found http://www.androidviews.net/2012/10/android-coverflow/ ; https://code.google.com/p/android-coverflow/ but this library/snippet use extends gallay (Gallery class was deprecated in API level 16. (4.1.2)), so is there any (modern) library? Maybe in combination with picasso?

Pravin Desai
  • 527
  • 5
  • 18
Björn Ternes
  • 1,137
  • 4
  • 14
  • 33
  • [Check out this question and answer](http://stackoverflow.com/questions/35475178/how-can-i-create-viewpager-with-previous-and-next-page-preview-and-centred-one/35498701#35498701) – Bincy Baby Apr 07 '16 at 08:53
  • Try my project: https://github.com/vejei/CarouselView – zeleven May 10 '20 at 13:19

5 Answers5

13

Here is the free link of CarouselView Github Link and Android Arsenal

Gradle:

compile 'com.synnapps:carouselview:0.0.7'

Maven:

<dependency>
 <groupId>com.synnapps</groupId> 
 <artifactId>carouselview</artifactId>
 <version>0.0.7</version>
 <type>pom</type>
</dependency>

Usage:
Include following code in your layout:

<com.synnapps.carouselview.CarouselView
    android:id="@+id/carouselView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:fillColor="#FFFFFFFF"
    app:pageColor="#00000000"
    app:radius="6dp"
    app:slideInterval="3000"
    app:strokeColor="#FF777777"
    app:strokeWidth="1dp"/>

Include following code in your activity:

public class SampleCarouselViewActivity extends AppCompatActivity {

CarouselView carouselView;

int[] sampleImages = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample_carousel_view);

    carouselView = (CarouselView) findViewById(R.id.carouselView);
    carouselView.setPageCount(sampleImages.length);

    carouselView.setImageListener(imageListener);
}

ImageListener imageListener = new ImageListener() {
    @Override
    public void setImageForPosition(int position, ImageView imageView) {
        imageView.setImageResource(sampleImages[position]);
    }
};

}

If you want to add custom view, implement ViewListener.

public class SampleCarouselViewActivity extends AppCompatActivity {

CarouselView customCarouselView;
int NUMBER_OF_PAGES = 5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample_carousel_view);

    customCarouselView = (CarouselView) findViewById(R.id.customCarouselView);
    customCarouselView.setPageCount(NUMBER_OF_PAGES);
    // set ViewListener for custom view 
    customCarouselView.setViewListener(viewListener);
}

ViewListener viewListener = new ViewListener() {

    @Override
    public View setViewForPosition(int position) {
        View customView = getLayoutInflater().inflate(R.layout.view_custom, null);
        //set view attributes here

        return customView;
    }
};

For more information go to github link

  • It throws me FATAL EXCEPTION: main Process: com.xxx.xxxxxx, PID: 10881 java.lang.RuntimeException: View must set ImageListener or ViewListener. – iwooli Apr 14 '16 at 08:10
  • customCarouselView.setViewListener(viewListener);customCarouselView.setPageCount(NUMBER_OF_PAGES); first set viewlistner then set pagecount – Rajesh N May 04 '17 at 10:35
  • Hi sir. i know this is already a long time post, but can i put a XML view in the last array in the carousel? – Janessa Bautista May 20 '20 at 11:24
8

I haven't found any such library available when searching for it, but when making my own app and using a ViewPager to display fragments, I've implemented my own class which implements ViewPager.PageTransformer.

Though you've asked for a library, I ran into a similar situation but ended up finding it easy enough to create my own class for it, so you may too. My guidance came from Using ViewPager for Screen Slides in the Google Developer documentation. Here is my code:

public class CarouselPageTransformer implements ViewPager.PageTransformer {
    @Override
    public void transformPage(View page, float position) {
        float scaleFactor = 0.5f;
        float rotationFactor = 20;

        if (position < 0) {
            page.setRotationY(rotationFactor * -position);
            float scale = 1 + scaleFactor * position;
            page.setScaleX(scale);
            page.setScaleY(scale);

        } else {
            page.setRotationY(rotationFactor * -position);
            float scale = 1 - scaleFactor * position;
            page.setScaleX(scale);
            page.setScaleY(scale);
        }
    }
}

The activity hosting the transform then calls:

viewPager.setPageTransformer(true, new CarouselPageTransformer());

This code positions the ViewPager's fragments to the sides of the visible screen, but you could just as easily position them to be visible, such as in the link you provided in your question.

Be aware of the minimum SDK required; from the documentation for ViewPager.PageTransform:

As property animation is only supported as of Android 3.0 and forward, setting a PageTransformer on a ViewPager on earlier platform versions will be ignored.

Jules
  • 1,677
  • 1
  • 19
  • 25
6

Very Old Solution. Ignore

I have tried searching a lot for an alternative to Carousel. I finally found the perfect alternative.

InfiniteCycleViewPager

This does just what the Carousel library does.

I modified it to load images from my SD card rather than Drawables.

Works Perfectly fine.

Devenom
  • 915
  • 1
  • 9
  • 20
0
private void setImageToCaroussel(){

    imgUrls.add("https://im.idiva.com//content/2015/May/salon_ettiquette1.jpg");
    imgUrls.add("https://www.womenfitness.net/wp/wp-content/uploads/2018/04/What-to-look-for-when-choosing-a-hair-salon-1000x667.jpg");
    imgUrls.add("https://cdn.website.thryv.com/e3abe5c926184c4ca25389d43ae29913/dms3rep/multi/mobile/713b0a2.jpg");

    carouselView = (CarouselView) findViewById(R.id.carouselView);
    carouselView.setPageCount(imgUrls.size());

    carouselView.setImageListener(imageListener);

}
 ImageListener imageListener = new ImageListener() {
    @Override
    public void setImageForPosition(final int position, final ImageView imageView) {

        Glide.with(ShopActivity.this)
                .asBitmap()
                .load(imgUrls.get(position))
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@Nullable Bitmap resource,@Nullable Transition<? super Bitmap> transition) {
                        imageView.setImageBitmap(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }

                });
    }
};
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Jaber
  • 9
  • 1
  • 4
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Jul 31 '19 at 22:52
0

call setImageListener before setPageCount will resolve

Asad
  • 283
  • 3
  • 6