Image Slider
Dependency :
This is an amazing image slider for the Android .
You can easily load images with your custom layout, and there are many kinds of amazing animations you can choose.
implementation 'com.github.smarteist:autoimageslider:1.4.0'
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SliderViewPager">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="300dp"
app:sliderAnimationDuration="600"
app:sliderAutoCycleDirection="right"
app:sliderAutoCycleEnabled="true"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="2dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="1"
app:sliderStartAutoCycle="true" />
</LinearLayout>
MainActivity.java
package com.example.myapplication10;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.myapplication10.Adapter.SliderAdapter;
import com.smarteist.autoimageslider.IndicatorView.animation.type.IndicatorAnimationType;
import com.smarteist.autoimageslider.SliderAnimations;
import com.smarteist.autoimageslider.SliderView;
public class SliderViewPager extends AppCompatActivity {
private SliderView imageSlider;
int images[]={R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider_view_pager);
imageSlider=findViewById(R.id.imageSlider);
SliderAdapter sliderAdapter=new SliderAdapter(SliderViewPager.this,images);
imageSlider.setSliderAdapter(sliderAdapter);
imageSlider.setIndicatorAnimation(IndicatorAnimationType.WORM);
imageSlider.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
imageSlider.startAutoCycle();
}
}
SliderAdapte.java
package com.example.myapplication10.Adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.myapplication10.R;
import com.example.myapplication10.SliderViewPager;
import com.smarteist.autoimageslider.SliderViewAdapter;
public class SliderAdapter extends SliderViewAdapter<SliderAdapter.ViewData> {
Activity activity;
int[] images;
public SliderAdapter(SliderViewPager sliderViewPager, int[] images) {
activity=sliderViewPager;
this.images=images;
}
@Override
public ViewData onCreateViewHolder(ViewGroup parent) {
View view= LayoutInflater.from(activity).inflate(R.layout.slider_item,parent,false);
return new ViewData(view);
}
@Override
public void onBindViewHolder(ViewData viewHolder, int position) {
viewHolder.img.setImageResource(images[position]);
}
@Override
public int getCount() {
return images.length;
}
class ViewData extends SliderViewAdapter.ViewHolder {
private final ImageView img;
public ViewData(View itemView) {
super(itemView);
img=itemView.findViewById(R.id.img);
}
}
}
Slider_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:id="@+id/img"/>
</LinearLayout>
0 Comments