首先看一下实现效果需求:
图片左右滑动展示,实现高度自适应效果
第一步:布局效果
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_banner_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"
android:paddingRight="15dp"
android:textColor="@color/white"
android:textSize="12sp" />
</RelativeLayout>
第二步:适配器设置 - 这里面传入其实就是数据源(图片url - 以及图片高度)
private List<String> list;
private int[] imgheights;
public StarShowDetailsTopAdapter(int[] imgheights, List<String> list) {
this.imgheights = imgheights;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
final ImageView imageView = new ImageView(container.getContext());
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
GlideApp.with(container.getContext()).asBitmap().load(list.get(position)).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap loadedImage, Transition<? super Bitmap> transition) {
if (loadedImage != null) {
float scale = (float) loadedImage.getHeight() / loadedImage.getWidth();
int height = (int) (scale * UiUtils.getScreenWidth(container.getContext()));
int maxHeight = UiUtils.dip2px(container.getContext(), 415);
if (height > maxHeight){//限制图片
imgheights[position] = maxHeight;
}else{
imgheights[position] = height;
}
imageView.setImageBitmap(loadedImage);
} else {
Toast.makeText(container.getContext(), "图片为空", Toast.LENGTH_LONG).show();
}
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) { }
});
container.addView(imageView);
return imageView;
}
第三步:这里要注意的是第一次加载的时候,首先默认一张图片高度加载 - 然后再去默认initViewpager
/**
* 默认加载一张
*/
GlideApp.with(this).asBitmap().load(list.get(0)).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
float scale = (float) resource.getHeight() / resource.getWidth();
int default_height = (int) (scale * UiUtils.getScreenWidth(StarShowDetailActivity.this));
int maxHeight = UiUtils.dip2px(StarShowDetailActivity.this, 415);
if (default_height > maxHeight){
default_height = maxHeight;
}
/**
* init - viewpager
*/
initViewPager(default_height);
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
}
});
接下来就是init - viewpager - 并加载数据了
/**
* 获取第一张图片高度后,给viewpager设置adapter
*/
private void initViewPager(final int default_height) {
if (imgheights == null || imgheights.length != list.size()) {
imgheights = null;
imgheights = new int[list.size()];
}
StarShowDetailsTopAdapter starShowDetailsTopAdapter = new StarShowDetailsTopAdapter(imgheights, list);
mViewPager.setAdapter(starShowDetailsTopAdapter);
/**
* ViewPager设置高度
*/
ViewGroup.LayoutParams params = mViewPager.getLayoutParams();
params.height = default_height;
mViewPager.setLayoutParams(params);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == imgheights.length - 1) {
return;
}
//计算ViewPager现在应该的高度,heights[]表示页面高度的数组。
int height = (int) ((imgheights[position] == 0 ? default_height : imgheights[position]) * (1 - positionOffset) +
(imgheights[position + 1] == 0 ? default_height : imgheights[position + 1]) * positionOffset);
ViewGroup.LayoutParams params = mViewPager.getLayoutParams();//为ViewPager设置高度
params.height = height;
mViewPager.setLayoutParams(params);
}
@Override
public void onPageSelected(int position) {
tv_banner_indicator.setText(position+1 + "/"+ list.size());//默认展示的数量
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
重点总结:其实最重要的就是vp的高度计算
int height = (int) ((imgheights[position] == 0 ? default_height : imgheights[position]) * (1 - positionOffset) +
(imgheights[position + 1] == 0 ? default_height : imgheights[position + 1]) * positionOffset);
好好吃饭,努力学习!有需要的留言发demo.