1 效果图
没有录制动态效果图.....
2 依赖
XBanner 参考地址https://github.com/xiaohaibin/XBanner
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.xiaohaibin:XBanner:1.6.1'
implementation 'com.github.bumptech.glide:glide:4.9.0'
}
3 布局
<com.stx.xhb.xbanner.XBanner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="300dp"
android:clipChildren="false"
app:AutoPlayTime="3000"
app:clipChildrenLeftRightMargin="20dp"
app:isClipChildrenMode="true"
app:isShowNumberIndicator="false"
app:pointsVisibility="false"></com.stx.xhb.xbanner.XBanner>
<!--AutoPlayTime 图片轮播时间间隔-->
<!--tipTextSize 提示文案的文字大小-->
<!--isClipChildrenMode 是否开启一屏显示多个模式-->
<!--pointsVisibility 是否显示指示器-->
<!--clipChildrenLeftRightMargin 一屏显示多个左右间距-->
4 设置图片集合
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private XBanner banner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = findViewById(R.id.banner);
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
list2.add("https://file.alapi.cn/image/comic/215610-154116697054cd.jpg");
}
banner.setData(list2, null);
banner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
//设置图片圆角角度
RoundedCorners roundedCorners = new RoundedCorners(20);
//通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
// RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(MainActivity.this).load(list2.get(position)).apply(options).into((ImageView) view);
}
});
banner.setPageTransformer(Transformer.Default);
}
}