?
implementation 'com.makeramen:roundedimageview:2.3.0'
main
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp300"
android:clipChildren="false"
android:orientation="vertical">
<ImageView
android:id="@+id/ivbg"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp40"
android:layout_marginTop="@dimen/dp20"
android:layout_marginRight="@dimen/dp40"
android:layout_marginBottom="@dimen/dp20"
android:clipChildren="false" />
</RelativeLayout>
</LinearLayout>
</layout>
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- android:foreground="?android:attr/selectableItemBackground"-->
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scaleType="fitXY"
app:riv_border_color="#333333"
app:riv_border_width="2dip"
app:riv_corner_radius="@dimen/dp20"
app:riv_mutate_background="true"
app:riv_oval="false" />
</RelativeLayout>
transform
public class MyAlphaAndScalePageTransformer implements ViewPager.PageTransformer {
final float SCALE_MAX = 0.8F;
final float ALPHA_MAX = 0.5F;
public MyAlphaAndScalePageTransformer() {
}
@Override
public void transformPage(View page, float position) {
float scale = position < 0.0F ? 0.19999999F * position + 1.0F : -0.19999999F * position + 1.0F;
float alpha = position < 0.0F ? 0.5F * position + 1.0F : -0.5F * position + 1.0F;
if (position < 0.0F) {
page.setPivotX((float)page.getWidth());
page.setPivotY((float)(page.getHeight() / 2));
} else {
page.setPivotX(0.0F);
page.setPivotY((float)(page.getHeight() / 2));
}
page.setScaleX(scale);
page.setScaleY(scale);
}
}
adapter
class MyAdapter extends PagerAdapter {
private List<Integer> list;
public MyAdapter(List<Integer> list) {
this.list = list;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View inflate = View.inflate(container.getContext(), R.layout.item_banner, null);
ImageView iv = inflate.findViewById(R.id.iv);
Glide.with(container.getContext())
.load(list.get(position % list.size()))
.into(iv);
container.addView(inflate);
return inflate;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(((View) object));
}
}
main
public class MainActivity extends BaseActivity<ActivityMainBinding> {
private int lastSelect = 1;
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void initShape() {
}
@Override
protected void initView() {
List<Integer> list = new ArrayList<>();
list.add(R.drawable.bg1);
list.add(R.drawable.bg2);
list.add(R.drawable.bg3);
list.add(R.drawable.bg4);
List<Drawable> listDrawables = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Integer integer = list.get(i % list.size());
Glide.with(mContext)
.asBitmap()
.load(integer)
.apply(RequestOptions.bitmapTransform(new BlurTransformation(22,80)))
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
listDrawables.add(ImageUtils.bitmap2Drawable(resource));
}
});
}
ThreadUtils.executeBySingle(new ThreadUtils.Task<Object>() {
@Override
public Object doInBackground() throws Throwable {
Thread.sleep(500);
return null;
}
@Override
public void onSuccess(Object result) {
mViewBinding.ivbg.setImageDrawable(listDrawables.get(lastSelect));
}
@Override
public void onCancel() {
}
@Override
public void onFail(Throwable t) {
}
});
MyAdapter myAdapter = new MyAdapter(list);
mViewBinding.vp.setOffscreenPageLimit(list.size());
mViewBinding.vp.setAdapter(myAdapter);
mViewBinding.vp.setPageMargin(40);
mViewBinding.vp.setCurrentItem(list.size() * 500 + lastSelect);
mViewBinding.vp.setPageTransformer(false, new MyAlphaAndScalePageTransformer());
mViewBinding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Drawable bitmap = listDrawables.get(position % listDrawables.size());
Drawable lastbitmap = listDrawables.get(lastSelect % listDrawables.size());
Glide.with(mContext)
.load(bitmap)
.transition(DrawableTransitionOptions.withCrossFade(1000))
.apply(new RequestOptions().placeholder(lastbitmap).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true))
.into(mViewBinding.ivbg);
lastSelect = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
protected void initData() {
}
@Override
protected void initListener() {
}
}
|