ViewPager轮播无法正确显示小图像

时间:2018-05-25 05:43:53

标签: java android user-interface animation

嗨我是android的新手,最近我使用this制作了一个带有viewpager的旋转木马,它适用于全宽度高度图像,但是当我调整图像大小时,它无法正常工作,我的意思是它需要屏幕的整个宽度而不是指定的宽度。 实际的想法是在页面底部有一个带有小图像的轮播。 所以这就是我被困住的地方,需要一些帮助,实际上我对此并不太了解,所以我很感激一些帮助。 谢谢

我的代码如下

CarouselTransformer.class

public class CarouselEffectTransformer implements ViewPager.PageTransformer {

    private int maxTranslateOffsetX;
    private ViewPager viewPager;

    public CarouselEffectTransformer(Context context) {
        this.maxTranslateOffsetX = dp2px(context, 180);
    }

    public void transformPage(View view, float position) {
        if (viewPager == null) {
            viewPager = (ViewPager) view.getParent();
        }

        int leftInScreen = view.getLeft() - viewPager.getScrollX();
        int centerXInViewPager = leftInScreen + view.getMeasuredWidth() / 2;
        int offsetX = centerXInViewPager - viewPager.getMeasuredWidth() / 2;
        float offsetRate = (float) offsetX * 0.38f / viewPager.getMeasuredWidth();
        float scaleFactor = 1 - Math.abs(offsetRate);

        if (scaleFactor > 0) {
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
            view.setTranslationX(-maxTranslateOffsetX * offsetRate);
            //ViewCompat.setElevation(view, 0.0f);
        }
        ViewCompat.setElevation(view, scaleFactor);

    }

    /**
     * Dp to pixel conversion
     */
    private int dp2px(Context context, float dipValue) {
        float m = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * m + 0.5f);
    }

}

PagerAdapter

public class MyPagerAdapter extends PagerAdapter{
    Context context;

    int adapterType;


  ArrayList<NearbyDataProvider> arrayList  = new ArrayList<>();

    public MyPagerAdapter(Context context,ArrayList<NearbyDataProvider> arrayList, int adapterType) {
        this.context = context;
        this.arrayList = arrayList;
        this.adapterType=adapterType;

    }


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cover, null);
        try {
            NearbyDataProvider nearbyDataProvider = arrayList.get(position);
            LinearLayout linMain = (LinearLayout) view.findViewById(R.id.linMain);
            RoundedImageView imageCover = (RoundedImageView) view.findViewById(R.id.imageCover);
            CustomTextViewMedium customTextViewMedium = (CustomTextViewMedium)view.findViewById(R.id.imageTitle);
            CustomTextViewMedium customTextViewMedium1  =(CustomTextViewMedium)view.findViewById(R.id.imageContent);
            linMain.setTag(position);

            switch (adapterType)
            {
                case NearbyFragment.ADAPTER_TYPE_TOP:

                    break;
                case NearbyFragment.ADAPTER_TYPE_BOTTOM:
                    linMain.setBackgroundResource(0);
                    break;
            }

            GlideApp.with(context)
                    .load(arrayList.get(position).getImage())
                    .into(imageCover);
            customTextViewMedium.setText(nearbyDataProvider.getPlace());
            container.addView(view);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public int getCount() {
        return arrayList.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);
    }


}

0 个答案:

没有答案