Modern carousel library for android

2020-02-10 18:31发布

问题:

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?

回答1:

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



回答2:

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.

Contact me if you are still working on it and need any help.



回答3:

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.



回答4:

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) {

                    }

                });
    }
};