package com.example.library;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import java.lang.ref.WeakReference;
//适配器
public abstract class BaseOverlayPageAdapter extends PagerAdapter {
private Context context;
private String[] imgUrls;
private WeakReference<Bitmap>[] bitmaps;
protected RequestOptions mRequestOptions;
public BaseOverlayPageAdapter(Context context, @NonNull RequestOptions imageOptions) {
this.context = context;
this.mRequestOptions = imageOptions;
}
/** * item布局 * * @return */
protected abstract View itemView();
public void setImgUrlsAndBindViewPager(ViewPager vp, String[] imgUrls) {
setImgUrlsAndBindViewPager(vp, imgUrls, 3);
}
public void setImgUrlsAndBindViewPager(ViewPager vp, String[] imgUrls, int layerAmount) {
setImgUrlsAndBindViewPager(vp, imgUrls, layerAmount, -1, -1);
}
/** * @param vp * @param imgUrls * @param layerAmount 显示层数 */
public void setImgUrlsAndBindViewPager(ViewPager vp, String[] imgUrls, int layerAmount, float scaleOffset, float transOffset) {
this.imgUrls = imgUrls;
if (imgUrls != null && imgUrls.length > 0) {
bitmaps = new WeakReference[imgUrls.length];
vp.setOffscreenPageLimit(layerAmount);
OverlayTransformer transformer = new OverlayTransformer(layerAmount, scaleOffset, transOffset);
vp.setPageTransformer(true, transformer);
}
Log.i("liuhongliang","String[] imgUrls = "+ imgUrls.toString()+" layerAmount = "+layerAmount +" scaleOffset = " +scaleOffset);
}
@Override
public int getCount() {
if (null == imgUrls)
return 0;
if (imgUrls.length <= 1)
return imgUrls.length;
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
protected ImageView findImageView(View rootView) {
ImageView iv = rootView.findViewById(R.id.card_iv);
if (null != iv)
return iv;
if (rootView instanceof ImageView) {
return (ImageView) itemView();
}
throw new RuntimeException("you should set one of ImageViews id=card_iv or rootView=ImageView");
}
public onCklie onCklie;
interface onCklie {
void o(int p);
}
public void setOnCklie(BaseOverlayPageAdapter.onCklie onCklie) {
this.onCklie = onCklie;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
final int p = position % imgUrls.length;
final String imgUrl = imgUrls[p];
View view = itemView();
if (null == view) {
throw new RuntimeException("you should set a item layout");
}
final ImageView iv = findImageView(view);
if (null == iv) {
throw new RuntimeException("you should set a item layout");
}
if (null != bitmaps && null != bitmaps[p] && null != bitmaps[p].get()) {
iv.setImageBitmap(bitmaps[p].get());
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("liuhongliang ","instantiateItem 点击"+position);
Toast.makeText(context,"我是第" +position +"个",Toast.LENGTH_LONG);
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.i("liuhongliang ","我长按了 点击"+position);
return false;
}
});
Glide.with(context).asBitmap().load(imgUrl).apply(mRequestOptions).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
bitmaps[p] = new WeakReference<Bitmap>(resource);
iv.setImageBitmap(resource);
}
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
super.onLoadStarted(placeholder);
iv.setImageDrawable(placeholder);
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
iv.setImageDrawable(errorDrawable);
}
});
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
package com.example.library;
import android.support.v4.view.ViewPager;
import android.view.View;
/** * 叠加卡片效果 * Created by Acorn on 2017/12/29. */
public class OverlayTransformer implements ViewPager.PageTransformer {
private float scaleOffset = 40;
private float transOffset = 40;
private int overlayCount;
public OverlayTransformer(int overlayCount) {
this.overlayCount = overlayCount;
}
public OverlayTransformer(int overlayCount, float scaleOffset, float transOffset) {
this.overlayCount = overlayCount;
if (Float.compare(scaleOffset, -1) != 0)
this.scaleOffset = scaleOffset;
if (Float.compare(transOffset, -1) != 0)
this.transOffset = transOffset;
}
public int getOverlayCount() {
return overlayCount;
}
@Override
public void transformPage(View page, float position) {
if (position <= 0.0f) {
//当前页
page.setTranslationX(0f);
page.setAlpha(1 - 0.5f * Math.abs(position));
page.setClickable(true);
} else {
otherTrans(page, position);
page.setClickable(false);
}
}
private void otherTrans(View page, float position) {
//缩放比例
float scale = (page.getWidth() - scaleOffset * position) / (float) (page.getWidth());
page.setScaleX(scale);
page.setScaleY(scale);
page.setAlpha(1f);
if (position > overlayCount - 1 && position < overlayCount) {
//当前页向右滑动时,最右面第四个及以后页面应消失
float curPositionOffset = transOffset * (float) Math.floor(position); //向下取整
float lastPositionOffset = transOffset * (float) Math.floor(position - 1); //上一个卡片的偏移量
float singleOffset = 1 - Math.abs(position % (int) position);
float transX = (-page.getWidth() * position) + (lastPositionOffset + singleOffset * (curPositionOffset - lastPositionOffset));
page.setTranslationX(transX);
} else if (position <= overlayCount - 1) {
float transX = (-page.getWidth() * position) + (transOffset * position);
page.setTranslationX(transX);
} else {
page.setAlpha(0f);
// page.setTranslationX(0); //不必要的隐藏在下面
}
}
}
//调用类
package com.example.library;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;`在这里插入代码片`
import com.bumptech.glide.request.RequestOptions;
public class SimpleOverlayAdapter extends BaseOverlayPageAdapter {
private LayoutInflater mInflater;
private Context context;
public SimpleOverlayAdapter(Context context) {
super(context, new RequestOptions().error(R.drawable.error).placeholder(R.drawable.placeholder));
this.context =context;
mInflater = LayoutInflater.from(context);
}
public SimpleOverlayAdapter(Context context, RequestOptions imageOptions) {
super(context, imageOptions);
mInflater = LayoutInflater.from(context);
}
@Override
protected View itemView() {
return mInflater.inflate(R.layout.item_viewpager, null);
}
}
package com.acorn.overlaycardviewpager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.library.BaseOverlayPageAdapter;
import com.example.library.SimpleOverlayAdapter;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private String[] imgUrls = new String[]{
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556094759217&di=943686daea0415763a8364d3ac0d8233&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fe7abe6079c12527f838195f6b0de9c88bdb0b6cb.jpg"
, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556094800005&di=ba8f6dbb833600fa76af7850265d1309&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0145b25cade37fa801214168567afc.jpg%401280w_1l_2o_100sh.jpg"
, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556094815760&di=1abe539eb4691346c07dd44a6bba7383&imgtype=0&src=http%3A%2F%2Fpic.xoyo.com%2Fbbs%2F2010%2F11%2F30%2F10113010300bdf68f9f96b70e4.jpg"
, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556094832094&di=ceb750ce77ad8699859603b80f6479f5&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015fa95c67b3d7a801203d22e96898.jpg%401280w_1l_2o_100sh.jpg"
, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556094846679&di=fb63907df03214def9e4576149ac1b8e&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fc9257b7638df71ad54d17de9cc92034741c86236.jpg"
, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556094860210&di=56a4eba83e1fa1fee69787b35f5b2806&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F5d3129cc0cc155f696a68eec5356d9387f4c517d.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = findViewById(R.id.example_vp);
SimpleOverlayAdapter adapter = new SimpleOverlayAdapter(this);
adapter.setImgUrlsAndBindViewPager(vp, imgUrls, 3);
vp.setAdapter(adapter);
TextView maxye = findViewById(R.id.max);
TextView fy = findViewById(R.id.ye);
maxye.setText(imgUrls.length+"");
}
}
文章评论